
Description
I often set grid-item properties on elements which by design I shouldn’t have much context about. For example when laying out different children on a (reusable) parent grid-container. This results in having to match grid-items based on their specific attributes (e.g. class names).
It would be great to be able to set the layout logic on a parent and separate it from children.
Instead of:
<div class="parent">
<div class="child"></div>
<div class="child"></div>
<div class="child"></div>
</div>
.parent {
display: grid;
}
.child {
grid-column: 1 / 2;
}
We could have:
<div class="parent">
<div></div>
<div></div>
<div></div>
</div>
.parent {
display: grid;
}
.parent:grid-item {
grid-column: 1 / 2;
}
This way we wouldn’t have to assume much about grid-items.
As a workaround I’ve been using the .parent > *
compound selector to match grid-items, but this is not ideal for potential specificity issues.
A step further would match .parent:grid-item
only when .parent
has display: grid
.
I also see this principle loosely applying on flexbox parent-child relationship.
My argument is to set all layout logic on a parent and have children follow the rules while being completely context-agnostic.
Cheers!