Short answer
A layout holds together when width, columns, gutter, and the heading share one grid. Set a container width, define the column tracks, and put the space between them in the CSS gap property. Place the heading on that grid, then check ordinary text against the WCAG 4.5:1 contrast floor.
Last verified against the sources listed below.
Key takeaways
- MDN: display: grid makes each direct child a grid item.
- The fr unit is a share of free space in the grid container.
- W3C: gutters sit between tracks, not before the first or after the last.
- MDN: one gap value sets both row-gap and column-gap. Two values set row, then column.
- WCAG 1.4.3 asks for 4.5:1 contrast, or 3:1 for large text.
Learning how to make layout design starts with a container, column tracks, and a gutter you can name in CSS.
Web design covers the whole page. This guide is the grid part: width, columns, the gap between them, the heading, and contrast.
Elements of design still apply. Alignment and space are doing the holding, not a stock margin size.
What do you need before you start?
You need a container, a column plan, and a contrast check.
MDN’s grid guide says a grid container is any element with display: grid or display: inline-grid. The direct children become grid items. You do not need a framework for that.
Flexbox is the other layout tool on the same site. MDN calls flexbox one-dimensional: a row or a column. Grid is two-dimensional, so columns and rows are set together.
Use flex inside a cell. Use grid for the page frame.
How do you set the page width?
Set a max-width on the container so the measure stays readable.
The grid specification does not name a universal page width or a required outer margin. Size the container yourself, then let the tracks divide that width. A fixed pixel width on the whole page is a choice, not a spec rule.
Keep the container as the only place you set that limit. If every card also has its own max-width, the columns stop sharing a common edge.
How do you choose columns?
Choose tracks with grid-template-columns, and use fr when columns should share space.
MDN says grid-template-columns and grid-template-rows define tracks. A track is the space between two adjacent grid lines. The example 1fr 1fr 1fr is three equal shares of free space.
repeat(3, 1fr) is the same list written shorter.
Tracks you name are the explicit grid. If content needs another row, the browser adds an implicit track. Those implicit tracks are auto-sized unless you set grid-auto-rows or grid-auto-columns.
A CSS Grid Generator can sketch the tracks. The CSS you keep should still say the same thing the spec says: track sizes, then gap.
How do you set the gutter?
Set gap on the grid container so the space sits between tracks.
The W3C grid spec says row-gap, column-gap, and the gap shorthand define gutters. For track sizing, each gutter is treated like an extra empty fixed-size track. Gutters only appear between tracks.
There is none before the first track or after the last.
MDN says one gap value sets both directions. Two values set row-gap first, then column-gap. The initial value normal is 0 on a grid, and 1em in a multi-column layout.
The older name grid-gap is an alias browsers still accept.
The CSS Box Alignment draft’s gap section now points readers to the CSS Gaps module. The behavior you rely on is the one the grid spec and MDN still describe: space between tracks, not an outer margin.
Where should the heading sit?
Place the heading on the grid with line numbers, a name, or a grid area.
MDN lists those three placement methods. A heading that should span the title band needs an explicit placement. If you leave it automatic, it occupies the next cell and the title column looks accidental.
Put the heading’s text in the document order you want a screen reader to meet. Placement changes the visual cell. It should not be the only thing that says what the page is about.
How do you check contrast?
Check ordinary text at 4.5:1, and large text at 3:1, under WCAG 1.4.3.
The W3C understanding document for success criterion 1.4.3 sets those floors. It treats 18 point text, or 14 point bold, as large-scale, and it says not to round a computed ratio up into a pass. Images of text are covered too.
Contrast is separate from elevation. Material Design 3 defines elevation as distance on the z-axis, drawn with tone or shadow. A shadow does not fix a pale heading.
Measure the text color against the surface behind it.
What should you fix first?
Fix track overflow, stray outer space, and low-contrast type before you decorate.
| What you see | What the spec points to | What to change |
|---|---|---|
| Space before the first column | Gutters are only between tracks | Look at margin and padding, not gap |
| A gap larger than you set | Alignment can add space between tracks | Check justify-content and align-content |
| Heading stuck in one cell | Auto placement fills the next cell | Place it with lines, a name, or an area |
| A row that will not shrink | A fixed length track ignores free space | Give the flexible tracks an fr size |
| Pale body text | 4.5:1 is the ordinary-text floor | Darken the text or lighten the surface |
| Columns that only stack | Flex is one axis unless you switch to grid | Use grid when rows and columns both matter |
Work in this order:
- Give the page one container and set its max-width.
- Turn that container into a grid and write the column tracks.
- Set
gapfor the gutter, and keep outer space on the container’s padding. - Place the heading so it spans the tracks that form the title.
- Check the contrast ratio against WCAG 1.4.3 and adjust the colors.
Then read the page at a narrow width and a wide width. The tracks should still share one edge. If they do, the layout is holding together.
What should you recheck after the grid is in?
Recheck the heading span, the gap, and the contrast after any content change.
A new card can push an implicit row into existence. MDN says those rows are auto-sized unless you set grid-auto-rows. Look at that row before you add another fixed height.
If the heading no longer spans the title band, place it again with line numbers, a name, or an area. Gap will not do that job. It only separates tracks.
Frequently asked questions
Does gap add space before the first column?
No. The CSS grid specification says gutters appear only between tracks of the grid. There is no gutter before the first track or after the last track. If you see a band of space at the outer edge, that space is coming from margin or padding on the container or the item, not from gap. MDN also notes that justify-content and align-content can add space between tracks on top of the gap value, so the visible separation can be larger than the gap you typed.
Is flexbox enough for a full page layout?
Flexbox is a one-dimensional model. MDN says it lays items out as a row or as a column, while grid controls columns and rows together. A nav row or a button group is a natural flex job. A page with a heading, a main column, and a side column is a grid job, because those regions have to line up in two directions. You can nest a flex row inside a grid cell when a single band only needs one axis.
What does the fr unit mean?
MDN defines fr as a fraction of the free space in the grid container. Three tracks written as 1fr 1fr 1fr share that space equally. A track written as 2fr beside two 1fr tracks takes two shares out of four. If you mix a fixed length with fr, the fixed length is taken out first and the fractions split what remains. fr does not invent a page margin. It only divides space inside the container you already sized.
Which contrast ratio applies to a heading?
WCAG success criterion 1.4.3 asks for a contrast ratio of at least 4.5:1 for text and images of text. Large-scale text can go as low as 3:1. The understanding document treats 18 point text, or 14 point bold text, as large enough for that lower bar, with a caution about very thin fonts. The ratios are thresholds. A computed 4.499:1 does not meet 4.5:1, because the document says not to round up.
Should I set a universal page margin in pixels?
No single pixel margin is required by the CSS grid spec or by WCAG 1.4.3. Set a max-width on the container so the line length fits the content, then let gap handle the space between columns. Outer space is padding or margin on that container. Pick those lengths by looking at the page, not by copying a stock number. The spec cares that tracks and gutters are defined, not that the margin is a particular constant.
Sources
- Basic concepts of grid layout · MDN, 2026
- Basic concepts of flexbox · MDN, 2026
- gap · MDN, 2026
- CSS Grid Layout Module Level 1, gutters · W3C, 2026
- Understanding SC 1.4.3 Contrast (Minimum) · W3C Web Accessibility Initiative, 2026
- CSS Box Alignment Module Level 3, gaps · CSS Working Group, 2026
- Elevation · Material Design, 2026