Last year I asked, “Will we be flattening our HTML for CSS Grids?“
The issue is that the only way for elements to participate in the same CSS grid together (or flexbox for that matter) is for them to be siblings. So, in some cases we might be incentivized to forego HTML semantics for the benefit of layout (not great).
One answer to this is
display: contents;—a magical new display value that essentially makes the container disappear, making the child elements children of the element the next level up in the DOM.
Wanna understand it better? Rachel Andrew wrote “Vanishing boxes with display contents” and clarifies how it all works:
This value becomes useful if you want to add some element because it makes sense in terms of document semantics, but doesn’t in terms of display. Perhaps you have some content that makes sense marked up as an article, that article is then a flex item in your layout BUT the elements you really would like to be flex items are nested inside that article. Rather than flattening your markup and remove the article element to enable these inner elements to be part of the flex layout, you could remove the boxes generated by article using display: contents. You then get the best of both worlds, semantic markup plus the visual display your design requires. That sounds good to me.
Manuel Rego takes a stab at explaining it as well:
display: contentsmakes that the div doesn’t generate any box, so its background, border and padding are not rendered. However the inherited properties like color and font have effect on the child (span element) as expected.
There is also a very related subject to all this: subgrids. Probably literally
display: subgrid;. It’s probably less important in terms of maintaining semantics than
display: contents; but also different.
Eric Meyer called subgrids essential:
Grid layout is the first serious candidate to fill that hole in the past two decades, and I don’t want to see them hamstrung from the outset. Subgrids are essential to the adoption of grids. I hope they’ll be implemented as soon as possible
And to understand the difference, Rachel Andrew also wrote “Why display: contents is not CSS Grid Layout subgrid“:
You won’t get far through a conversation about subgrid in CSS Grid Layout without someone suggesting that
display: contentssolves most of the problems anyway, so do we really need subgrid? This really isn’t the case,
display: contentsdoes indeed solve a class of problems, but these are different problems to those that subgrid would help us with.
CSS Tricks Go to Source
Powered by WPeMatico