Sub topic 6
Place sections on a grid with a spacing scale
This chapter assumes you have a low-fidelity wireframe with ranked sections, and that you can read basic CSS. You will turn that wireframe into a grid with a fixed set of spacing values. The same decisions work in a design tool and in code.
Why a grid and a scale
A wireframe shows what goes where, but only roughly. Without a grid, each section ends up with its own width and its own gaps. The page then looks uneven, even if each section is fine on its own.
Two rules fix this:
- A layout grid is a set of columns, gutters, and margins. Sections snap to the columns.
- A spacing scale is a short list of allowed distances, such as 8, 16, 24, and 40 pixels. Every gap and padding value comes from that list.
Choose the grid
Start with the widest layout you will design, usually desktop. A common choice is 12 columns, because 12 divides evenly into 2, 3, 4, and 6. Set these values:
| Part | Typical desktop value | Role |
|---|---|---|
| Columns | 12 | Units that sections span |
| Gutter | 24 px | Gap between columns |
| Margin | 64 px or more | Space between the grid and the screen edge |
| Max width | 1200 px | Stops lines from becoming too long |
Then place each wireframe section by column span. The hero text might span 6 columns. Three feature cards span 4 columns each. A full-width proof strip spans all 12.
Define the spacing scale
Pick a base unit and build the scale from multiples of it. With a base of 8 px, you get 8, 16, 24, and 40 px. These match the tokens --space-1 to --space-4 in the rem values shown below.
Use the scale by relationship, not by feel:
- Small steps for items inside a component, such as a heading and its paragraph.
- Medium steps between related components, such as cards in a row.
- Large steps between sections.
Larger gaps between sections than inside them tell the eye which things belong together.
Express it in CSS
The grid and the scale translate directly into code. This example defines the scale and a 12-column container.
:root {
--space-1: 0.5rem;
--space-2: 1rem;
--space-3: 1.5rem;
--space-4: 2.5rem;
}
.container {
display: grid;
grid-template-columns: repeat(12, 1fr);
column-gap: var(--space-3);
max-width: 75rem;
margin: 0 auto;
padding: 0 var(--space-2);
}
.hero-text {
grid-column: 1 / span 6;
padding-block: var(--space-4);
}
.features-heading {
grid-column: 1 / -1;
margin-bottom: var(--space-3);
}
.feature-card {
grid-column: span 4;
}grid-column: span 4 makes each card cover four of the twelve columns, so three cards fill a row. The negative index -1 means "to the last line", so 1 / -1 spans the full width.
Common errors
- Off-scale values. A stray
margin: 13pxbreaks the rhythm. Search the file for values that are not tokens. - Doubled gaps. If cards have their own margins and the grid also has a gap, the spacing is counted twice. Let the grid own the gap.
- Nested grids that drift. A grid inside a card needs its own gutter, but it should use the same scale.
- Content that ignores the grid. A section that spans 11 columns for no reason looks like a mistake. Span a count you can justify.
Exercise
A hero has 12 columns, a 24 px gutter, and two blocks: text and an image. The text should be wider than the image, and both should end at the grid's edges. Give one valid column split, and say which spacing step you would use between the hero and the features section.
Show answer
Text spans 7 columns and the image spans 5 (any split that sums to 12 with text larger, such as 7/5 or 8/4, is valid). The gutter provides the gap between them. Between sections use the largest step in the scale, such as --space-4 (2.5rem), because it is bigger than the gaps inside components.