Lesson 17 of 296
The box model
What you will learn
- Why every element is a box
- The four layers of the box
- How width is really calculated
box-sizing: border-boxand why everyone uses it- Margin collapsing
- Outline vs border
On this page
Every element is a box
The browser draws every element as a rectangle, even round-looking buttons and single words. Understanding this box is the key to all CSS layout.
The four layers
From the inside out:
+-------------------------------- margin --------------------------------+
| +------------------------------ border ----------------------------+ |
| | +-------------------------- padding ------------------------+ | |
| | | | | |
| | | content (text, image) | | |
| | | | | |
| | +-----------------------------------------------------------+ | |
| +-----------------------------------------------------------------+ |
+------------------------------------------------------------------------+| Layer | What it is | Has the background colour? |
|---|---|---|
| Content | The text or image | Yes |
| Padding | Space inside the border, around the content | Yes |
| Border | A line around the padding | Has its own colour |
| Margin | Space outside the border, pushing other elements away | No, always see-through |
.card {
padding: 16px;
border: 1px solid #ccc;
margin: 24px;
}Writing padding and margin
| Written | Means |
|---|---|
margin: 10px; | All four sides 10px |
margin: 10px 20px; | Top and bottom 10px, left and right 20px |
margin: 10px 20px 30px; | Top 10px, left and right 20px, bottom 30px |
margin: 10px 20px 30px 40px; | Top, right, bottom, left (clockwise from the top) |
margin-top: 10px; | One side only |
Remember the four-value order with the word TRouBLe: Top, Right, Bottom, Left.
Centring a block horizontally:
.container {
width: 800px;
margin: 0 auto;
}auto on the left and right shares the free space equally.
How width is calculated: the trap
By default, width sets only the content width. Padding and border are added on top.
.box {
width: 300px;
padding: 20px;
border: 5px solid black;
}Real visible width = 300 + 20 + 20 + 5 + 5 = 350px. Not 300.
This surprises almost everyone. It is called the content-box model.
The fix: box-sizing: border-box
.box { box-sizing: border-box; }Now width: 300px means the whole box including padding and border is 300px. The content shrinks to fit. This is much easier to think about.
Nearly every project starts with this rule for every element:
*,
*::before,
*::after {
box-sizing: border-box;
}Margin collapsing
When two vertical margins touch, they do not add up. Only the larger one is used. This is called margin collapsing.
h2 { margin-bottom: 30px; }
p { margin-top: 20px; }The gap between the heading and the paragraph is 30px, not 50px.
Facts about collapsing:
- It only happens with top and bottom margins, never left and right.
- It does not happen inside Flexbox or Grid containers.
- A child's top margin can "escape" through its parent if the parent has no padding or border. Adding padding to the parent, or making it a flex or grid container, stops this.
Padding vs margin: which one?
- Padding for space inside an element (between the text and the edge of a button).
- Margin for space between elements.
- In Flexbox and Grid layouts, prefer the
gapproperty for space between items (Lessons 21 and 22).
Outline vs border
| Border | Outline | |
|---|---|---|
| Part of the box model | Yes | No |
| Takes space and moves other elements | Yes | No, drawn on top |
| Main use | Design | Focus rings for keyboard users |
Seeing the box model
In Chrome dev tools, select an element and open the Computed tab. It shows a diagram of the content, padding, border and margin with exact numbers. Use it whenever spacing looks wrong.
Common mistakes
- Expecting
width: 300pxwith padding to be 300px wide, withoutborder-box. - Being surprised by margin collapsing.
- Using margins on every item instead of
gapin Flexbox and Grid. - Removing outlines without adding another clear focus style.
How to say it in an interview
"Every element is a box made of content, padding, border and margin. By default, width only sets the content, so padding and border are added on top. I set box-sizing to border-box on everything so width includes padding and border. Vertical margins between blocks collapse, so only the larger one applies, but that does not happen in flex or grid containers."
Key words
| Word | Meaning |
|---|---|
| Box model | The content, padding, border and margin of every element |
| Padding | Space inside the border |
| Margin | Space outside the border |
| border-box | Width and height include padding and border |
| Margin collapsing | Touching vertical margins merging into the larger one |
| Outline | A line drawn outside the border that takes no space |
Now prove it
This lesson is complete when you pass the quiz and all three practice levels.