A modern CSS layout cheatsheet for 2026
Container queries, subgrid and :has() — the layout features you can finally use everywhere.
CSS layout has quietly become excellent. Features that needed JavaScript or fragile hacks a few years ago are now baseline in every evergreen browser. Here is the short list worth memorising.
Container queries
Media queries ask about the viewport. Container queries ask about the component’s own space, which is what you actually care about when a card can live in a sidebar or a full-width grid.
.card-wrapper {
container-type: inline-size;
}
@container (min-width: 28rem) {
.card {
grid-template-columns: 12rem 1fr;
}
}
Subgrid
Subgrid lets nested elements align to the parent grid’s tracks. Card titles, excerpts and footers finally line up across a row without fixed heights.
.grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
}
.card {
display: grid;
grid-row: span 3;
grid-template-rows: subgrid;
}
The :has() selector
:has() is the long-awaited parent selector. Style a form field’s wrapper when the input is invalid, or change a card’s layout when it contains an image:
.card:has(img) {
grid-template-rows: auto 1fr;
}
.field:has(input:user-invalid) {
--field-border: var(--color-danger);
}
Honourable mentions
aspect-ratiofor media boxes without padding hacks.gapon flexbox, at last.- Logical properties like
margin-inlinefor writing-mode-aware spacing. text-wrap: balancefor headlines that don’t leave a lonely last word.
Put these together and most layouts need no layout JavaScript at all.
