{"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 :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

Breaking that down:<\/p>\n\n\n\n