CSS Layout
Normal flow, positioning, Flexbox and Grid in depth, responsive design with media and container queries, stacking contexts and z-index, and common layout recipes.
Modern CSS layout rests on two systems: Flexbox for laying out items along one axis, and Grid for two-dimensional layouts. Both build on normal flow and positioning.
Normal flow
Without layout rules, block elements stack vertically, each taking the full width, and inline elements flow horizontally like words in a paragraph, wrapping at the edge. Most of a page should stay in normal flow; reach for flex/grid for components and page structure.
Positioning
position | Behaviour |
|---|---|
static | Default; in normal flow |
relative | In flow, but offset by top/left; becomes the containing block for absolute children |
absolute | Removed from flow; positioned relative to the nearest positioned ancestor |
fixed | Relative to the viewport; stays put on scroll |
sticky | Normal until a scroll threshold, then sticks (e.g. top: 0 headers) |
.card { position: relative; }
.card .badge { position: absolute; top: 8px; right: 8px; } /* corner badge */
.site-header { position: sticky; top: 0; z-index: 10; }
.modal-backdrop { position: fixed; inset: 0; } /* inset = top/right/bottom/left */Flexbox
A flex container lays out its children along a main axis (row or column), with the cross axis perpendicular.
.toolbar {
display: flex;
flex-direction: row; /* main axis: row (default) | column */
justify-content: space-between; /* distribute along the main axis */
align-items: center; /* align along the cross axis */
gap: 0.75rem; /* spacing between items */
flex-wrap: wrap; /* allow multiple lines */
}| Property | On | Values |
|---|---|---|
justify-content | Container | flex-start, center, flex-end, space-between, space-around, space-evenly |
align-items | Container | stretch (default), center, flex-start, flex-end, baseline |
flex-grow | Item | Share of extra space (0 = don't grow) |
flex-shrink | Item | Share of shrinking when space is short (0 = never shrink) |
flex-basis | Item | Starting size before growing/shrinking |
align-self, order | Item | Per-item alignment, visual order |
.item { flex: 1; } /* = 1 1 0% : grow and shrink equally from zero → equal widths */
.sidebar { flex: 0 0 250px; }/* fixed 250px, never grows or shrinks */
.main { flex: 1; min-width: 0; } /* min-width: 0 lets long content shrink instead of overflowing */Grid
Grid defines rows and columns on the container and places items into cells.
.page {
display: grid;
grid-template-columns: 240px 1fr 300px; /* sidebar, content, aside */
grid-template-rows: auto 1fr auto; /* header, body, footer */
grid-template-areas:
"header header header"
"nav main aside"
"footer footer footer";
gap: 1rem;
min-height: 100dvh;
}
header { grid-area: header; }
nav { grid-area: nav; }
main { grid-area: main; }
aside { grid-area: aside; }
footer { grid-area: footer; }| Feature | Example |
|---|---|
fr unit | 1fr 2fr — fractions of free space |
repeat() | repeat(3, 1fr) |
minmax() | minmax(200px, 1fr) — at least 200px, at most a fraction |
| Line placement | grid-column: 1 / -1 (span full width), grid-row: span 2 |
auto-fill / auto-fit | As many tracks as fit |
subgrid | Children align to the parent grid's tracks |
The responsive card grid — no media queries
.cards {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(min(250px, 100%), 1fr));
gap: 1.5rem;
}Columns are at least 250px; as many as fit; they stretch to fill the row. One line replaces several breakpoints.
Flexbox or Grid?
| Use Flexbox when | Use Grid when |
|---|---|
| Content decides sizes (a toolbar, tags, a nav bar) | The layout decides sizes (a page shell, a dashboard) |
| One dimension: a row or a column | Two dimensions: rows and columns align |
| Items should wrap naturally | Items must line up across rows |
They combine freely: a grid page whose cells contain flex components.
Centering (finally easy)
.center-grid { display: grid; place-items: center; }
.center-flex { display: flex; justify-content: center; align-items: center; }
.center-block { max-width: 65ch; margin-inline: auto; } /* horizontally centre a block */Responsive design
Design mobile-first: base styles for small screens, then enhance at larger widths.
.layout { display: grid; gap: 1rem; }
@media (min-width: 768px) {
.layout { grid-template-columns: 240px 1fr; }
}
/* Container queries: respond to the component's space, not the viewport */
.card-wrapper { container-type: inline-size; }
@container (min-width: 420px) {
.card { display: grid; grid-template-columns: 120px 1fr; }
}
@media (hover: hover) { .btn:hover { … } } /* only on devices that can hover */
@media (prefers-color-scheme: dark) { … }Container queries make components truly reusable: the same card adapts whether it's in a narrow sidebar or a wide main column.
Stacking and z-index
z-index orders overlapping positioned elements — but only within the same stacking context. A new stacking context is created by position + z-index, opacity < 1, transform, filter, isolation: isolate, and more. A child with z-index: 9999 can't escape a parent context with z-index: 1 — the source of most "z-index doesn't work" bugs.
.card { isolation: isolate; } /* contain children's z-indexes inside this component */Keep a small scale (--z-dropdown: 10; --z-modal: 100; --z-toast: 1000;), and render modals at the end of the body (or use the native <dialog> element, which uses the top layer).
- Normal flow first; positioning (relative, absolute, fixed, sticky) for overlays and pinned elements.
- Flexbox: one axis, content-driven; master justify-content, align-items, flex grow/shrink/basis and min-width: 0.
- Grid: two dimensions; fr, minmax, repeat(auto-fill), template areas.
- Mobile-first media queries; container queries for component-level responsiveness.
- z-index only works within a stacking context; isolate components and keep a z-index scale.