The box model: content, padding, border, margin
You set two columns to width: 50%, add some padding, and the second one drops onto a new line. Here is what width really measures, layer by layer, and the one line that makes it mean what you expected.
01 Two halves that do not fit
Two cards should sit side by side, each taking half the row. Without padding they do. Add padding: 16px so the text does not touch the edges, and the second card wraps underneath.
Each card is now 50% of the row plus 32px of padding. Two of them add up to more than 100%, so the second one has no room on the line and wraps.
02 Every element is four boxes
The browser lays out every element as a rectangle made of four nested areas, from the inside out:
- Content: where the text, images and child elements go.
- Padding: space between the content and the border. It shows the element’s background.
- Border: the line around the padding. It takes up space even when it is the same colour as the background.
- Margin: space outside the border that keeps neighbours away. It is always transparent.
The background is painted under the content and padding (and, by default, under the border). The margin never shows a background.
03 What width measures by default
The CSS default is box-sizing: content-box. Under it, width and height size the content area only. Padding and border are added on top.
- just added
- already counted
- not added yet
Start with the rule width: 200px; padding: 20px; border: 2px solid; margin: 10px. Nothing is laid out yet.
That is the bug from the first section. width: 50% gave each card half the row of content, and the padding came on top.
04 border-box: width means the box you see
With box-sizing: border-box, width sizes the box from one outer border edge to the other. Padding and border are taken out of it, and the content gets what is left.
- just taken out
- left for content
- not worked out yet
Same rule, plus box-sizing: border-box. Now width: 200px is the outer edge of the border. Nothing is shared out yet.
05 The one-line fix
Most projects switch every element to border-box at the top of the stylesheet. Popular resets such as modern-normalize and the Tailwind base styles do the same.
box-sizing is not inherited, which is why the rule lists every element and both pseudo-elements. With it in place, the two cards from the start are exactly 50% each, padding included, and sit side by side.
Margins are never inside width, under either setting. Two width: 50% cards with margin: 8px still overflow the row. Use gap on a flex or grid container for space between items, or subtract the margin with calc(50% - 16px).
06 See the numbers yourself
In Chrome or Firefox DevTools, select the element and open the Computed tab. The box-model diagram at the top shows margin, border, padding and content with their sizes. From the console, three properties tell the layers apart:
| Measure | content-box | border-box |
|---|---|---|
| content area | 200 | 156 |
| clientWidth (content + padding) | 240 | 196 |
| offsetWidth (+ border) | 244 | 200 |
| space in the row (+ margin) | 264 | 220 |
07 When the box model surprises you
width: 100%; padding: 1rem;Full width that overflowsUnder content-box this is wider than its parent and makes a horizontal scrollbar. A block element with no width already fills its parent and keeps the padding inside.h2 { margin-bottom: 24px }
p { margin-top: 16px }Vertical margins collapseIn normal block flow, touching top and bottom margins merge into one: the gap is 24px, the larger of the two, not 40px. Margins of flex and grid items never collapse.padding-top: 50%;Percentages use the parent’s widthPercentage padding and margin, even top and bottom, are worked out from the width of the containing block. That is why padding-top: 56.25% once made 16:9 boxes; today use aspect-ratio: 16 / 9.span { width: 200px; margin-top: 20px }Inline elements ignore widthA plain inline element such as span or a takes no width or height, and its vertical margins do not push lines apart. Make it inline-block or block first.outline: 2px solid; box-shadow: 0 0 0 4px;Outlines and shadows take no spaceUnlike border, an outline or shadow is drawn over the neighbours without moving them, so it is a safe way to show focus without a layout shift.