Sub topic 1
What White Space Is and Why It Matters
White space is the empty area on a page: the gaps around text, between images, and inside buttons. This chapter explains what it is, which CSS properties create it, and why it is the main tool for showing which content belongs together. Later chapters cover each technique in detail.
Prerequisites
This chapter assumes you already know:
- Basic HTML structure: headings, paragraphs, lists and forms.
- Basic CSS selectors and the box model (content, padding, border, margin).
- The
remandemunits.
You do not need to know flexbox, grid or custom properties yet. They appear briefly in the examples and are explained in later chapters.
What white space is
White space, also called negative space, is any part of a layout with no content in it. It does not have to be white. A dark theme, a photo background or a colored panel all have white space wherever nothing is drawn on top of the background.
Designers separate it into two kinds:
- Macro white space is the large-scale space: page margins, the gaps between sections, and the room around a hero image.
- Micro white space is the small-scale space: line height, the gap between letters, the space between a label and its input, and the padding inside a button.
You will use both kinds on every page. Macro space organizes the page into regions. Micro space makes each region readable.
Where white space comes from in CSS
Browsers do not have a "white space" property. The empty areas come from several existing properties, and each one has a different job.
| Source | What it spaces | Typical use |
|---|---|---|
| `padding` | Between an element's content and its own border | Room inside cards, buttons and inputs |
| `margin` | Between an element's border and its neighbors | Separating blocks in normal flow |
| `gap` | Between children of a flex or grid container | Rows of cards, form layouts |
| `line-height` | Between lines of text | Readable paragraphs |
| `letter-spacing`, `word-spacing` | Between characters and words | Fine typographic tuning |
The chapters ahead explain how to choose among these. For now, remember that "adding space" is not one action. Space inside an element and space between elements behave differently, and picking the wrong one causes most spacing bugs.
Why it matters
White space does four jobs. The first is the subject of this tutorial.
1. It shows grouping
People read distance as a relationship. Items that sit close together look related. Items that sit far apart look separate. Chapter 2 covers this as the proximity principle. A heading that sits close to the paragraph below it looks like that paragraph's heading. If it sits halfway between two paragraphs, readers have to work out which one it belongs to.
2. It supports reading
Lines of text need room. If line-height is too small, the eye loses its place when it moves from one line to the next. Paragraphs with no gap between them look like one block. Generous but not excessive spacing lets people scan a page and find what they need.
3. It sets hierarchy and focus
A button surrounded by space draws attention. An element packed into a crowded area gets lost. Extra space around something signals that it matters. Many "premium" designs look that way mainly because they give their key elements more room.
4. It reduces the need for other markup
Without good spacing, developers reach for borders, background colors and divider lines to separate content. Each added line is more visual noise. Space does the same job without adding anything to look at.
A worked example: the same content, two spacings
The page below has two short entries. The first uses the same gap everywhere. The second uses a small gap inside each entry and a larger gap between entries. Save it as spacing-demo.html and open it in a browser.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Spacing demo</title>
<style>
body {
font-family: system-ui, sans-serif;
line-height: 1.5;
max-width: 40rem;
margin: 2rem auto;
padding: 0 1rem;
}
/* Version A: one value for everything */
.even h2,
.even p {
margin: 0 0 1.5rem;
}
/* Version B: small gap inside a group, large gap between groups */
.grouped article {
margin-bottom: 2.5rem;
}
.grouped h2 {
margin: 0 0 0.25rem;
}
.grouped p {
margin: 0;
}
h2 { font-size: 1.25rem; }
</style>
</head>
<body>
<h1>Version A: even spacing</h1>
<section class="even">
<article>
<h2>Opening hours</h2>
<p>Monday to Friday, 9:00 to 17:00.</p>
</article>
<article>
<h2>Contact</h2>
<p>Email us at [email protected].</p>
</article>
</section>
<h1>Version B: grouped spacing</h1>
<section class="grouped">
<article>
<h2>Opening hours</h2>
<p>Monday to Friday, 9:00 to 17:00.</p>
</article>
<article>
<h2>Contact</h2>
<p>Email us at [email protected].</p>
</article>
</section>
</body>
</html>In version A, every element is 1.5rem from the next. The gap between "Opening hours" and its paragraph is the same as the gap between that paragraph and the "Contact" heading. The four lines look like an evenly spaced list, and the reader sees no pairs.
In version B, the heading is 0.25rem from its paragraph, but 2.5rem separates the entries. The two pairs are visible with no border, no background and no extra markup. The HTML is identical in both versions. Only the spacing differs.
The ratio matters. The between-group gap here is ten times the within-group gap. A tiny difference, such as 1rem inside and 1.1rem between, would not be noticed. Later chapters show how to choose these values from a fixed scale instead of inventing them one at a time.
Common errors
These mistakes show up often in code written by people who are learning spacing.
- Using the same spacing everywhere. This is the version A problem. It removes the signal that grouping depends on.
- Making spacing with markup. Repeated
<br>tags, empty<p></p>elements and runs of add space but also add noise for screen readers and break when the content or font changes. Use CSS properties instead. - Adding space to the wrong side. If you give a heading a large
margin-bottomand a smallmargin-top, it sits closer to the paragraph above than to the one below it. The heading then looks attached to the wrong content. - Choosing values one at a time. Values like
13px,17pxand22pxscattered through a stylesheet produce uneven pages that are hard to maintain. A fixed scale fixes this. - Treating white space as wasted. Removing space to fit more on screen usually makes a page slower to read. Dense layouts do have a place, such as data tables, but you should choose them on purpose.
- Too much space. Gaps that are too large can break groups apart, force extra scrolling, and make related content feel unconnected. The aim is contrast between gaps, not maximum space everywhere.
A practical rule to carry forward
When you place two elements on a page, ask two questions:
- Are they part of the same group?
- If so, is the space between them smaller than the space between this group and the next one?
If the answer to the second question is no, the grouping is probably unclear. Fix it by shrinking the inner gap, growing the outer gap, or both. Growing the outer gap is often the better choice, because it avoids crowding the content inside the group.
What comes next
The rest of the tutorial builds on this foundation in the order below.
- How the proximity principle decides which gaps should be small and which large.
- Padding versus margin, and when
gapis the better choice. - A spacing scale built with CSS custom properties, so every gap comes from a short list of values.
- Margin collapsing, flow spacing, typography, cards, sections and forms.
- Responsive and accessible spacing, then an audit method.
Spacing is not decoration added at the end. It is part of how a layout communicates structure, and it is easiest to get right when you plan it from the start.
Exercise
A page shows a heading, then a paragraph, then another heading, then another paragraph. Every element has margin-bottom: 1.5rem. Readers say it is hard to tell which paragraph goes with which heading. Describe a spacing change that fixes this without adding borders or new elements, and explain why it works.
Show answer
Reduce the gap between each heading and the paragraph that follows it (for example to 0.25rem to 0.5rem), and keep or increase the gap after each paragraph (for example to 2rem to 2.5rem). Now the heading and its paragraph are closer to each other than to anything else, so the proximity principle makes the pair read as one group. The change works because the contrast between the small inner gap and the larger outer gap carries the grouping, and the HTML does not need to change.
Exercise
Name three CSS properties that create white space, and say for each one whether it adds space inside an element, between elements, or within text.
Show answer
padding adds space inside an element, between its content and its border. margin adds space between elements, outside the border. gap adds space between the children of a flex or grid container. line-height adds space between lines of text. Any three of these, with the correct kind of space, is a correct answer. letter-spacing and word-spacing also work and both add space within text.