Sub topic 5
Margins: the space outside the grid
This chapter assumes you know basic HTML and CSS syntax, the units px, rem, and %, and the box model (margin, padding, width). It also builds on the previous chapters. Columns divide the width into tracks, and gutters are the gaps between those tracks.
What grid margins are
Margins are the empty space between the outer edge of the grid and the edge of the viewport (the visible browser window). They sit outside the first and last columns. Gutters sit between columns. The two are separate settings, and the grid should let you change each on its own.
Margins do three jobs:
- They keep text and controls away from the physical edge of the screen, where they are hard to read and hard to tap.
- They give the page a visible frame, so content does not look cramped against the browser window.
- On wide screens they absorb leftover space, so lines of text do not stretch too long.
The last job overlaps with the container's maximum width, which the next chapter covers. This chapter focuses on the minimum margin, the space that is always present, even on a phone.
Choosing margin values
A margin should be visibly at least as large as the gutter, and it is often larger. If the outer margin is smaller than the gutter, the page looks as if it is spilling off the edge. A common starting point is to make the margin equal to the gutter on small screens and larger on big ones.
| Viewport | Typical margin | Typical gutter | Reasoning |
|---|---|---|---|
| Under 40em (phones) | 1rem | 1rem | Space is scarce, so keep both small. |
| 40em to 64em (tablets) | 1.5rem to 2rem | 1.5rem | Room for a frame without wasting width. |
| Over 64em (desktops) | 2rem to 4rem, or automatic | 1.5rem to 2rem | Spare width goes to the margin. |
These numbers are starting points, not rules. Test them with your real content. Dense data tables may need smaller margins, and an editorial layout may use more generous ones.
Use rem for fixed steps so margins grow with the user's font-size setting. Use vw (a percentage of viewport width) only inside clamp(), so the value cannot become too small or too large.
Implementing margins in CSS
You can create outer margins with margin or with padding on a wrapper element. For a grid container, padding is usually the better choice, for these reasons:
- Padding stays inside the element's box, so the element's background still covers the edge area. A colored band behind the page then reaches the screen edge.
- Padding does not collapse. Vertical margins between block elements can merge, which causes surprises, but padding never does.
- Padding and
max-widthcombine predictably whenbox-sizing: border-boxis set.
Here is the markup, reused from the grid example:
<div class="page">
<div class="grid">
<main class="main">Main content</main>
<aside class="sidebar">Sidebar</aside>
</div>
</div>And the CSS that makes .page supply the margins:
*,
*::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;
}Here padding-inline: 1rem is the minimum margin. The margin-inline: auto line only has an effect when the viewport is wider than 72rem, and it then splits the extra space evenly on both sides.
The box-sizing rule matters. With content-box (the browser default), max-width: 72rem would apply to the content only, and the padding would be added outside it, making the element 74rem wide. With border-box, the 72rem includes the padding, so the grid's content area is 70rem wide. Know which one you have before you calculate column widths by hand.
Logical properties
padding-inline and margin-inline are logical properties. They refer to the start and end of the text direction, not to left and right. In a right-to-left language such as Arabic they still apply to both sides, and if you set only one side with padding-inline-start, it moves to the correct side automatically. Prefer them over padding-left and padding-right for new work.
Margins that scale with the viewport
A fixed 1rem margin is too tight on a tablet. One way to fix that is to change the value at breakpoints. A smoother way is clamp(), which takes a minimum, a preferred value, and a maximum.
.page {
max-width: 80rem;
margin-inline: auto;
padding-inline: clamp(1rem, 4vw, 3rem);
}At a 320px-wide viewport, 4vw is 12.8px, which is below the 1rem minimum, so the margin is 1rem (16px). At 800px, 4vw is 32px, so the margin grows with the screen. At 1440px, 4vw would be 57.6px, but the maximum of 3rem (48px) caps it. The margin is fluid between the limits and fixed outside them.
Some teams keep margins at a few fixed steps instead, because it makes the page easier to reason about and to match to a design file. Both approaches work. Pick one and apply it everywhere, because mixed approaches cause misaligned edges between sections.
Full-width backgrounds with a contained grid
Often a design needs a background that reaches the screen edge, such as a hero band, while the text still follows the margins. Do not put the margins on the outermost element in that case. Put the background on a full-width wrapper and the margin-and-container rules on an inner element:
<header class="hero">
<div class="page">
<h1>Welcome</h1>
</div>
</header>.hero {
background: #12355b;
color: white;
}The .hero element spans the whole viewport, and the .page element inside it keeps the same margins as the rest of the site, so the heading lines up with the content below it.
Edge cases
Notches and safe areas
Some phones have a camera cutout or rounded corners. If the page opts into the full screen with viewport-fit=cover in the viewport meta tag, content can be hidden behind them. The env() function reads the safe area sizes, and max() keeps your normal margin as a floor:
.page {
padding-inline-start: max(1rem, env(safe-area-inset-left));
padding-inline-end: max(1rem, env(safe-area-inset-right));
}If you do not set viewport-fit=cover, the browser already keeps content inside the safe area, and the insets are zero.
The scrollbar problem
Do not size a full-width element with width: 100vw. On systems where the scrollbar takes up space (most desktop browsers), 100vw includes the scrollbar width, so the element is wider than the visible area and causes a horizontal scrollbar. Use width: 100% or let block elements fill their parent.
Percentage padding
A percentage value in padding-inline is calculated from the width of the containing block, not from the element itself. That is predictable at the top level, but inside nested elements the margin changes with the parent's width. Use rem or clamp() for margins, and keep percentages for other jobs.
Doubled margins
If a nested element also has its own padding-inline, the margins add up, and the nested content ends up inset from the page grid. Apply the outer margin once, at the container. Inner sections should have no horizontal padding unless you intend an indent.
Common errors
- Margin on the grid, not the container. Adding
margin-inlineto.gridwhile.pagehas padding gives two layers of space, and the total is hard to predict. - Forgetting
box-sizing: border-box, which makes the container wider than itsmax-widthsuggests and can cause horizontal overflow on small screens. - Setting
margin-inline: autoon an element that haswidth: 100%. The auto margins have no space to share, so nothing is centered. Usemax-widthinstead of a fixed width. - Using a margin of 0 on phones. Text then touches the screen edge, which hurts readability and makes edge taps unreliable.
- Using different margins for header, content, and footer, so their left edges do not line up.
Exercise
A page has .page { max-width: 60rem; margin-inline: auto; padding-inline: clamp(1rem, 5vw, 2.5rem); } with box-sizing: border-box. The viewport is 1000px wide and 1rem is 16px. How wide is each side margin, and how wide is the grid's content area?
Show answer
5vw of 1000px is 50px. The clamp limits are 16px and 40px (2.5rem), so the value is capped at 40px. The container's max-width is 60rem, which is 960px, and the viewport (1000px) is wider, so the container is 960px wide and centered. The auto margins add 20px of space on each side outside the container, and each padding is 40px inside it. The side margin measured from the container edge is therefore 40px, and from the viewport edge it is 60px. The grid's content area is 960px minus 80px, which is 880px.
Exercise
Why is padding usually better than margin for the outer spacing of a grid container that has a background color?
Show answer
Padding stays inside the element's box, so the background color extends to the container's outer edge and the space does not collapse with neighboring margins. Margin is outside the box, so the background would stop short of it.