Lesson 15 of 23
In progress
Spacing as a system
Replace arbitrary margins with a small spacing scale, use gap for layout, and keep vertical rhythm consistent across sections.
Spacing is the difference between a page that feels designed and one that feels assembled. The fix is not more values; it is fewer, used consistently.
What you will learn
- Build a spacing scale with custom properties.
- Choose between margin, padding and
gap. - Keep a consistent vertical rhythm without magic numbers.
A small scale beats many values
Pick a base unit, usually 4 or 8 pixels, and derive a handful of steps: 4, 8, 12, 16, 24, 32, 48, 64. Name them by step, not by pixel value, so a later adjustment changes everything at once.
Editable. Runs in your browser.
.stack > * + * { margin-block-start: var(–space-4); }
.card { padding: var(–space-6); border: 1px solid #d9d9e3; }
</style>
<div class="card">
<h2>Consistent spacing</h2>
<p>Every gap in this card comes from the same scale.</p>
<p>Which is why the rhythm feels calm rather than accidental.</p>
</div>
Press Run to see the output.
The lobotomised owl selector, .stack > * + *, adds space between children but not before the first or after the last. It is a compact way to stack content without wrapping every child in its own margin. The first item stays flush with the container edge, which is usually what you want inside a card, panel, section or wrapper element.
Margin or padding?
Padding is inside the element and shares its background. Margin is outside and always transparent.
Use padding when the space is part of the component: a button’s breathing room, a card’s interior. Use margin when separating components from each other. If a background colour should not extend into the space, it is margin. If a border should enclose the space, it is padding.
Editable. Runs in your browser.
<p>Text before the notice.</p>
<div class="notice">Inside the notice, the padding creates the tinted area.</div>
<p>Text after it, separated by the margin.</p>
Press Run to see the output.
gap is the layout tool
For Flexbox and Grid, gap spaces items without margins on the children. It does not add space at the edges, and it does not collapse.
Editable. Runs in your browser.
<div class="row">
<span class="chip">HTML</span>
<span class="chip">CSS</span>
<span class="chip">Accessibility</span>
</div>
Press Run to see the output.
With margins you would need .chip + .chip { margin-left: 12px; } and separate rules for wrapped rows. gap handles both directions and stays consistent when items wrap.
Vertical rhythm across sections
A page reads better when section spacing is consistent. One approach is a single --section-space token used by every section, adjusted once per breakpoint.
Editable. Runs in your browser.
<section><h2>First section</h2><p>Content.</p></section>
<section><h2>Second section</h2><p>More content.</p></section>
Press Run to see the output.
padding-block gives the section its own breathing room, and clamp() lets it grow with the viewport without a media query. The border between sections is a divider, not a spacing hack.
Containers and readable widths
A container caps the content width and centres it. Combining a max width with fluid padding keeps text readable on large screens and usable on small ones.
.container {
width: min(100% - 2rem, 72rem);
margin-inline: auto;
}
min() picks the smaller of the two, so the container never touches the viewport edge. margin-inline: auto centres it. This one rule replaces a pile of media queries for basic page width.
Avoid magic numbers
Logical properties such as padding-inline and margin-block follow the writing direction, so the same stylesheet works for right-to-left languages without a mirrored set of rules. Physical properties are still fine for a single-language site, but the logical names cost nothing and travel better.
If a value appears twice, make it a token. If a margin exists only to fix one element’s alignment, question the layout instead. Values like margin-top: 37px are a sign that something above is not behaving as expected.
Using margins on every child and then adding gap as well doubles the space and makes it unpredictable at wrap points. Choose one system per container: gap for layout children, margins for prose elements such as paragraphs and headings.
Build a page section with a heading, two paragraphs and a three-item row of tags. Use the spacing scale for all vertical space and gap for the tags, with no arbitrary pixel values.
Show solution
Editable. Runs in your browser.
<section class="section">
<h2>Spacing scale</h2>
<p>One scale, used consistently.</p>
<p>No magic numbers.</p>
<ul class="tags">
<li>HTML</li><li>CSS</li><li>Layout</li>
</ul>
</section>
Press Run to see the output.
Vertical space comes from the stack rule, the tags use gap, and each value traces back to a token. Changing the scale updates the whole section.
Recap
- A small spacing scale, exposed as custom properties, keeps a page consistent.
- Padding is inside the component; margin separates components;
gapspaces layout children. - Cap content width with
min()and centre it withmargin-inline: auto.
Next, you will lay out rows and columns with Flexbox.
Should spacing be in px or rem?
Why do my margins not collapse inside a flex container?
Progress is saved in this browser only. No account, no email.