The Box Model
See every element as a box of content, padding, border and margin, calculate its real size, use box-sizing, and choose between block, inline and inline-block.
Welcome back!
Step 1 of 15Learn
Everything is a box
Open developer tools on any page, hover over elements, and you'll see colored rectangles appear. That's the single most important idea in CSS layout: every element is a rectangular box.
Each box has four layers, from the inside out:
- Content: the text or image itself, sized by
widthandheight. - Padding: space between the content and the border. The background color fills it.
- Border: a line around the padding (
border: 2px solid gray). - Margin: space outside the border that pushes other boxes away. It's always transparent.
.card {
width: 300px;
padding: 20px;
border: 2px solid #ccc;
margin: 16px;
}Padding and margin accept one to four values, going clockwise from the top:
padding: 20px;applies to all four sides.padding: 10px 20px;is 10 px top and bottom, 20 px left and right.padding: 10px 20px 30px 40px;is top, right, bottom, left. Remember TRBL, or "trouble."
You can also set one side, like margin-top: 2rem;. A centering trick you'll use constantly: margin: 0 auto; on a box with a set width centers it horizontally, because auto splits the leftover space equally on the left and right.
Step 2 of 15Note
Step 3 of 15Practice · Put in order
Put the layers of the CSS box model in order from the inside out.
Drag the items, or use the arrow buttons, to put them in order.
- Border
- Padding
- Content
- Margin
Step 4 of 15Diagram
The box model, from the outside in
Tap a shape with to see more.
- Margin
Transparent space outside the border that pushes other boxes away.
- Border
The line around the box (can be zero width).
- Padding
Space inside the border; shares the background color.
- Content
The text or image itself, sized by width and height.
Step 5 of 15Learn
How wide is that box, really?
Here's the surprise that trips up nearly every beginner. With the CSS default, width sets only the content width. Padding and border are added on top.
For the .card above:
- Content: 300 px
- Padding: 20 px left + 20 px right = 40 px
- Border: 2 px left + 2 px right = 4 px
- Visible box width: 344 px
- Plus margins: 16 px + 16 px, so it takes up 376 px of horizontal space.
That's why two boxes set to width: 50% with padding won't fit side by side: each is actually more than half.
The fix is one line that most professionals put at the top of every stylesheet:
*, *::before, *::after {
box-sizing: border-box;
}With box-sizing: border-box, width includes the content, padding and border. A 300 px card is 300 px wide on screen, and the browser shrinks the content area to make room for the padding and border. Margins are still added outside.
The default behavior is called content-box. Border-box makes sizes predictable, especially with percentages: two boxes at 50% with border-box really do fit side by side.
Step 6 of 15Try the calculator
Box size calculator
Enter a box's CSS values. Set box-sizing to 0 for content-box (the default) or 1 for border-box.
- Visible width (border to border)
- 344 px
- Total space taken including margins
- 376 px
- Width of the content area
- 300 px
Step 7 of 15Practice · Work it out
A button has width: 200px; padding: 12px; border: 3px solid; and the default content-box sizing. What is its visible width from border edge to border edge, in pixels?
Step 8 of 15Practice · Chart
Same CSS, two box-sizing values
width: 200px; padding: 12px; border: 3px. With content-box the padding and border are added on; with border-box they are taken out of the 200.
Tap the box whose total visible width is exactly 200 px.
Tap the bar that answers the question, then press Check.
| Item | Content | Padding (both sides) | Border (both sides) |
|---|---|---|---|
| content-box (default) | 200 px | 24 px | 6 px |
| border-box | 170 px | 24 px | 6 px |
Step 9 of 15Practice · Code challenge
Write boxWidth(width, padding, border, borderBox) that returns an element's visible width in pixels. Padding and border are per side. If borderBox is true, the visible width is just width; otherwise add padding and border on both sides.
boxWidth(300, 20, 2, false)boxWidth(300, 20, 2, true)boxWidth(100, 0, 1, false)
Step 10 of 15Learn
Block, inline and inline-block
Not all boxes behave the same way in the flow of a page. The display property controls this.
Block elements (div, p, h1–h6, ul, section and more) start on a new line and stretch to fill the full width of their parent by default. Width, height, padding and margin all work as expected.
Inline elements (a, strong, em, span) sit within a line of text and only take as much width as their content. Width and height are ignored, and top and bottom margins don't push other lines away. That's why setting a height on a link does nothing.
inline-block is a hybrid: the element sits in the line like inline, but respects width, height, padding and margin like block. It's handy for button-like links.
none removes the element from the page entirely, as if it weren't there.
You can change any element's display: a.button { display: inline-block; padding: 0.75rem 1.5rem; } turns a link into a proper clickable button shape, and display: block makes an image sit on its own line.
Later you'll meet display: flex and display: grid, which turn a box into a layout container for its children.
One more quirk: when two block elements' vertical margins meet, they collapse into the larger of the two rather than adding up. A 20 px bottom margin above a 30 px top margin gives 30 px of space, not 50.
Step 11 of 15Note
Step 12 of 15Practice · Sort into groups
Is each element block or inline by default?
Tap an item, then tap its group (or drag it there).
Step 13 of 15Note
Step 14 of 15Practice · True or false
Two stacked paragraphs, the first with margin-bottom: 20px and the second with margin-top: 30px, end up 50px apart.
Step 15 of 15Practice · Choose one
A designer sets width: 120px; height: 40px; on a plain link (a element), but nothing changes. Why?
Lesson complete!