Box Model¶
Every element is a rectangular box. The box has four layers, from inside out:
margin
┌─────────────────────────┐
│ border │
│ ┌─────────────────────┐ │
│ │ padding │ │
│ │ ┌─────────────────┐ │ │
│ │ │ content │ │ │
│ │ └─────────────────┘ │ │
│ └─────────────────────┘ │
└─────────────────────────┘
- Content: the text or image.
- Padding: space inside the border, around the content.
- Border: the edge of the box.
- Margin: space outside the border, between this box and its neighbours.
Seeing the box model¶
By default, width sets the content width only. The visible box is wider because padding and border are added:
That default is called content-box and it catches almost everyone at least once.
box-sizing¶
Set box-sizing: border-box once at the top of the stylesheet:
Now 240px includes the padding and border. This makes widths easier to reason about, especially in layouts.
Padding and margin¶
- Padding is inside the box. The background colour covers it.
- Margin is outside the box. The background does not cover it.
Use padding when you want more space around the content inside a box. Use margin when you want space between boxes.
Shorthand¶
/* all four sides */
margin: 1rem;
/* vertical | horizontal */
padding: 0.5rem 1rem;
/* top | horizontal | bottom */
margin: 1rem 0 2rem;
/* top | right | bottom | left */
padding: 1rem 1.5rem 1rem 1.5rem;
The same pattern works for border, although you also need a width, style, and colour:
Margin collapse¶
Vertical margins between block elements can collapse. The larger margin wins:
The gap between them is 1rem, not 2rem. This is normal. If you need exact spacing, use padding or gap in a flex or grid container.
Try it¶
- Add a card to a page with a background colour, padding, border, and margin.
- Open the browser's developer tools and inspect the card.
- Find the box model diagram in the styles panel. Hover over the content, padding, border, and margin areas to see them highlighted.
- Change
box-sizingfromborder-boxtocontent-boxand compare the visible width. - Put the setting back.
If the box is wider than you expected, check whether box-sizing is set.
Next: Selectors