{"id":367158,"date":"2022-08-02T06:10:49","date_gmt":"2022-08-02T13:10:49","guid":{"rendered":"https:\/\/css-tricks.com\/?p=367158"},"modified":"2022-08-02T06:10:51","modified_gmt":"2022-08-02T13:10:51","slug":"implicit-grids-repeatable-layout-patterns-and-danglers","status":"publish","type":"post","link":"https:\/\/css-tricks.com\/implicit-grids-repeatable-layout-patterns-and-danglers\/","title":{"rendered":"Implicit Grids, Repeatable Layout Patterns, and Danglers"},"content":{"rendered":"\n
Dave Rupert with some modern CSS magic<\/a> that tackles one of those classic conundrums: what happens when the CSS for component is unable to handle the content we throw at it?<\/p>\n\n\n\n The specific situation is when a layout grid expects an even number of items, but is supplied with an odd number instead. We\u2019re left with a \u201cdangling\u201d element at the end that throws off the layout. Sounds like what\u2019s needed is some Defensive CSS<\/a> and Dave accomplishes it.<\/p>\n\n\n\n\n\n\n\n He reaches for Breaking that down:<\/p>\n\n\n\n In this case, that last If\u2026 then\u2026 CSS has conditional logic powers! We’re only talking about support for Safari TP and Edge\/Chrome Canary at the moment, but that’s pretty awesome.<\/p>\n\n\n\n:has()<\/code> to write a nifty selector that sniffs out the last item in a grid that contains an odd number of items:<\/p>\n\n\n\n
.items:has(.item:last-of-type:nth-of-type(odd)) .item:first-of-type { }<\/code><\/pre>\n\n\n\n
.items<\/code>.<\/li>
:has()<\/code> an
.item<\/code> child that is the last of its type,<\/li>
.item<\/code> happens to be an odd-numbered instance,<\/li>
.item<\/code> element of that type and style it!<\/li><\/ul>\n\n\n\n
.item<\/code> can be set to go full-width to prevent holes in the layout.<\/p>\n\n\n\n