---
name: css-layout
description: Grid vs flex, eight layout primitives, spacing that scales, and stopping horizontal overflow at 320px
when: Laying out a page, aligning a row of cards, or when a layout overflows or scrolls sideways on mobile
---
# CSS layout
A page is not a set of breakpoints. It is a handful of layout *shapes* nested
inside each other, each deciding its behaviour from the space it is given — so
it is responsive with almost no `@media`, and survives being dropped into a
sidebar, a modal or a half-width column, which a breakpoint layout does not.
## Grid or flex — the question answers itself
- **Flex** = one dimension. A row of buttons, a nav bar, a card's inner stack.
- **Grid** = two dimensions, or *"the container decides the shape"* — page
layouts, galleries, anything with rows AND columns.
Writing `flex-wrap` plus width percentages plus negative margins for a gallery
means you wanted grid:
```css
.gallery { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 1.5rem; }
```
⭐ A responsive gallery with **no media queries at all**.
## Use `gap`, not margins between children
`gap` puts space *between* items, never before the first or after the last.
`margin-right` on every child plus `:last-child { margin-right: 0 }` is the old
workaround and it is still wrong when the row wraps — the last item *of each
line* keeps a trailing margin.
⭐ **Vertical margins between siblings collapse; `gap` does not.** Set 24px and
32px on adjacent elements and you measure 32px; flex or grid with `gap` gives
what you expected.
---
# The eight primitives
> **Provenance.** These shapes and their names (stack, cluster, sidebar,
> switcher, cover, reel, frame, centre) are common CSS-community vocabulary
> popularised by *Every Layout*. That book's text is copyrighted and none is
> reproduced here — the CSS below is written from the box-model behaviour it
> relies on, which is spec, not authorship.
⚠️ `acuvo-ui.css` already ships stack (`.stack`), cluster (`.row`), `.grid`,
`.sidebar` and `.switcher`. Reference it rather than pasting them.
## 1. Stack — vertical rhythm, owned by the parent
```css
.stack > * + * { margin-top: var(--gap, 1rem); }
```
The `* + *` is the point: **no margin before the first child and none after the
last**, so a stack never adds space at its container's edge. Nested stacks each
keep their own `--gap`, so a `
` of stacks
gives two levels of rhythm from one rule.
⚠️ **Do not put `margin-bottom` on a component.** A component carrying its own
outside space cannot be reused where the spacing differs. Space belongs to the *arrangement*, not the
thing arranged.
## 2. Cluster — things in a line that wrap gracefully
```css
.cluster { display: flex; flex-wrap: wrap; gap: var(--gap, .75rem); align-items: center; }
```
Tag lists, button rows, nav links, meta lines, filter chips.
## 3. Sidebar — two columns, then stacked, with no breakpoint
```css
.sidebar { display: flex; flex-wrap: wrap; gap: var(--gap, 1.5rem); }
.sidebar > :first-child { flex-basis: var(--side, 18rem); flex-grow: 1; }
.sidebar > :last-child { flex-basis: 0; flex-grow: 999; min-inline-size: var(--main-min, 55%); }
```
**How it decides.** `flex-grow: 999` makes the main column eat every spare pixel,
so the sidebar sits at its `flex-basis`. `min-inline-size: 55%` says main may
never be narrower than 55% of the line — the instant that cannot be honoured
alongside the sidebar, `flex-wrap` puts them on separate lines.
⭐ **The switch point is a property of the CONTENT, not the viewport** — which is
why this works inside a card and a media query does not.
## 4. Switcher — N columns or N rows, never a ragged half-state
```css
.switcher { display: flex; flex-wrap: wrap; gap: var(--gap, 1.25rem); }
.switcher > * { flex-grow: 1; flex-basis: calc((var(--threshold, 32rem) - 100%) * 999); }
```
**Why that works — it looks like a magic number and is not.** `100%` resolves
against the container's width. Container **narrower** than `--threshold` →
bracket positive → ×999 is a huge basis → each item demands more than the whole
line → one item per row. Container **wider** → bracket negative → basis clamps to
`0` → `flex-grow: 1` shares the line → one row of equal columns.
⭐ **Prefer this to `grid auto-fit` for a 3-up feature row: it is all-or-nothing.**
`auto-fit` happily leaves 2 on one line and 1 orphan below, which reads as broken;
a switcher goes 3-across to 3-stacked.
## 5. Cover — a hero centred, never shorter than the fold
```css
.cover { display: flex; flex-direction: column; min-block-size: 100svh; padding: 1.5rem; }
.cover > * { margin-block: auto; }
.cover > :first-child:not(.centred) { margin-block-start: 0; }
.cover > :last-child:not(.centred) { margin-block-end: 0; }
```
`auto` margins in a flex container absorb free space, so one each side of the
middle child centres it while pinning a header top and a footer bottom — no
absolute positioning, no `translate(-50%)`.
⚠️⚠️ **`100vh` is wrong on mobile — this is the bug behind "the button is under
the browser bar".** `vh` is sized to the *largest* viewport, ignoring the address
bar actually on screen. Use `svh` (always-visible) or `dvh` (resizes as the bar
hides), widely available since December 2022. And prefer `min-block-size` to
`height`: a fixed-height hero clips content at larger font settings.
## 6. Reel — a horizontal scroller that does not break the page
```css
.reel { display: flex; gap: 1rem; overflow-x: auto; overscroll-behavior-x: contain;
scroll-snap-type: x mandatory; padding-bottom: 1rem; }
.reel > * { flex: 0 0 var(--item, 18rem); scroll-snap-align: start; }
```
⭐ **`overscroll-behavior-x: contain` stops the browser's back-swipe** when someone
flicks past the end — without it, scrolling a carousel navigates away.
⚠️ `scroll-snap-type` does nothing without `scroll-snap-align` on the children.
⚠️ **`overflow-x: auto` implies `overflow-y: auto`** — a dropdown inside a reel is
clipped. Use the top layer (`popover` / `