Sub topic 2
The parts of a grid
This chapter assumes you know basic HTML (elements and classes), basic CSS (selectors, properties, and the units px, rem, and %), and the box model (margin, padding, width). The previous chapter explained why pages use a column grid. This chapter names the parts of one. Later chapters cover each part in detail.
The three parts
A column grid has three parts, and each one controls a different kind of space.
| Part | What it is | Where it sits |
|---|---|---|
| Columns | The repeating vertical tracks that content is placed in | Inside the grid |
| Gutters | The empty space between two neighboring columns | Between columns only |
| Margins | The empty space between the grid and the edge of the screen | Outside the grid |
Columns hold content. Gutters keep neighboring content from touching. Margins keep the whole grid away from the screen edge.
Counting the gutters
A grid with N columns has N − 1 gutters. Gutters exist only between columns, so there is none at the outer edges. The space at the edges belongs to the margins.
This count is how you calculate a column's width. Take a content area 1200px wide, 12 columns, and 24px gutters:
- Total gutter space: 11 × 24px = 264px.
- Space left for columns: 1200px − 264px = 936px.
- Width of one column: 936px ÷ 12 = 78px.
If you change the gutter, every column width changes. The parts depend on each other. They are not independent settings.
The container
The container is the box that holds the grid. Its width limits the grid, and its side spacing creates the margins. Many designs also give the container a maximum width, so the grid stops growing on very wide screens. You will see the details in the chapter on containers.
Mapping the parts to CSS
Each part has a direct CSS counterpart. Here is the markup:
<div class="page">
<div class="grid">
<main class="main">Main content</main>
<aside class="sidebar">Sidebar</aside>
</div>
</div>And here are the styles, with a comment on each line that creates a part:
.page {
max-width: 72rem; /* container: caps the width */
margin-inline: auto; /* container: centers it */
padding-inline: 1rem; /* margins: space at the screen edges */
}
.grid {
display: grid;
grid-template-columns: repeat(12, 1fr); /* columns: twelve flexible tracks */
column-gap: 1.5rem; /* gutters: space between tracks */
}
.main {
grid-column: span 8;
}
.sidebar {
grid-column: span 4;
}The .main and .sidebar rules place content on the columns. Chapter 9 explains grid-column.
Common confusion
- Gutter and margin are different things. A gutter is inside the grid, between columns. A margin is outside it. Do not use the CSS
marginproperty to create gutters.column-gapkeeps the gutter size in one place, and it adds no space at the outer edges. - "Margin" in grid design is not always the CSS
marginproperty. Here it describes the space outside the grid, and you may build it with padding. - Content does not have to fill one column. An item can span several columns, and the gutters between the spanned columns become part of the item.
Exercise
A content area is 960px wide. It has 12 columns and 20px gutters. How many gutters are there, and how wide is each column?
Show answer
There are 11 gutters, because a grid has one fewer gutter than columns. Their total is 11 × 20px = 220px. The remaining 960px − 220px = 740px is shared by 12 columns, so each column is about 61.67px wide.