Learn CSS Visually - CSS explained one diagram at a time - flexbox, grid, the box model and more

The CSS Box Model, Finally Explained Visually

By · Updated

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.

To-scale diagram of the CSS box model for a box with width 300px, height 100px, padding 20px, a 5px border and a 30px margin: a blue content box of 300 by 100 pixels, a green padding ring, a yellow border ring and a hatched orange, transparent margin ring, with measuring arrows showing the content box at 300px, the padding box at 340px, the border box at 350px and the margin box at 410px.
The box model in one picture: content (blue), padding (green), border (yellow) and margin (hatched, always transparent), drawn 1 unit per CSS pixel.

What the diagram shows, in plain text (.box { width: 300px; height: 100px; padding: 20px; border: 5px solid; margin: 30px; }):

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. width and height size 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 sizes for width: 300px; height: 100px; padding: 20px; border: 5px; margin: 30px — calculated by Learn CSS Visually from the W3C CSS Box Model and Box Sizing specs, November 18, 2026
BoxWhat it includesWith content-box (the default)With border-box
Content boxcontent only300 x 100px250 x 50px
Padding boxcontent + padding340 x 140px290 x 90px
Border boxcontent + padding + border (what you see)350 x 150px300 x 100px
Margin boxeverything + margin (the space it takes)410 x 210px360 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 width at auto. A block box with width: auto fills 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. Then width: 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.

To-scale comparison of a card with width 200px, padding 20px and a 10px border: with box-sizing content-box the content is 200 pixels and the rendered box is 260 pixels wide, 60 pixels wider than the width set; with box-sizing border-box the rendered box is exactly 200 pixels and the content shrinks to 140 pixels.
Same CSS, different width: content-box adds padding and border on top of width, border-box fits them inside it.

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 width in 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:

width: 200px under content-box vs border-box for five padding and border setups — calculated by Learn CSS Visually from the W3C CSS Box Sizing Module Level 3, November 18, 2026
Padding + border on each sidecontent-box: rendered widthcontent-box: contentborder-box: rendered widthborder-box: content
0 + 0200px200px200px200px
10px + 1px222px200px200px178px
20px + 10px260px200px200px140px
40px + 5px290px200px200px110px
100px + 10px420px200px220px (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.

Margin, border and padding side by side — compiled by Learn CSS Visually from the W3C CSS Box Model Module Level 3 and CSS 2.1, November 18, 2026
QuestionPaddingBorderMargin
Inside or outside the border?insideis the edgeoutside
Shows the background?yesyes, underneath itnever (always transparent)
Part of a link’s or button’s clickable area?yesyesno
Can it be negative?nonoyes
Counts in width with border-box?yesyesno
Can it collapse?nonoyes, vertical margins between blocks
auto allowed?nonoyes — 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.

To-scale diagram of margin collapsing in three panels: two stacked block siblings with a 30 pixel bottom margin and a 20 pixel top margin leave a 30 pixel gap because the margins overlap; a child with margin-top 24px inside a parent with no padding or border pushes its 24 pixel margin outside the parent, moving the whole parent down; and the same two boxes as flex or grid items keep both margins for a 50 pixel gap.
Margin collapsing in one picture: siblings share one margin, a first child’s margin escapes its parent, and flex or grid items never collapse.

What the diagram shows, in plain text:

  • Two block siblings: A has margin-bottom: 30px, B has margin-top: 20px. The gap is max(30, 20) = 30px, not 50px.
  • Parent and first child: the child’s margin-top: 24px escapes a parent with no padding and no border, and pushes the whole parent down by 24px. Fix it with padding-top or a border on the parent, or display: 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:

The gap between two stacked boxes A and B — calculated by Learn CSS Visually from CSS 2.1 section 8.3.1 and MDN’s margin collapsing guide, November 18, 2026
A margin-bottom / B margin-topRule usedGap between A and B
30px / 20px (normal blocks)largest wins: max(30, 20)30px
30px / 30pxequal: just one of them30px
40px / -15pxlargest positive + most negative: 40 — 1525px
-10px / -25pxno positive margin, most negative wins-25px (B overlaps A by 25px)
30px / 20px, with an empty div between them that has margin: 20px 0all four margins collapse through the empty box30px
30px / 20px as flex or grid itemsno collapsing in flex or grid50px

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 with display: flex or grid, use gap instead 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, overflow other than visible, and display: inline-block all 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, padding and border
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-inline and padding-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

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.

How this was written: Written from the W3C CSS Box Model Module Level 3 (Recommendation, 11 April 2024), the CSS 2.1 specification (sections 8 and 10.3.3, still the rules for margin collapsing and block widths), the W3C CSS Box Sizing Module Level 3 (box-sizing) and CSS Basic User Interface Module Level 4 (outline), MDN Web Docs’ box model and margin collapsing guides, the box model module of web.dev’s Learn CSS course, the Chrome and Firefox DevTools documentation and MDN’s browser-compat-data, all read on November 18, 2026. Every size in the diagrams and tables is our own arithmetic from those rules. Code samples are simplified for learning.

Oh, one more thing…

If this helped, you’ll like our free updates: the insights we discover while researching, and the moments worth sharing with other people learning CSS — no spam, no promotions.

One short email when there’s something worth your time. Unsubscribe any time.