Subgrid for Nested Alignment
snippetSay we have a list of value groups:
- π 30 π 30 π 7
- π 70 π 43.5 π 17
- π 83.5 π 100 π 49
Similar fields should be aligned for visual consistency; vertically stacked (i.e. columns).
We cannot make assumptions about fieldsβ values, so assigning fixed widths is not an option.
<table>
seems the obvious solution, but is semantically undesirable in this
case (in part because text messages might be interspersed among tabular data).
π 30 | π 30 | π 7 |
π 70 | π 43.5 | π 17 |
π 83.5 | π 100 | π 49 |
Letβs assume that display: table
is equally undesirable.
That leaves subgrid:
- π 30 π 30 π 7
- π 70 π 43.5 π 17
- π 83.5 π 100 π 49
This also allows us to add arbitrary text content in between tabular items:
- π 30 π 30 π 7
- π 70 π 43.5 π 17
- Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
- π 83.5 π 100 π 49
(Note that we added an extra column to avoid unnecessarily extending tabular items.)
.grid {
--cell-padding: calc(0.5 * var(--spacing)) var(--spacing);
--cell-border: 1px dashed #CCC;
display: inline-grid;
grid-template-columns: repeat(4, auto);
padding-inline: 0;
list-style-type: none;
> * {
grid-column: 1 / -1;
}
> .text {
font-size: small;
}
> :not(.text) {
display: grid;
grid-template-columns: subgrid;
> * {
border: var(--cell-border);
padding: var(--cell-padding);
& + * {
border-left: none; /* collapse */
}
}
& + * > * {
border-top: none; /* collapse */
}
> :nth-child(1) {
grid-column: 1 / 2;
}
> :nth-child(2) {
grid-column: 2 / 3;
}
> :nth-child(3) {
grid-column: 3 / 4;
}
}
}