Sub topic 6
Control spacing with the box model
This chapter assumes you can write an HTML page with a linked stylesheet and apply CSS rules with selectors. You should also know that a rule has a selector and declarations. Here you will learn how the browser sizes and spaces every element, and how to fix the most common spacing surprises.
Every element is a box
The browser treats each element as a rectangle made of four layers, from the inside out:
- Content: the text, image or child elements.
- Padding: space between the content and the border, inside the element's background.
- Border: a line around the padding.
- Margin: transparent space outside the border that pushes other elements away.
Padding takes on the element's background color. Margin never does. That difference decides which property you use. Use padding to give a card breathing room inside its colored area. Use margin to separate the card from its neighbors.
Fix widths with border-box
By default, width sets only the content width. Padding and border are added on top. A box with width: 20rem and padding: 1rem ends up 22rem wide, plus the border. This makes layouts hard to predict.
The box-sizing: border-box value changes the rule so width includes padding and border. The stylesheet from this tutorial applies it to every element:
*,
*::before,
*::after {
box-sizing: border-box;
}With this rule, a 20rem box stays 20rem wide no matter how much padding you add.
Use the shorthand properties
margin and padding accept one to four values. The values run clockwise from the top.
| Values | Meaning |
|---|---|
| `1rem` | All four sides |
| `1rem 2rem` | Top and bottom, then left and right |
| `1rem 2rem 3rem` | Top, left and right, bottom |
| `1rem 2rem 3rem 4rem` | Top, right, bottom, left |
You can also set one side, such as margin-bottom: 1rem, or use logical properties like padding-inline, which set the left and right sides in left-to-right languages.
Center a column with auto margins
The site's main element combines these ideas:
main {
max-width: 60rem;
margin: 0 auto;
padding: calc(var(--space) * 2) var(--space);
}max-width stops the column from growing past 60rem but lets it shrink on small screens. margin: 0 auto gives no vertical margin and splits the leftover horizontal space equally, which centers the block. The padding keeps text away from the screen edges. calc() does arithmetic on values, here doubling the --space variable for the top and bottom.
Watch for margin collapse
When the vertical margins of two blocks touch, the browser does not add them. It uses the larger one. If a paragraph has margin-bottom: 2rem and the next heading has margin-top: 1rem, the gap is 2rem, not 3rem. This is called margin collapse. It applies only to vertical margins of block elements, not to horizontal margins.
A common way to stay in control is to space elements in one direction only, for example with margin-bottom.
Common errors
- A page that scrolls sideways. A fixed
widthplus padding overflows withoutborder-box. Usemax-widthandborder-box. - Padding where you wanted margin. If a gap shows the element's background color, it is padding.
- Auto margins that do nothing.
margin: 0 autoneeds a width ormax-widthsmaller than the parent. Without one, a block already fills the whole row. - Unexpected default margins. Browsers give
body, headings and paragraphs default margins. Inspect the element in your browser's developer tools to see which layer creates a gap.
Exercise
A card has width: 300px, padding: 20px and border: 5px solid. With the default content-box sizing, how wide is the card on screen? How wide is it with border-box?
Show answer
With content-box, the total is 300 + 20 + 20 + 5 + 5 = 350px. With border-box, it is 300px, and the content area shrinks to 250px.
Exercise
A paragraph has margin-bottom: 24px. The heading below it has margin-top: 16px. What is the gap between them?
Show answer
24px. The vertical margins collapse, and the browser uses the larger of the two.