Short answer
Width applies to the content box unless you set border-box, so padding and border are added on top. A 200px width with 20px of padding on each side is 240px wide under content-box when the border is 0. border-box keeps the stated width and shrinks the content instead. Margin stays outside either way.
Last verified against the sources listed below.
Key takeaways
- CSS2 says each box has content, padding, border, and margin areas.
- content-box is the default, so width does not include padding or border.
- A 200px width plus 20px padding on each side is 240px when the border is 0.
- web.dev's quiz adds a 10px border on each side and gets 260px.
- border-box puts padding and border inside the width you set. Margin stays outside.
How to use css box model on a real element means naming four areas, then deciding whether width includes padding and border. CSS lays out boxes. The size you type is only the content box until you say otherwise.
CSS2 says each box has a content area and optional padding, border, and margin. The CSS Box Model Module Level 3 editor’s draft of October 24, 2025 describes margin and padding as the properties that create that spacing. Treat the draft as work in progress.
The CSS2 terms and the MDN examples below are the stable picture.
What are the four areas?
Every block box is content, then padding, border, and margin.
| Area | What it holds | Property |
|---|---|---|
| Content | Text, image, or child boxes | width and height |
| Padding | Space inside the border, where the background shows | padding |
| Border | The frame around padding and content | border |
| Margin | Space outside the border, between this box and the next | margin |
MDN uses those four names. CSS2 adds the edges: the content edge, the padding edge, the border edge, and the margin edge, also called the outer edge. If padding is 0, the padding edge sits on the content edge.
Margin backgrounds are always transparent, so a gap between cards does not paint the card color.
web.dev puts the same stack in plain order. The content box holds the content. The padding box is the space from padding, and scrollbars live there when overflow scrolls.
The border box is the visible frame. The margin box is the gap outside. Outline and box-shadow paint in that outer space without changing the border edge.
How wide is content-box?
The default adds padding and border outside the width you set.
MDN says content-box is the initial value. Width and height are the content only. A width of 200px plus 20px of padding on the left and 20px on the right is 240px of border box when the border is 0.
Add a border and both sides count again.
web.dev uses that same 200px width and 20px padding, then adds a 10px border. Its quiz says the visible width is 260px: 200 plus 40 of padding plus 20 of border. MDN’s shorter example is a 350px width with a 10px border, which renders 370px wide under content-box.
The border-box version of that rule stays 350px, and the content area is 330px.
Margin is not part of that sum. MDN’s longer sample uses width 350px, height 150px, padding 25px, and border 5px. The border box is 410px wide and 210px tall.
The 10px margin sits outside. Four boxes set to width: 25% will not share one row if any of them has horizontal padding or a border.
What does border-box change?
border-box makes the declared width include padding and border.
MDN’s definition is direct. With border-box, width and height include content, padding, and border, and they still exclude margin. The content area cannot go negative.
It floors at 0.
Apply that to the 200px example. Width 200px, padding 20px on each side, and no border means the content is 160px and the border box is still 200px. Add web.dev’s 10px border on each side and the content shrinks further, to 140px, while the outer measurement stays 200px.
The number in the stylesheet matches the frame you see.
MDN also notes that browsers already use border-box for button, select, and textarea, and for inputs of type radio, checkbox, reset, button, submit, color, or search. A normal div or p does not get that default. You have to set it.
How do you set border-box?
Put one rule on the page, then check a real element.
- Decide that new elements should use border-box.
- Add the rule from MDN or the shorter rule from web.dev.
- Reload and select a padded box in the browser’s box diagram.
- Confirm the listed width matches the width you typed.
- Convert a pixel padding or width with the px to rem converter if the rest of the page uses rem.
MDN sets the switch on the root and lets the rest inherit. web.dev applies border-box to every element and to ::before and ::after in one rule. Either pattern stops a 25% column from overflowing when you add padding.
MDN says content-box can still be useful on a position: relative or absolute box when you want the offsets to ignore later padding and border edits.
Inline boxes are the other trap. MDN says a span ignores width, height, and vertical margins. Horizontal padding still pushes neighbors.
Top and bottom padding can overlap the line above. If you need a reliable width, set display: block or inline-block first, then size the box.
Where do people mis-count?
They add margin into the width, or they forget both sides.
| Mistake | What the docs say | What you see |
|---|---|---|
| Counting margin as width | Margin is outside the border edge | A gap, not a wider background |
| Adding padding once | Left and right both count | The row wraps too early |
| Assuming border-box on a div | The default for normal elements is content-box | The box is wider than the width property |
| Setting width 100% plus padding | content-box adds the padding | Horizontal scroll |
| Vertical padding on a span | Inline boxes do not push the next line | Overlap with the line above |
Check the box in devtools. web.dev lists that diagram as the way to see content, padding, border, and margin as separate numbers. If the content figure plus both paddings plus both borders equals the border box, you are on content-box.
If the border box equals the width you wrote, you are on border-box.
Frequently asked questions
Does margin count inside the width?
No. MDN says margin is outside the border, and CSS2 calls the margin edge the outer edge. Under content-box, width is the content, then padding and border are added, and margin is extra space beyond that. Under border-box, width includes content, padding, and border, and margin is still outside. A 20px margin on each side adds 40px of gap, not 40px of background.
What is the default box-sizing value?
content-box. MDN lists it as the initial value, and web.dev says user-agent styles also start elements at content-box. That is why a declared width grows when you add padding or a border. MDN notes an exception: browsers use border-box by default for many form controls, including buttons, checkboxes, radios, and search fields. A div does not get that exception.
How wide is 350px with a 10px border?
MDN's content-box example is a 350px width and a 10px solid border, which renders 370px wide because both sides of the border are added. The same declaration with border-box stays 350px, and the content area becomes 330px. If you also add padding, add both sides again under content-box, or subtract both sides from the content under border-box. Margin is still not in either total.
Should every element use border-box?
It is the usual page default because the number you type is the number you see. web.dev shows a universal rule on every element and on before and after boxes. MDN shows another pattern: set border-box on the html element and let other elements inherit it. Keep content-box only when you want a positioned box's offsets to ignore padding and border changes.
Do inline elements use the full model?
They use part of it. MDN says width, height, and top and bottom margins do not apply to inline boxes, while left and right padding, margins, and borders do affect neighbors. Top and bottom padding and borders can change the box size without pushing other lines, which can cause overlap. Block boxes use content, padding, border, and margin together.
Sources
- The box model · MDN, 2026
- box-sizing · MDN, 2026
- CSS2 box model · W3C, 2026
- The box model · web.dev, 2026
- CSS Box Model Module Level 3 · CSS Working Group, 2025