Open a chat app. The messages one person sends in a row sit close together, then there's a wider gap before someone else's reply. There's no line and no dividing box, yet you know right away which messages go together.
The thing doing that work is empty space. It's called negative space, or whitespace: the empty space around and between elements. It isn't wasted space. It's what tells the eye which things belong together and which are separate. Picture a food stall's menu board written tightly with no blank lines. "Iced tea" and "Fried rice" run together, and you have to read everything to find the drinks. Add one blank line between the food group and the drinks group. Now the eye sees two groups before reading a single word. The blank line on the menu board is whitespace.
Micro and macro whitespace
Whitespace comes in two sizes:
- Micro whitespace: small space inside a component, such as the padding inside a card, a button's padding, the gap between an icon and its text, and a paragraph's line-height (the height of each line).
- Macro whitespace: large space between parts of the page, such as the space from the header (the top part of the page) to the content, and the space between the "Choose a room" section and the "Getting to Mist Homestay" section.
Micro whitespace makes one component easy to read. Macro whitespace makes the page read as several parts instead of one long pile.
A spacing scale: multiples of 4 and 8
Don't pick spacing by feel ("13px looks about right"). Use a fixed scale: a list of spacing values chosen in advance, built from multiples of 4 and 8. In CSS you can write the scale as CSS variables (names such as --space-4 that each hold one value) inside :root (the outermost element), so the whole page can use them:
:root {
--space-1: 4px;
--space-2: 8px;
--space-3: 12px;
--space-4: 16px;
--space-6: 24px;
--space-8: 32px;
--space-12: 48px;
--space-16: 64px;
}
- Small numbers (4, 8, 12, 16) for micro: inside cards and buttons.
- Large numbers (48, 64) for macro: between parts of the page.
- At the large numbers, the jumps get bigger (32, 48, 64) so that people notice the difference. A 4px difference on an 8px gap is 50%, but on a 60px gap it's only about 7%.
You don't need to invent this scale yourself. In Tailwind (a set of ready-made CSS classes), one spacing step is 0.25rem (4px). Material Design (Google's design guidelines) builds its spacing from multiples of 4 and 8. When designers and developers use the same scale, they say the same numbers, and the spacing across the page has a consistent rhythm.
Spacing that groups
Here is the Terrace Room card. It holds three things: the room name, the price and the night buttons (the buttons for choosing how many nights). The name and the price are one group: which room, and how much it costs. The night buttons are another group: what you choose.
| Equal | Grouped | |
|---|---|---|
| name → price | 12px | 4px |
| price → night buttons | 12px | 16px |
With equal gaps the price floats in the middle: does it belong to the room name or to the buttons? With 4px and 16px, the price clearly belongs with the name. The rule: space inside a group is smaller than space between groups. This principle is called proximity (closeness), and the Gestalt lesson covers it again.
In CSS, you write those gaps as bottom margins. .name is the room name and .price is its price. margin: 0 0 4px means 0 on the top and sides, and a 4px bottom margin:
.name { margin: 0 0 4px; } /* close to the price: one group */
.price { margin: 0 0 16px; } /* far from the buttons: another group */
Example: the "Choose a room" section
In the exercise, the rooms section on Mrs. Sri's page starts out cramped. This table holds the values in the exercise's starter code, and the values that give the section room:
| Space | Starter (cramped) | Roomy |
|---|---|---|
padding inside a card (.card padding) |
8px | 24px |
card to card (.card margin-bottom) |
4px | 16px |
heading to the first card (h1 margin-bottom) |
4px | 24px |
section to the next section (.section margin-bottom) |
8px | 64px |
This is the code for the rooms section with the roomy values. <section> is the tag for one part of a page, and <style> holds its CSS:
<section class="section">
<h1>Choose a room</h1>
<div class="card">Terrace Room · $35 / night</div>
<div class="card">Attic Room · $27.50 / night</div>
</section>
<style>
.card { padding: 24px; margin-bottom: 16px; } /* micro: room inside, then space between cards */
.section { margin-bottom: 64px; } /* macro: space before the next section */
h1 { margin-bottom: 24px; } /* heading to its section's content */
</style>
padding: 24pxgives room inside the card, between the card's edge and its text.margin-bottom: 16px(the bottom margin) on the card puts space between it and the next card. This number is smaller than the padding. That's fine when the card has a border or a background colour: the card's edge already separates the cards.- The 64px on
.sectionis the largest space on the page, so the next section feels like a new section. - The 24px on the
h1keeps the heading close enough to its cards to read as that section's heading.
Vertical margins can merge
The last card has margin-bottom: 16px, and its section margin-bottom: 64px. How far is that card from the next section: 16px, 64px or 80px? It looks like 16 + 64 = 80px. It's actually 64px. Vertical margins (top and bottom margins) that touch, with no padding or border between them, merge into one, and the largest one is used. In this example the section has no padding or border, so the last card's bottom margin touches the section's bottom margin. This is called margin collapsing (margin collapse in the box model lesson).
Merging only happens to vertical margins. Padding never merges, and neither does gap (the space between items) in flexbox or grid, two ways CSS arranges elements.
⚠️ When does whitespace confuse people?
- Every gap the same. 12px everywhere looks tidy, but nothing groups. The price floats, and a new section doesn't feel new.
- Numbers off the scale. 5px, 7px, 13px. Every new number should come from the 4 and 8 scale.
- Adding margins up. 16 + 64 isn't 80. Vertical margins that touch merge into the largest.
- Too roomy. On a phone 375px wide, the page has 16px of padding on the left and right. A card with 64px padding on the left and right leaves only 375 - 32 - 128 = 215px for its content. Too much whitespace hides the content too.
TL;DR: Negative space (whitespace) is the empty space around and between elements, and it's what shows which things form a group. Micro whitespace sits inside components (card padding, button padding, line-height, the height of each line). Macro whitespace sits between parts of the page, such as 64px before the next section. Take every gap from a scale of multiples of 4 and 8: 4, 8, 12, 16, 24, 32, 48, 64. Space inside a group is smaller than space between groups: on the room card, name to price is 4px, and price to the night buttons (for choosing how many nights) is 16px. The roomy rooms section uses padding: 24px and margin-bottom: 16px on the cards, margin-bottom: 24px on the h1 heading, and margin-bottom: 64px on .section (the page section). Vertical margins that touch merge: 16 and 64 make 64, not 80.
⚡ Empty space that groups
Meaning: Negative space, or whitespace, is the empty space around and between elements. It shows which things form a group. Micro whitespace sits inside a component (card padding, button padding, line-height, the height of each line). Macro whitespace sits between parts of the page.
⚡ Tighter inside a group
Write:
.name { margin: 0 0 4px; } /* close to the price: one group */
.price { margin: 0 0 16px; } /* far from the buttons: another group */
Meaning: On the room card, .name is the room name and .price is its price. margin: 0 0 4px gives only a bottom margin of 4px. Space inside a group is smaller than space between groups. If every gap is 12px, the price floats between the room name and the buttons for choosing nights below it. Take every number from the scale 4, 8, 12, 16, 24, 32, 48, 64.
⚡ Vertical margins can merge
Write:
.card { padding: 24px; margin-bottom: 16px; } /* micro: room inside, then space between cards */
.section { margin-bottom: 64px; } /* macro: space before the next section */
Guess first: the cards (.card) sit inside one page section (.section). The last card has a 16px bottom margin, the section 64px. How far is that card from the next section?
64px, not 80px. Vertical margins that touch merge into one, and the largest is used.
Meaning: These merging margins are called margin collapsing, and merging only happens to vertical margins. Padding never merges, and neither does gap (the space between items) in flexbox and grid, two ways CSS arranges elements.