The CSS Box Model, Finally Explained Visually
Disclosure: this page contains Amazon affiliate links. As an Amazon Associate we earn from qualifying purchases, at no extra cost to you.
Hey there, onion peeler!
The CSS box model is the four layers wrapped around every element on a page, content, padding, border and margin, and once you can picture them, why is my box 60 pixels too wide?
turns into a two-second fix.
Every single thing on a web page is a rectangle with four layers around it, and once you can picture those layers, half of the why is this the wrong size?
moments in CSS just disappear.
If you like a printed reference beside the keyboard, the CSS Visual Dictionary paperback on Amazon draws the box model as pictures too.
Short answer: The CSS box model says every element is a rectangle made of four layers: the content, then padding, then a border, then margin on the outside. By default, width sets only the content, so padding and border make the box wider. box-sizing: border-box makes width include them. Margins sit outside and never count.
What the diagram shows, in plain text (.box { width: 300px; height: 100px; padding: 20px; border: 5px solid; margin: 30px; }):
- Content box: 300 x 100px — the size you set with
widthandheight. - Padding box: 340 x 140px (20px of padding on every side).
- Border box: 350 x 150px (a 5px border on every side) — the box you can see.
- Margin box: 410 x 210px (30px of margin on every side) — the space the box takes up on the page.
- With
box-sizing: border-box, the same CSS gives a 300 x 100px border box, a 250 x 50px content box and a 360 x 160px margin box.
What is the CSS box model?
The CSS box model is the rule that turns every element into a rectangle with four nested areas — content, padding, border and margin — and decides how big each one is.
Headings, paragraphs, images, buttons and even a single word in a <span> all get a box.
MDN Web Docs’ Introduction to the CSS basic box model puts it this way:
When laying out a document, the browser’s rendering engine represents each element as a rectangular box according to the standard CSS basic box model.
The web.dev Learn CSS box model module adds that everything displayed by CSS is a box, even if it’s just some text, or has a border-radius that makes it look like a circle
. A round avatar is still a square box with its corners painted away.
The official definition lives in the W3C CSS Box Model Module Level 3 (a W3C Recommendation since 11 April 2024), whose abstract says it describes the margin and padding properties, which create spacing in and around a CSS box
.
What are the four parts of the CSS box model?
The four parts of the CSS box model are the content area, the padding area, the border area and the margin area, from the inside out.
Each one has an outer edge, and each edge gives a box its name — the content box, padding box, border box and margin box:
- Content
- the text, image or child elements.
widthandheightsize this area by default. - Padding
- space inside the border, around the content. Initial value 0. It shows the element’s background, and it can’t be negative — the W3C spec says
Negative values for padding properties are invalid.
- Border
- the line drawn around the padding (
border: 5px solid). Its width adds to the box like padding does. - Margin
- space outside the border that pushes other boxes away. Initial value 0. It’s always transparent, and it can be negative:
Negative values for margin properties are allowed, but there may be implementation-specific limits.
The background colour or image of an element fills the content and padding, and also runs underneath the border — so a dashed border shows the background through its gaps. The margin never shows a background; CSS 2.1 says Margin backgrounds are always transparent.
How do you calculate the size of a box in CSS?
To calculate how wide a box is on screen, add its width to its left and right padding and its left and right border; add the left and right margins to that to get the space it takes.
That’s with the default box-sizing: content-box. For a block box filling its parent, the CSS 2.1 specification, section 10.3.3, writes the whole sum as one equation:
margin-left + border-left-width + padding-left + width
+ padding-right + border-right-width + margin-right
= width of containing block
Here are the four boxes of the diagram at the top, worked out both ways:
| Box | What it includes | With content-box (the default) | With border-box |
|---|---|---|---|
| Content box | content only | 300 x 100px | 250 x 50px |
| Padding box | content + padding | 340 x 140px | 290 x 90px |
| Border box | content + padding + border (what you see) | 350 x 150px | 300 x 100px |
| Margin box | everything + margin (the space it takes) | 410 x 210px | 360 x 160px |
web.dev shows the same surprise with a smaller box: a paragraph with width: 100px, padding: 20px and a 1px border comes out wider than you asked for.
In web.dev’s words, The content ends up 142px wide, instead of the 100px you specified, and it breaks out of your element.
That’s 100 + 20 + 20 + 1 + 1 = 142px.
Why is my element wider than its parent with width: 100%?
An element with width: 100% plus padding or a border is wider than its parent because the 100% goes to the content box alone, and the padding and border are added on top.
In an 800px parent, width: 100%; padding: 20px; border: 5px solid; makes an 850px box that overflows by 50px.
There are two easy fixes:
- Leave
widthatauto. A block box withwidth: autofills its parent by solving the 10.3.3 equation above: in the 800px parent, the content shrinks to 750px so the border box is exactly 800px. - Use
box-sizing: border-box. Thenwidth: 100%means the border box is 800px, and the content box is 750px — the same result.
.parent { width: 800px; }
.child { width: 100%; padding: 20px; border: 5px solid; } /* 850px - overflows */
.child { padding: 20px; border: 5px solid; } /* auto: 800px - fits */
.child { width: 100%; padding: 20px; border: 5px solid; box-sizing: border-box; } /* 800px */
What is the difference between content-box and border-box?
With box-sizing: content-box (the default), width and height set the content box, so padding and border are added on top.
With box-sizing: border-box, width and height set the border box, so the padding and border fit inside the size you wrote and the content gets smaller instead.
What the diagram shows, in plain text (.card { width: 200px; padding: 20px; border: 10px solid; }):
box-sizing: content-box— content 200px; rendered width 10 + 20 + 200 + 20 + 10 = 260px, which is 60px more than the width you set.box-sizing: border-box— rendered width 200px, exactly the width you set; the content shrinks to 200 — 60 = 140px.- Margins are outside both, so they never count toward
widthin either mode.
The W3C CSS Box Sizing Module Level 3 defines the two values. For content-box, sizes are applied to the content box
. For border-box:
Any <length-percentage> values in the sizing properties are applied to the border box, thus representing the box’s visually-apparent sizes: the padding and border of the box (but not its margins) are essentially laid out and drawn inside the used width and height, with the content box sized to fill the remaining space.
box-sizing isn’t inherited, and it changes every sizing property — width, height, min-width, max-width and even flex-basis, which is why flex: 1 columns with different padding come out unequal under the default.
Here’s how much the choice matters for one width: 200px:
| Padding + border on each side | content-box: rendered width | content-box: content | border-box: rendered width | border-box: content |
|---|---|---|---|---|
| 0 + 0 | 200px | 200px | 200px | 200px |
| 10px + 1px | 222px | 200px | 200px | 178px |
| 20px + 10px | 260px | 200px | 200px | 140px |
| 40px + 5px | 290px | 200px | 200px | 110px |
| 100px + 10px | 420px | 200px | 220px (can’t fit) | 0px |
Why does everyone use * { box-sizing: border-box }?
Developers put box-sizing: border-box on every element because then the width you write is the width you see, and adding padding or a border never breaks a layout. web.dev explains: Because the alternative box model can be more predictable, developers often add this rule to resets and normalizers
.
This is the common version:
*,
*::before,
*::after {
box-sizing: border-box;
}
The ::before and ::after lines matter because the * selector doesn’t match pseudo-elements, and box-sizing isn’t inherited, so they would stay content-box without them.
What happens when padding is bigger than the width with border-box?
When padding and border add up to more than the width under border-box, the content box shrinks to zero and the box grows past the width you set.
The W3C Box Sizing spec calculates the content size by flooring the result at zero (as the inner size of a box cannot be negative)
.
That’s the last row of the table above: width: 200px with 100px of padding and a 10px border on each side gives a 220px box with no room for content.
What is the difference between margin, padding and border?
Padding is space inside the border that belongs to the element, the border is the line around it, and margin is space outside the border that pushes other elements away.
The quick test: if the background colour should fill the space, or the space should be clickable as part of a button, it’s padding; if it’s a gap between two things, it’s margin. Our margin vs padding guide turns that test into four questions, with buttons, cards and nav links worked through.
| Question | Padding | Border | Margin |
|---|---|---|---|
| Inside or outside the border? | inside | is the edge | outside |
| Shows the background? | yes | yes, underneath it | never (always transparent) |
| Part of a link’s or button’s clickable area? | yes | yes | no |
| Can it be negative? | no | no | yes |
| Counts in width with border-box? | yes | yes | no |
| Can it collapse? | no | no | yes, vertical margins between blocks |
auto allowed? | no | no | yes — margin: 0 auto centers a block |
That last row is the classic centering trick — our guide to centering a div draws how two auto margins split the leftover space evenly.
Why does padding-top: 10% use the width, not the height?
Percentage padding and margin are always worked out from the width of the containing block — even on the top and bottom.
The W3C Box Model spec says percentages refer to logical width of containing block
. So padding: 10% inside a 600px-wide parent is 60px on all four sides, however tall the parent is.
That’s why old responsive-video tricks used padding-top: 56.25% to make a 16:9 box (9 / 16 = 0.5625). Today the aspect-ratio property does that job directly.
What is margin collapsing in CSS?
Margin collapsing is when the top and bottom margins of two block boxes touch and merge into one margin the size of the larger one, instead of adding up.
Honest moment: margin collapsing felt like CSS was quietly eating my spacing. Seeing that two touching margins simply share one gap — the bigger one — is what finally made it feel fair instead of spooky.
A box with margin-bottom: 30px above a box with margin-top: 20px leaves a 30px gap, not 50px. Our margin collapse deep dive works through ten stacks and nine fixes.
What the diagram shows, in plain text:
- Two block siblings: A has
margin-bottom: 30px, B hasmargin-top: 20px. The gap is max(30, 20) = 30px, not 50px. - Parent and first child: the child’s
margin-top: 24pxescapes a parent with no padding and no border, and pushes the whole parent down by 24px. Fix it withpadding-topor a border on the parent, ordisplay: flow-root. - The same boxes as flex or grid items: margins never collapse, so 30 + 20 = 50px.
The rule comes from CSS 2.1, section 8.3.1, which is still where the W3C Box Model Module points for the details:
In CSS, the adjoining margins of two or more boxes (which might or might not be siblings) can combine to form a single margin. Margins that combine this way are said to collapse, and the resulting combined margin is called a collapsed margin.
Negative margins follow their own sum. MDN’s Mastering margin collapsing says the result is the sum of the largest positive margin and the smallest (most negative) negative margin
. Here are six setups worked out:
| A margin-bottom / B margin-top | Rule used | Gap between A and B |
|---|---|---|
| 30px / 20px (normal blocks) | largest wins: max(30, 20) | 30px |
| 30px / 30px | equal: just one of them | 30px |
| 40px / -15px | largest positive + most negative: 40 — 15 | 25px |
| -10px / -25px | no positive margin, most negative wins | -25px (B overlaps A by 25px) |
30px / 20px, with an empty div between them that has margin: 20px 0 | all four margins collapse through the empty box | 30px |
| 30px / 20px as flex or grid items | no collapsing in flex or grid | 50px |
Why is my child’s margin-top pushing the parent down?
A first child’s margin-top pushes its parent down because the two top margins collapse: with nothing between the parent’s top edge and the child’s, the child’s margin moves outside the parent.
MDN lists exactly when that happens: The margin-top of a parent collapses with the margin-top of its first in-flow descendant unless the parent has a border-top, padding-top, contains any inline content (such as text), or has clearance applied.
The same goes for a last child’s margin-bottom.
Any one of these fixes keeps the margin inside the parent:
.parent { padding-top: 1px; } /* any padding separates them */
.parent { border-top: 1px solid transparent; }
.parent { display: flow-root; } /* new block formatting context */
.parent { display: flex; flex-direction: column; } /* flex never collapses */
display: flow-root is the cleanest: it doesn’t change how the parent looks, it only starts a new block formatting context.
CSS 2.1 says elements that do that do not collapse with their in-flow children
. An older way to get the same effect is overflow: hidden, which also clips anything that sticks out.
When don’t margins collapse?
Margins don’t collapse sideways, inside flex or grid containers, on floats or absolutely positioned boxes, or across a padding or border. In full:
- Horizontal margins. CSS 2.1:
Horizontal margins never collapse.
Left and right margins always add up. - Flex and grid items. MDN:
Margins don’t collapse in a container with display set to flex or grid.
If you build a vertical stack withdisplay: flexorgrid, usegapinstead of margins and the problem goes away — see our flexbox explainer and our CSS Grid guide. - Floats and absolutely positioned elements. MDN:
the margins of floating and absolutely positioned elements never collapse
. - Padding, a border, or content in between. Anything that sits between two margins keeps them apart.
- A new block formatting context.
display: flow-root,overflowother thanvisible, anddisplay: inline-blockall stop a box’s margins collapsing with its children. - The root element. CSS 2.1:
Margins of the root element’s box do not collapse.
Does the box model work the same on inline elements like span?
No — inline elements such as <span>, <a> and <strong> ignore width and height, and their top and bottom margins have no effect. CSS 2.1, section 10.6.1, sizes their height from the font, and section 8.3 says vertical margins don’t apply to non-replaced inline elements.
Left and right margins, padding and borders do work — but top and bottom padding and borders are only painted; they overlap the lines above and below instead of pushing them away.
To give an inline element real width, height and vertical spacing while keeping it in the line of text, use display: inline-block. To make it a full block, use display: block.
a.button {
display: inline-block; /* now width, height and margin-top work */
padding: 10px 16px; /* and this padding pushes other lines away */
}
Is outline part of the box model?
No — an outline is drawn outside the border but takes up no space at all, so it never changes the size or position of anything.
The W3C CSS Basic User Interface Module Level 4 says Outlines do not take up space
and that the outline is always on top and doesn’t influence the position or size of the box, or of any other boxes
.
That makes outline handy for two jobs: showing keyboard focus, and debugging.
A temporary * { outline: 1px solid red; } shows every box on the page without moving a single pixel — a border would add 2px to every element and shift the layout you’re trying to look at.
How do you see the box model in DevTools?
Every browser’s DevTools draws the box model of the selected element as four nested rectangles with the real numbers in them. web.dev notes that Browser developer tools provide a visualisation of a selected box’s box model calculations
.
- Chrome and Edge: right-click the element, choose Inspect, and look at the box model diagram. The Chrome DevTools CSS reference says:
To view the box model of an element, go to the Styles tab and click the Show sidebar button in the action bar.
It also appears in the Computed tab.To change a value, double-click on it.
- Firefox: open the Inspector’s Layout panel. The Firefox Box Model view shows which rule each value comes from when you hover it, and you can edit the values and see the page change straight away.
Hovering the element in either browser also tints the page itself: content blue, padding green, border yellow and margin orange — the same colours as our diagrams.
Does box-sizing work in every browser?
Yes — box-sizing, margin, padding and border work in every browser in use today, with no prefixes needed. The versions below are from MDN’s browser-compat-data, read on November 18, 2026:
margin,paddingandborder- Chrome 1, Edge 12, Firefox 1, Safari 1
box-sizing(unprefixed)- Chrome 10, Edge 12, Firefox 29, Safari 5.1 — older versions needed
-webkit-or-moz- margin-inlineandpadding-inline- Chrome 87, Edge 87, Firefox 66, Safari 14.1
margin-inline and padding-inline are the logical versions of left-and-right spacing: in English they mean left and right, and in a right-to-left or vertical language they follow the text direction instead.
Open DevTools on any page, click a heading, and find the little four-ring diagram with the real numbers in it. Then toggle box-sizing: border-box and watch those numbers rearrange — it is weirdly satisfying, and you will never guess at a width again.
Sources and further reading
- W3C: CSS Box Model Module Level 3 — Recommendation, 11 April 2024: margin, padding and the four box areas
- W3C: CSS 2.1, 8.3.1 Collapsing margins — the margin collapsing rules
- W3C: CSS 2.1, 10.3.3 Block-level, non-replaced elements in normal flow — the width equation
- W3C: CSS Box Sizing Module Level 3, box-sizing — content-box and border-box
- W3C: CSS Basic User Interface Module Level 4, outlines — outlines take up no space
- MDN Web Docs: Introduction to the CSS basic box model — the four areas and edges
- MDN Web Docs: Mastering margin collapsing — parent-child, empty blocks, negative margins, flex and grid
- MDN Web Docs: box-sizing — reference and examples
- web.dev Learn CSS: Box Model — the 142px example and the border-box reset
- Chrome DevTools: CSS features reference — viewing and editing the box model
- Firefox DevTools: Examine and edit the box model — the Box Model view
- MDN browser-compat-data — browser versions, read November 18, 2026
Questions and answers
Does padding add to width in CSS?
Yes, by default. With box-sizing: content-box, width sets only the content, so width: 200px plus 20px of padding on each side gives a 240px box. With box-sizing: border-box, padding fits inside the width instead and the content gets smaller.
Is margin included in the width of an element?
No. Margin is outside the border, so it is never part of width in either box-sizing mode. It does add to the space the element takes up, which is why two floated 50%-wide boxes with a margin no longer fit side by side.
What are the 4 parts of the CSS box model?
From the inside out: content, padding, border and margin. Content holds the text or child elements, padding is space inside the border, the border is the line around it, and margin is transparent space outside that separates the box from its neighbours.
Should I always use box-sizing: border-box?
For most projects, yes: with *, *::before, *::after { box-sizing: border-box; } at the top of your CSS, the width you set is the width you see. Add it at the start of a project, because switching an existing site changes the size of every box with padding or a border.
How do I stop margins from collapsing?
Put padding or a border between the margins, give the parent display: flow-root, or lay the boxes out with display: flex or grid, where margins never collapse. In flex and grid layouts you can also use gap for spacing instead of margins.