Sub topic 5
Margins: the space outside the grid
This chapter assumes you know basic CSS syntax, units such as px, rem, and %, and the box model (margin, padding, width). It also assumes you have met columns and gutters in the earlier chapters.
What margins are in a grid
Margins are the empty space between the outer edge of the grid and the edge of the viewport. Gutters separate columns from each other. Margins sit outside the first and last columns.
Margins keep text and images away from the screen edge. Without them, text touches the bezel on a phone and becomes hard to read. They also give a page a clear frame on wide screens.
Choosing margin values
Margins usually change with screen size. A small screen has little room to spare, so use a small margin. A large screen can afford more.
| Viewport | Typical side margin |
|---|---|
| Phone | 1rem (16px) |
| Tablet | 1.5rem to 2rem |
| Desktop | 2rem or more, or whatever space remains once the container hits its maximum width |
Use rem for margins so they scale if a user raises the browser's default font size. Keep the margin at least as wide as the gutter, so the page edge does not look tighter than the space between columns.
Two ways to create margins
There are two common approaches, and they behave differently.
- Padding on the container. The container gets
padding-inline. The grid sits inside that padding. - Margin on the container. The container gets
margin-inline: auto, which centers it and splits the free space equally on both sides.
In practice you use both together. Padding guarantees a minimum margin on small screens. Auto margins create extra space once the container reaches its maximum width.
<div class="page">
<div class="grid">
<main class="main">Main content</main>
<aside class="sidebar">Sidebar</aside>
</div>
</div>.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;
}On a 400px phone, the container is narrower than 72rem, so margin-inline: auto has no free space to share. The 1rem padding on each side becomes the page margin. On a 1600px monitor, the container stops at 72rem and the auto margins center it. The space on each side is the leftover width divided by two, plus the padding.
Note that max-width applies to the content box by default. Here, with padding-inline added, the container's total width is 72rem plus 2rem. If you want the padding included in the cap, set box-sizing: border-box.
Common errors
- Margin on the grid instead of the container. Putting
margin-inlineon.gridworks, but it mixes page framing with column layout. Keep margins on the outer element so the grid stays reusable. - Using
width: 100%with padding. Withoutbox-sizing: border-box, the element becomes wider than the viewport and causes horizontal scrolling. - No minimum margin. If you only use
margin-inline: auto, the content touches the screen edge below the maximum width. Always add padding. - Percentage margins that grow too large. A margin of
10%gives a good result on desktop. On a tablet it can waste space. Prefer fixedremvalues orclamp().
A fluid option uses clamp() to scale the margin between a minimum and maximum:
.page {
max-width: 72rem;
margin-inline: auto;
padding-inline: clamp(1rem, 4vw, 2rem);
}The padding is 4% of the viewport width, but never less than 1rem and never more than 2rem.
Exercise
A container has max-width: 60rem, margin-inline: auto, and padding-inline: 1rem, with box-sizing: border-box. The viewport is 1200px wide and 1rem is 16px. How wide is each side margin, measured from the screen edge to the start of the grid content?
Show answer
The container is 60rem = 960px wide, including padding. The free space is 1200 - 960 = 240px, so each auto margin is 120px. Add the 16px of padding, and the grid content starts 136px from the screen edge.