{"id":3137,"date":"2019-10-21T13:20:02","date_gmt":"2019-10-21T13:20:02","guid":{"rendered":"https:\/\/rengga.dev\/blog\/?p=3137"},"modified":"2023-02-12T03:08:22","modified_gmt":"2023-02-12T03:08:22","slug":"css-tutorial-css-grid-of-puzzle-pieces","status":"publish","type":"post","link":"https:\/\/rengga.dev\/blog\/css-tutorial-css-grid-of-puzzle-pieces\/","title":{"rendered":"CSS Tutorial &#8211; CSS Grid of Puzzle Pieces"},"content":{"rendered":"<p><span style=\"color: #ef3207;\"><a style=\"color: #ef3207;\" href=\"https:\/\/rengga.dev\/\" target=\"_blank\" rel=\"noopener\"><strong>Rengga Dev<\/strong><\/a> <\/span>&#8211; This time, I want to dive into another type of grid, one that works with shapes.<\/p>\n<p>Like, what if the images aren\u2019t perfectly square but instead are shaped like hexagons or rhombuses? Spoiler alert: we can do it. In fact, we\u2019re going to combine CSS Grid techniques we\u2019ve looked at and drop in some CSS\u00a0<code>clip-path<\/code>\u00a0and\u00a0<code>mask<\/code>\u00a0magic to create fancy grids of images for just about any shape you can imagine!<\/p>\n<p>This time we are going to play with the CSS\u00a0<code>mask<\/code>\u00a0property to make the images look like pieces of a puzzle.<br \/>\n<iframe style=\"width: 100%;\" title=\"Puzzle-like grid of images\" src=\"https:\/\/codepen.io\/renggagumilar\/embed\/LYdaPRd?default-tab=result&amp;theme-id=dark\" height=\"500\" frameborder=\"no\" scrolling=\"no\" allowfullscreen=\"allowfullscreen\"><br \/>\nSee the Pen <a href=\"https:\/\/codepen.io\/renggagumilar\/pen\/LYdaPRd\"><br \/>\nPuzzle-like grid of images<\/a> by Rengga Gumilar (<a href=\"https:\/\/codepen.io\/renggagumilar\">@renggagumilar<\/a>)<br \/>\non <a href=\"https:\/\/codepen.io\">CodePen<\/a>.<br \/>\n<\/iframe><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">.gallery {\r\n  --s: 200px; \/* control the size *\/\r\n  --g: 6px;   \/* control the gap *\/\r\n  --r: 42px;  \/* control the circular shapes *\/\r\n  \r\n  display: grid;\r\n  gap: var(--g);\r\n  grid: auto-flow var(--s)\/repeat(2,var(--s));\r\n}\r\n.gallery &gt; img {\r\n  object-fit: cover;\r\n  -webkit-mask: var(--_m);\r\n          mask: var(--_m);\r\n}\r\n.gallery &gt; img:is(:nth-child(1),:nth-child(4)) {\r\n  width: 100%;\r\n  height: calc(100% + var(--r));\r\n}\r\n.gallery &gt; img:is(:nth-child(2),:nth-child(3)) {\r\n  height: 100%;\r\n  width: calc(100% + var(--r));\r\n}\r\n.gallery &gt; img:nth-child(1) {\r\n  --_m: \r\n    radial-gradient(calc(var(--r) + var(--g)) at calc(100% + var(--g)) 50%,#0000 95%,#000) \r\n      top\/100% calc(100% - var(--r)) no-repeat, \r\n    radial-gradient(var(--r) at left 50% bottom var(--r),#000 95%,#0000);\r\n}\r\n.gallery &gt; img:nth-child(2) {\r\n  place-self: end;\r\n  --_m: \r\n    radial-gradient(calc(var(--r) + var(--g)) at 50% calc(100% + var(--g)),#0000 95%,#000) \r\n      right\/calc(100% - var(--r)) 100% no-repeat, \r\n    radial-gradient(var(--r) at top 50% left var(--r),#000 95%,#0000);\r\n}\r\n.gallery &gt; img:nth-child(3) {\r\n  --_m: \r\n    radial-gradient(calc(var(--r) + var(--g)) at 50% calc(0% - var(--g)),#0000 95%,#000) \r\n      left\/calc(100% - var(--r)) 100% no-repeat, \r\n    radial-gradient(var(--r) at top 50% right var(--r),#000 95%,#0000);\r\n}\r\n.gallery &gt; img:nth-child(4) {\r\n  place-self: end;\r\n  --_m: \r\n    radial-gradient(calc(var(--r) + var(--g)) at calc(0% - var(--g)) 50%,#0000 95%,#000) \r\n      bottom\/100% calc(100% - var(--r)) no-repeat, \r\n    radial-gradient(var(--r) at left 50% top var(--r),#000 95%,#0000);\r\n}\r\n\r\nbody {\r\n  margin: 0;\r\n  min-height: 100vh;\r\n  display: grid;\r\n  place-content: center;\r\n  background: #ECD078;\r\n}<\/pre>\n<p>Here is another example where I am using a conic gradient instead of a radial gradient. This gives us triangular puzzle pieces while keeping the same underlying HTML and CSS.<br \/>\n<iframe style=\"width: 100%;\" title=\"Puzzle Grid of images\" src=\"https:\/\/codepen.io\/renggagumilar\/embed\/YzagKNO?default-tab=result&amp;theme-id=dark\" height=\"500\" frameborder=\"no\" scrolling=\"no\" allowfullscreen=\"allowfullscreen\"><br \/>\nSee the Pen <a href=\"https:\/\/codepen.io\/renggagumilar\/pen\/YzagKNO\"><br \/>\nPuzzle Grid of images<\/a> by Rengga Gumilar (<a href=\"https:\/\/codepen.io\/renggagumilar\">@renggagumilar<\/a>)<br \/>\non <a href=\"https:\/\/codepen.io\">CodePen<\/a>.<br \/>\n<\/iframe>A last one! This time I am using\u00a0<code>clip-path<\/code>\u00a0and since it\u2019s a property we can animate, we get a cool hover by simply updating the custom property that controls the shape.<br \/>\n<iframe style=\"width: 100%;\" title=\"Puzzle-like grid of images with hover effect\" src=\"https:\/\/codepen.io\/renggagumilar\/embed\/ExEMYWw?default-tab=result&amp;theme-id=dark\" height=\"500\" frameborder=\"no\" scrolling=\"no\" allowfullscreen=\"allowfullscreen\"><br \/>\nSee the Pen <a href=\"https:\/\/codepen.io\/renggagumilar\/pen\/ExEMYWw\"><br \/>\nPuzzle-like grid of images with hover effect<\/a> by Rengga Gumilar (<a href=\"https:\/\/codepen.io\/renggagumilar\">@renggagumilar<\/a>)<br \/>\non <a href=\"https:\/\/codepen.io\">CodePen<\/a>.<br \/>\n<\/iframe><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Rengga Dev &#8211; This time, I want to dive into another type <a class=\"read-more\" href=\"https:\/\/rengga.dev\/blog\/css-tutorial-css-grid-of-puzzle-pieces\/\" title=\"CSS Tutorial &#8211; CSS Grid of Puzzle Pieces\" itemprop=\"url\"><\/a><\/p>\n","protected":false},"author":1,"featured_media":3181,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11,2],"tags":[268,302,197,103,227],"newstopic":[],"class_list":{"0":"post-3137","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-html-css","8":"category-web-design","9":"tag-css-grid","10":"tag-css-grid-of-puzzle-pieces","11":"tag-css-tutorial","12":"tag-web-design","13":"tag-web-designer"},"_links":{"self":[{"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/posts\/3137","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/comments?post=3137"}],"version-history":[{"count":1,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/posts\/3137\/revisions"}],"predecessor-version":[{"id":3139,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/posts\/3137\/revisions\/3139"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/media\/3181"}],"wp:attachment":[{"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/media?parent=3137"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/categories?post=3137"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/tags?post=3137"},{"taxonomy":"newstopic","embeddable":true,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/newstopic?post=3137"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}