Beginner · CSS basics · topic 16 of 64 · 30 min
The box model
Explanation
Four layers of every box
- Content: the text or image itself.
- Padding: space inside the box, between content and border. It takes the background colour.
- Border: a line around the padding.
- Margin: space outside the border that pushes other boxes away. It is always transparent.
.card {
width: 300px;
padding: 16px; /* all four sides */
border: 2px solid #ddd;
margin: 24px auto; /* 24px top and bottom, centred left-right */
}
/* Shorthand order: top right bottom left (clockwise) */
.note { padding: 8px 16px 8px 16px; }box-sizing: the width surprise
By default (content-box), width means only the content. Padding and border are added on top: 300px + 2 x 16px + 2 x 2px = 336px wide. With box-sizing: border-box, width includes padding and border, so the card is exactly 300px. Almost every project sets this for all elements.
*, *::before, *::after {
box-sizing: border-box;
}display: how boxes sit
| Value | Behaviour | Default for |
|---|---|---|
| block | Starts on a new line, takes the full width | div, p, h1, section, ul |
| inline | Flows inside text; width and height are ignored | span, a, strong |
| inline-block | Flows inside text but accepts width, height and padding | Often used for badges |
| none | Removed from the page completely | Hidden elements |
| flex / grid | Lay out the children (intermediate level) | Layouts |
Width, max-width and centring
width: 100% fills the parent. max-width stops a box from growing too wide on big screens while letting it shrink on phones. A block with a max-width and margin-inline: auto (or margin: 0 auto) sits in the centre.
.container {
max-width: 960px;
margin: 0 auto;
padding: 0 16px;
}Margin collapsing and overflow
When two block boxes sit one above the other, their vertical margins do not add up; the bigger one wins. This is called margin collapsing. And when content is bigger than its box, overflow decides what happens: visible (spills out), hidden (cut off), or auto (scroll bar when needed).
Watch the lesson
The box model · 1/6
Every element is a box. Learning the box model removes half of all CSS confusion.
Prefer a video by a teacher? Find free videos on “css box model margin padding border box-sizing tutorial” on YouTube ↗
In real life: A product card on a shopping site
Each product card has 16px padding so the photo and price do not touch the edge, a thin grey border, rounded corners and a 12px margin from the next card. With box-sizing: border-box, the developer can say "each card is 50% wide" on phones and know two cards fit side by side exactly.
Practice (0/2 done)
Style the .card: padding 16px, a border "1px solid #cccccc", margin-bottom 12px, and box-sizing border-box.
Write a function boxWidth(width, padding, border, sizing) that returns the total width a box takes on screen (without margin). With sizing "content-box", padding and border are added on both sides; with "border-box" the total is just width. Example: boxWidth(300, 16, 2, "content-box") is 336.
Quick quiz
Read more (free)
Pass the quiz (60% or more) or mark this topic as done when you are ready.
Your progress is saved on this device only.