The Box Model and box-sizing
How every element becomes a rectangle of content, padding, border and margin, why width does not always mean what you expect, and what box-sizing changes.
The Box Model and box-sizing
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- Every element generates a box with four layers from the inside out: content, padding, border, margin. Margin is outside the box and is never part of its width.
- By default `width` sets the content box only, so `width: 200px` with `padding: 20px` and `border: 5px` renders 250px wide. With `box-sizing: border-box` the same declarations render exactly 200px.
- The standard reset is `*, *::before, *::after { box-sizing: border-box; }`. `box-sizing` is not inherited, so it has to be applied to each element.
- Percentage padding and margin resolve against the width of the containing block, even for top and bottom values.
- `inline-size` and `block-size` are the writing-mode-aware versions of `width` and `height`. In a normal horizontal language they are identical.
Everything is a rectangle
The browser does not lay out text, images and buttons as separate kinds of thing. It lays out boxes. A paragraph is a box. A link inside it is a (usually inline) box. Even a circle is a rectangle with border-radius. If you can reason about one box well, you can reason about any layout, because layout is boxes being arranged.
Each box has four layers, going outward:
- content: where the text or children go.
widthandheightsize this by default. - padding: space between the content and the border. The background shows through it.
- border: a line around the padding.
- margin: transparent space outside the border, pushing other boxes away.
Padding is the cushion inside a shipping box, margin is the gap between boxes on the shelf.
The default surprise
CSS originally defined width as the width of the content. That sounds tidy and is awkward in practice:
.card {
width: 200px;
padding: 20px;
border: 5px solid;
}
Add it up: 200 + 20 + 20 + 5 + 5 = 250px. I measured 250px for this in Chromium. You asked for a 200px card and got a 250px one, and every time you add padding the layout grows. In a two-column layout of width: 50% columns, adding a border pushes the second column onto the next line.
box-sizing changes what width measures
.card { box-sizing: border-box; }
With border-box, width covers content, padding and border together. The same declarations now render at exactly 200px, and the content area shrinks to fit (200 − 40 − 10 = 150px). Padding and borders stop being a threat to your widths. Margin is never included in either mode.
The reset almost every modern stylesheet starts with:
*,
*::before,
*::after {
box-sizing: border-box;
}
* does not match pseudo-elements, so they are listed separately. Some people write html { box-sizing: border-box } *, *::before, *::after { box-sizing: inherit } so a third-party widget can opt back out by setting its own container to content-box. Both approaches work; the simpler one is fine unless you embed foreign components. box-sizing is not an inherited property, which is why the inherit keyword is needed in the second version.
Old code and some third-party widgets still assume content-box, so keep it in mind when widths in someone else’s stylesheet look too small by exactly their padding.
Percentages are relative to the width
Percentage padding and margin resolve against the width of the containing block, even for padding-top. A 400px-wide parent and padding-top: 10% gives 40px, which I confirmed. This is why padding-top: 56.25% was the old trick for holding a 16:9 shape, and why aspect-ratio has since replaced it.
Width, height, and their logical names
width and height are tied to the screen’s horizontal and vertical axes. inline-size and block-size are tied to the direction text flows. In an English page inline-size is width and block-size is height. In a vertical Japanese layout they swap. The video touches this, and writing modes and logical properties takes it apart.
min-width, max-width, min-height and max-height clamp the result. They also follow box-sizing, so with border-box a max-width: 600px caps the box including its padding.
Auto width fills; set width shrinks
A block box with width: auto stretches to fill its container, and its padding and border go inside that space. If you set width: 100% and add padding in content-box mode, the box overflows by the padding amount. Leaving width alone on a block is often the better answer. Margin collapsing, covered in margin collapsing and formatting contexts, then decides how vertical margins between neighbouring boxes behave.
Outlines and shadows are not part of the box
outline and box-shadow paint outside the border but take no space and never affect layout, which is why focus outlines do not shift neighbours around.
What the video covers
Kevin Powell’s clip, “HTML & CSS for Absolute Beginners: The box model - width, height, inline-size, block-size”, runs under ten minutes and comes from his free beginner course. By its title it covers the box model with width, height and the logical size names. The box-sizing arithmetic and the percentage-padding point are the lesson’s own and were run in Chromium, since I could not read a transcript. The runner-up is a short 2019 video that focuses entirely on box-sizing.
Where this fits
Every later layout lesson assumes this model. The frontend roadmap’s box model and cascade lesson shows the same four layers more briefly. Next, values, units and math functions covers what to put in the width and padding declarations: rem, ch, %, calc() and clamp().
Check yourself
A `<div>` has `width: 300px; padding: 25px; border: 5px solid;` with the default `box-sizing`. How wide is the rendered box, and what is it with `border-box`?
Default (`content-box`): 300 + 2×25 + 2×5 = 360px. With `border-box`: exactly 300px, and the content area shrinks to 240px to make room.
Why is `* { box-sizing: border-box }` usually written with `*::before, *::after` too?
The universal selector does not match pseudo-elements, and generated `::before` and `::after` boxes are real boxes that would otherwise keep `content-box` sizing.
A box is 400px wide and has `padding-top: 10%`. How tall is that padding?
40px. Percentage padding is resolved against the width of the containing block, not its height, in horizontal writing modes. I measured 40px in Chromium.
Is the margin included in the `width` of a box with `border-box`?
No. `border-box` includes content, padding and border. Margin is outside the box and always adds to the space the element takes in the layout.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.