Lesson 15 of 23

In progress

Course progress

HTML and CSS

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.

HTML
Editable. Runs in your browser.





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.

HTML
Editable. Runs in your browser.





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.

HTML
Editable. Runs in your browser.





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.

HTML
Editable. Runs in your browser.





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
HTML
Editable. Runs in your browser.





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; gap spaces layout children.
  • Cap content width with min() and centre it with margin-inline: auto.

Next, you will lay out rows and columns with Flexbox.

Should spacing be in px or rem?
`rem` respects the reader’s text size and is the better default. `px` is acceptable for hairlines and borders, where scaling would look wrong.
Why do my margins not collapse inside a flex container?
Flex items establish their own formatting context, so adjacent margins do not collapse. That is usually helpful, and it is why Flexbox layouts are easier to predict.

Progress is saved in this browser only. No account, no email.

Admin

About the author

Admin

Instructor and technical writer at tutorials.in

Writes beginner lessons for tutorials.in. Each lesson is checked against the language or tool documentation, and examples are run before publishing.

Reviewed by the editorial team before publishing. Last updated November 16, 2026.