Sub topic 1
What White Space Is and Why It Matters
Prerequisites
This chapter assumes basic HTML structure, CSS selectors, the box model, and familiarity with rem and em units.
White space and the proximity principle
White space is the empty space between or around visible content. It includes padding, margins, gaps, line spacing, and larger spaces between sections. White space is not wasted space. It helps readers identify structure and focus.
The proximity principle says that items placed close together appear related. Items separated by more space appear less related. The ratio matters more than any single value: a label should be closer to its input than the input is to the next form field.
Padding versus margin: spacing inside and outside
Padding is space inside an element, between its content and its border. Margin is space outside an element, separating it from neighboring elements.
Use padding when the space belongs to the component. A card's internal breathing room is padding. Use margin when spacing belongs to the relationship between separate elements. For groups of children, prefer gap when the parent uses grid or flexbox.
.card {
padding: 1.5rem;
border: 1px solid #ccc;
}
.card-list {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(16rem, 1fr));
gap: 1.5rem;
}Adjacent vertical margins can collapse in normal block flow. For example, a bottom margin of 2rem followed by a top margin of 1rem usually creates 2rem of space, not 3rem. This behavior does not apply in the same way to flex and grid containers, where gap gives more predictable results.
Building a spacing scale with custom properties
A spacing scale limits the values used across a page. CSS custom properties, also called variables, let you name and reuse those values.
:root {
--space-1: 0.25rem;
--space-2: 0.5rem;
--space-3: 1rem;
--space-4: 1.5rem;
--space-5: 2.5rem;
--space-6: 4rem;
}Choose a smaller token for closely related content and a larger token for weaker relationships. Consistent steps make the page's grouping easier to predict and maintain.
Exercise
A card has too little space between its title and body, but too much space between cards. Which properties should you inspect?
Show answer
Inspect the card's padding for internal space and the card-list's gap for space between cards. Adjust the appropriate relationship rather than adding arbitrary margins to the children.