Lesson 14 of 23
In progress
The box model, without the guessing
Understand content, padding, border and margin, switch to border-box, and explain why two margins sometimes become one.
Every element is a rectangle made of four layers. Almost every "why is this 20 pixels wider than I asked for" question is answered by naming which layer the width applied to.
What you will learn
- Name the four layers of a box and which one
widthaffects. - Use
box-sizing: border-boxso sizes include padding and borders. - Predict when vertical margins collapse.
Four layers
From the inside out:
- Content — the text or image.
- Padding — space inside the border, part of the element’s background.
- Border — the visible edge.
- Margin — space outside the border, always transparent.
Editable. Runs in your browser.
<div class="box">The content area is 240 pixels wide by default.</div>
Press Run to see the output.
By default, width sets the content box only. The element’s total width is 240 + 20 + 20 + 4 + 4 = 288 pixels. That is why a "240 pixel" box does not line up with a 240 pixel column.
border-box makes the number honest
Editable. Runs in your browser.
.box {
width: 240px;
padding: 20px;
border: 4px solid #7a4bd2;
background: #f1edfb;
}
</style>
<div class="box">Now the border and padding are inside the 240 pixels.</div>
Press Run to see the output.
With border-box, the declared width includes padding and border. The content area shrinks instead. This is the near-universal reset because it makes layout arithmetic match the number you typed.
The universal selector applies it to pseudo-elements too, which prevents a stray ::before from using the old model.
Height, min-height and overflow
Setting a fixed height on a text container is risky: content grows when the font size increases or the text changes, and the overflow is either clipped or spills.
Prefer min-height so the box can grow. If content must scroll, say so explicitly with overflow: auto, and test with a keyboard to make sure the scroll area is reachable.
Editable. Runs in your browser.
<div class="panel">This box grows if the text needs more room.</div>
Press Run to see the output.
Margin collapse
Vertical margins between block elements in normal flow collapse: the larger margin wins instead of adding. A heading with margin-bottom: 24px followed by a paragraph with margin-top: 16px produces 24 pixels, not 40.
Editable. Runs in your browser.
<div class="wrap">
<h2>Heading</h2>
<p>The gap is 24 pixels, not 40.</p>
</div>
Press Run to see the output.
Margin collapse does not happen inside Flexbox or Grid containers, which is one reason those layouts feel more predictable. It also does not happen when a border or padding separates the margins.
aspect-ratio is the modern way to reserve a shape without a fixed height: aspect-ratio: 16 / 9 keeps a video or image box proportional while the width changes, and the browser can reserve the space before the media loads.
Modern CSS gives you margin-block and margin-inline, which map to top/bottom and left/right in horizontal writing. They are shorter and adapt to vertical writing modes.
display changes the box
block elements take the full width and start on a new line. inline elements sit in the text flow and ignore width and vertical margin. inline-block sits in the flow but accepts width and height.
Editable. Runs in your browser.
<p>A <span class="tag">tag</span> sits inside the sentence.</p>
Press Run to see the output.
Outlines are drawn outside the box and do not affect layout, which is exactly why focus rings use them instead of borders. An inline element with width: 200px will not become 200 pixels wide; that is the single most common display confusion. Change it to inline-block or block when you need dimensions.
Adding width: 100% and padding to an element without border-box makes it wider than its container and causes horizontal scrolling. Either switch to border-box globally or use max-width: 100% and avoid fixed widths on fluid layouts.
Create a card exactly 320 pixels wide including 24 pixels of padding and a 2 pixel border, using border-box. Add a second card with the default box model and compare their total widths in the Elements panel.
Show solution
Editable. Runs in your browser.
<div class="card card–border-box">Border box: total width 320px.</div>
<div class="card">Content box: total width 372px.</div>
Press Run to see the output.
The first card’s content area is 320 minus 48 padding minus 4 border, which is 268 pixels. The second card’s content is 320 pixels, so its total is 372.
Recap
- Width applies to the content box by default; padding and border add to it.
box-sizing: border-boxmakes declared sizes include padding and border.- Adjacent vertical margins collapse, and Flexbox or Grid containers prevent it.
Next, you will turn spacing into a small system instead of a pile of arbitrary numbers.
Should I reset margins to zero?
Why does padding not push content away from a sibling?
Progress is saved in this browser only. No account, no email.