The Box Model: Content, Padding, Border, Margin and box-sizing
How every element is a box: the content, padding, border and margin areas, what width and height measure, box-sizing content-box versus border-box and the universal rule, form control defaults, inline boxes, and inspecting boxes in DevTools.
kvi5hnuCode Sprout team member7 min readOn this page
Every element on a page generates a rectangular box. Layout in CSS is the arrangement of these boxes, so knowing exactly what makes up a box, and what width and height measure, explains most "why is this 22 pixels too wide?" problems.
The four areas

From the inside out:
| Area | Set by | Notes |
|---|---|---|
| content | width, height, or the content itself | text, images, child boxes |
| padding | padding | space inside the border; the background shows through it |
| border | border | drawn between padding and margin |
| margin | margin | space outside the border; always transparent |
Each area's outer edge has a name: the content box, padding box, border box and margin box. These names appear as values in several properties, such as background-clip: padding-box (article 48), clip-path: inset(0) margin-box and shape-outside.
- Backgrounds fill the content, padding and border areas by default, so they show through dashed or dotted borders.
- Margins are transparent: the parent's background shows through them.
- Outlines (article 42) are drawn outside the border but take up no space: they aren't part of the box model, and never change layout.
.card {
width: 20rem;
padding: 1.5rem;
border: 1px solid #e3d9c9;
margin-block-end: 2rem;
}
What width and height measure
By default, width and height set the size of the content area only. Padding and border are added on top:
| Declared | Total border-box width |
|---|---|
width: 300px; padding: 20px; border: 5px solid | 300 + 2 × 20 + 2 × 5 = 350px |
Margins add space around that, but don't change the box's own size. That default is called content-box sizing, and it makes layouts fragile: two columns of width: 50% with any padding no longer fit side by side.
box-sizing: border-box
box-sizing changes what width and height (and their logical forms inline-size and block-size) refer to:
| Value | width sets | Padding and border |
|---|---|---|
content-box (initial) | the content area | added outside |
border-box | the border box | taken from inside, shrinking the content |
body { font: 0.9rem system-ui, sans-serif; margin: 1rem; }
.ruler { width: 240px; height: 6px; background: #8a3b12; margin-bottom: 0.5rem; }
.box { width: 240px; padding: 20px; border: 5px solid #2f7d4f; background: #e9f5ee; margin-bottom: 1rem; }
.border-box { box-sizing: border-box; }

In the right panel, the text input overflows because browsers give it content-box while the select and button already use border-box.
With border-box, the size you write is the size you see, which is what most people expect. Since padding is inside it, width: 50% columns with padding still fit. The content can't become negative, though: if padding and border are larger than the width, the box grows to fit them.
The universal rule
Nearly every project starts with this rule, part of most resets:
*, *::before, *::after {
box-sizing: border-box;
}
Pseudo-elements are listed separately because * doesn't match them. A variation sets border-box on html and box-sizing: inherit on everything else, so a third-party widget that expects content-box can switch its whole subtree back with one rule. Either works; the direct version is simpler.
Add the rule at the start of a project. Adding it to an existing stylesheet changes the size of every padded or bordered element with a set width, so check the layout afterward.
Form controls
Browsers already give some form controls border-box: in Chrome, buttons, checkboxes, radio buttons, selects, search inputs, meters and progress bars use it, while text inputs and textareas use content-box. That inconsistency is one more reason for the universal rule: it makes width: 100% inputs and buttons line up.
Boxes of inline elements
Everything above applies to block boxes, such as paragraphs and <div>s. Inline boxes, such as <span>, <a> and <strong>, behave differently (article 45):
widthandheightdon't apply to them.- Horizontal padding, borders and margins push the neighboring text away.
- Vertical padding and borders are painted, but don't move the lines above and below: the line height decides the line spacing. Vertical margins have no effect.
.highlight { padding: 0.1em 0.3em; background: #fff1c2; } /* fine: mostly horizontal */
.badge { display: inline-block; padding: 0.3em 0.6em; } /* needs inline-block for vertical space */

The inline version's vertical padding and margin don't change the line spacing, so the highlight runs into the neighboring lines.
display: inline-block makes an element flow inline like text, while its box behaves like a block: width, height and vertical padding and margins all work.
Replaced elements such as <img> and <input> are inline but have their own size, so width and height apply to them.
Inspecting boxes
Browser DevTools show every element's box model: select an element, and the Computed panel displays a diagram with the content size, padding, border and margin, and highlights each area on the page as you hover. Also useful:
outline: 1px solid redon*temporarily draws every box without changing layout, unlike borders.element.getBoundingClientRect()returns the border box's size and position;offsetWidthis the border-box width, andclientWidththe padding-box width without scrollbars.
What's current
box-sizinghas worked in every browser for over a decade. Onlycontent-boxandborder-boxexist; an oldpadding-boxvalue was removed.- Logical sizes
inline-sizeandblock-size, and logical padding and margins such aspadding-inline, followbox-sizingthe same way (article 47).
Common questions
Why is my 300px box 342px wide?
It uses content-box sizing, so its padding and borders are added to the 300px. Use box-sizing: border-box.
Do margins count in border-box?
No. border-box includes padding and border, never margins. Margins are always outside the box.
Why *, *::before, *::after and not just *?
The universal selector doesn't match pseudo-elements, which generate boxes too.
Why doesn't vertical padding on my link add space between lines?
Links are inline. Vertical padding on inline boxes is painted but doesn't affect line layout. Use display: inline-block, or increase line-height.
Is border-box slower or worse in any way?
No. It only changes what the numbers mean.
Summary
- Every element's box has content, padding, border and margin areas. Backgrounds fill the first three, and margins are transparent.
- Outlines are drawn outside the border and never affect layout.
- With the default
content-box,widthandheightset the content size, and padding and borders are added. - With
border-box, they set the border-box size, and padding and borders are inside it. - Start every project with
*, *::before, *::after { box-sizing: border-box; }. - Inline boxes ignore
widthandheight, and their vertical padding and margins don't move lines.inline-blockchanges that. - DevTools show the box model of any element.
Practice questions
An element has the rule below, with content-box sizing. How wide is its border box, and how much horizontal space does it take?
.box {
width: 400px;
padding: 10px 30px;
border: 2px solid;
margin: 0 20px;
}
Answer
The border box is 400 + 2 × 30 + 2 × 2 = 464px. With the margins, it takes 464 + 2 × 20 = 504px.
The same element uses box-sizing: border-box. What is its content width?
Answer
400 − 2 × 30 − 2 × 2 = 336px. The border box stays 400px.
Two columns use the rule below, and the second drops below the first. Why, and what's the fix?
.column {
width: 50%;
padding: 1rem;
float: left;
}
Answer
With content-box, each column is 50% plus 2rem of padding, so the two add up to more than 100%. With box-sizing: border-box, the padding is inside the 50%, and they fit.
A <span class="tag"> with padding: 0.5em overlaps the lines above and below it. Why, and how do you make it take up vertical space?
Answer
Vertical padding on an inline box is painted but doesn't affect line layout. Add display: inline-block, so the padding is part of the box's height in the line.
Previous: Color Contrast and Accessibility: WCAG Ratios, contrast-color() and Forced Colors
Next: Sizing Boxes: width, height, Logical Sizes, min/max and Intrinsic Sizes

More in CSS Course
Quiz: CSS Delicate Details
Twenty-five questions on the edge cases that surprise experienced developers: invalid custom properties, shorthands that reset values, layer order with !important, containing blocks, margin collapsing, specificity of :is() and :where(), intrinsic minimum sizes, and more, each checked in Chrome.
Quiz: CSS Interview Questions
Thirty front-end interview questions: cascade, specificity and layers, box model and margin collapsing, stacking contexts, positioning, flexbox and grid, responsive design, custom properties, performance, accessibility, modern selectors and architecture.
Quiz: CSS Course Final Exam
Thirty questions across the whole CSS course: syntax and error handling, selectors, the cascade and layers, values and units, color, the box model, backgrounds, positioning, flexbox, grid, responsive design, typography, generated content, effects, transforms, motion and architecture.
Quiz: CSS Architecture, Tooling and Debugging (CSS Part 17)
Twenty questions on modular CSS and BEM, layering a stylesheet, design tokens and theming, pattern libraries and safe refactoring, preprocessors and build tools, the at-rules reference, and debugging common CSS problems.
Discussion
Sign in to ask a question or share what you learned.