Sub topic 2
The parts of a grid
A column grid has three adjustable parts: columns, gutters, and margins. A fourth piece, the container, decides how wide the whole arrangement can get. This chapter assumes you know basic HTML (elements and classes), basic CSS syntax and units (px, rem, %), and the box model (margin, padding, width). Later chapters cover each part in detail. Here you learn how the parts fit together and how they map to CSS.
The four pieces
- Columns are the vertical tracks that hold content. Items start and end on column edges.
- Gutters are the fixed gaps between adjacent columns. They keep neighboring items from touching.
- Margins are the space between the outer edge of the grid and the edge of the viewport (the visible area of the browser window).
- The container is the box that holds the grid. It limits the grid's maximum width and centers it.
Margins and gutters are easy to confuse because both are empty space. The difference is position. A gutter sits between two columns. A margin sits outside the first and last columns. There is one fewer gutter than there are columns, and exactly two margins, one on each side.
A picture in numbers
Take a 12-column grid with these values:
| Part | Value |
|---|---|
| Container maximum width | 72rem |
| Margins | 1rem on each side |
| Gutters | 1.5rem, 11 of them |
| Columns | 12, equal width |
The container uses box-sizing: border-box, so its 72rem includes the margins. The usable width is 72rem − 2rem = 70rem. The 11 gutters take 11 × 1.5rem = 16.5rem. That leaves 53.5rem for columns, so each column is 53.5 ÷ 12 ≈ 4.46rem wide.
You rarely compute this by hand, because 1fr does it for you. The arithmetic still matters when you debug. It tells you why an item that spans four columns is wider than four column widths: it also contains three gutters.
A span of n columns has width n × column width + (n − 1) × gutter. In this grid:
- A span of 8 is 8 × 4.46 + 7 × 1.5 ≈ 46.17rem.
- A span of 4 is 4 × 4.46 + 3 × 1.5 ≈ 22.33rem.
- The two spans plus the gutter between them (1.5rem) add up to 70rem, the full usable width.
Mapping the parts to CSS
Each part has a direct CSS counterpart.
| Grid part | CSS mechanism |
|---|---|
| Columns | `grid-template-columns: repeat(12, 1fr)` |
| Gutters | `column-gap` (or `gap` for both axes) |
| Margins | `padding-inline` on the container, or `margin-inline` |
| Container | `max-width` plus `margin-inline: auto` |
This is the markup from the running example.
<div class="page">
<div class="grid">
<main class="main">Main content</main>
<aside class="sidebar">Sidebar</aside>
</div>
</div>And this is the CSS that produces the numbers above.
*,
*::before,
*::after {
box-sizing: border-box;
}
.page {
max-width: 72rem;
margin-inline: auto;
padding-inline: 1rem;
}
.grid {
display: grid;
grid-template-columns: repeat(12, 1fr);
column-gap: 1.5rem;
}
.main {
grid-column: span 8;
}
.sidebar {
grid-column: span 4;
}The box-sizing rule at the top is what makes max-width: 72rem include the padding. Without it, the content box would be 72rem and the padding would be added outside it, giving a 74rem box. Many sites set this reset globally, but a standalone example needs it to match the arithmetic.
Two details in this code are worth noting:
- The gutters belong to
.grid, not to the items. If you put margins on items instead, the first and last items get extra space at the outer edges, and the grid no longer lines up with the container. - The margins belong to
.page, not to.grid. On a narrow screen the container is narrower than 72rem, so the padding is the only thing that keeps text off the screen edge. On a wide screen the auto margins take over and center the grid, and the padding stays as a small buffer.
Seeing the grid
When a layout looks wrong, draw the columns. A temporary overlay shows where the tracks and gutters are. It reuses the same grid rules, so it stays accurate.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Grid overlay</title>
<style>
*, *::before, *::after { box-sizing: border-box; }
.page {
position: relative;
max-width: 72rem;
margin-inline: auto;
padding-inline: 1rem;
}
.grid {
display: grid;
grid-template-columns: repeat(12, 1fr);
column-gap: 1.5rem;
}
.main { grid-column: span 8; background: #e8f0fe; padding: 1rem; }
.sidebar { grid-column: span 4; background: #fce8e6; padding: 1rem; }
.overlay {
position: absolute;
inset-block: 0;
inset-inline: 1rem;
pointer-events: none;
}
.overlay span {
background: rgb(0 120 255 / 0.12);
}
</style>
</head>
<body>
<div class="page">
<div class="grid overlay" aria-hidden="true">
<span></span><span></span><span></span><span></span>
<span></span><span></span><span></span><span></span>
<span></span><span></span><span></span><span></span>
</div>
<div class="grid">
<main class="main">Main content</main>
<aside class="sidebar">Sidebar</aside>
</div>
</div>
</body>
</html>The overlay is absolutely positioned inside .page. Its inset-inline: 1rem matches the container's padding, so the twelve shaded stripes sit exactly where the real columns are. The white strips between them are the gutters. Remove the overlay markup before shipping. Browser developer tools can also draw grid lines for you. In Chrome and Firefox, the Elements or Inspector panel has a "grid" badge next to any element with display: grid.
How the parts interact
The parts are not independent. Changing one changes the space available to the others.
- Wider gutters shrink columns. With a fixed container, adding 0.5rem to each of the 11 gutters removes 5.5rem from the column total, and each column loses about 0.46rem.
- Larger margins shrink everything. Margins reduce the usable width before gutters are subtracted.
- More columns do not change the total width. Going from 12 to 16 columns makes each column narrower, and it adds gutters, so the total gutter space grows.
- The container sets the ceiling. Below the maximum width the grid is fluid and the columns stretch with the viewport. Above it the grid stops growing and the margins grow instead.
The last point is why the container is part of the anatomy. A grid with no maximum width keeps widening on large monitors until lines of text become hard to read.
Common errors
These mistakes come from misreading the anatomy.
- Putting margins on items to make gutters. The result is uneven outer edges and extra work in every breakpoint. Use
gapon the grid. - Forgetting
box-sizing: border-box. The container ends up wider than intended, and on narrow screens it can overflow horizontally. - Using
margin: 0 autowithout amax-widthorwidth. A block element already fills its parent, so there is nothing to center. The grid stays full width. - Treating the number of gutters as equal to the number of columns. The count is columns − 1, which matters when you compute spans by hand.
- Setting
padding-inlineon the grid element and the container. The margins double, and the overlay no longer lines up.
Choosing starting values
These are reasonable defaults when you have no design file to follow. Adjust them to match your content.
- Columns: 12 on wide screens, because 12 divides evenly into 2, 3, 4, and 6. Use 4 on phones.
- Gutters: 1rem to 1.5rem. Use the same gutter horizontally and vertically unless you have a reason not to.
- Margins: 1rem on small screens, growing to 2rem or more on large ones, or let auto margins absorb the extra width.
- Container maximum: 60rem to 80rem, so that body text stays at a comfortable line length.
Using rem for gutters and margins ties them to the user's font size setting. Someone who increases their default text size gets proportionally larger spacing, which keeps the layout balanced.
Exercise
A container is 60rem wide, uses border-box sizing, and has 2rem of padding on each side. Its grid has 8 columns and a 1rem gutter. How wide is one column, and how wide is an item that spans 3 columns?
Show answer
The usable width is 60 − 4 = 56rem. There are 7 gutters, which total 7rem, so the columns share 49rem and each is 49 ÷ 8 = 6.125rem. A 3-column span includes 2 gutters: 3 × 6.125 + 2 × 1 = 20.375rem.
Exercise
Your layout has the right gutters inside the grid but text touches the screen edge on a phone. Which part of the anatomy is missing, and which CSS property on which element fixes it?
Show answer
The margins are missing. Add padding-inline: 1rem (or similar) to the container, .page in this chapter's example. Gutters only separate columns from each other. They do not protect the outer edges.