CSS Box Model Explained: Content, Padding, Border & Margin
Understand the CSS box model — content, padding, border, margin — plus box-sizing, margin collapse, and layout debugging techniques with interactive examples.
What Is the Box Model?
Every HTML element is a rectangular box. The CSS box model describes the four concentric layers that make up that rectangle: content, padding, border, and margin. Understanding these layers — and how they interact — is the foundation of every CSS layout.
When you set width: 300px on a div, you might expect the element to be exactly 300 pixels wide on screen. But depending on your box-sizing mode, padding and border can push the total width well beyond 300px. This is the single most common source of layout bugs in CSS, and the box model is the key to understanding why.
The Four Layers
The box model has four distinct layers, each with its own role:
Content is the innermost layer — the space where text, images, and child elements live. The width and height properties control this area (in content-box mode).
Padding is the transparent space between the content edge and the border. The element's background extends under it and is set with padding, padding-top, padding-right, padding-bottom, and padding-left.
Border wraps the padding and content. It has its own thickness, style, and color, configured with border, border-width, border-style, and border-color.
Margin is the outermost layer — the transparent space between this element's border and its neighbors. Unlike padding, margin never has a background color. Margins can also be negative, pulling elements closer together or overlapping them.
content-box vs border-box
The box-sizing property controls how width and height are calculated:
With content-box (the CSS default), width and height apply only to the content area. Padding and border are added on top. A 300px-wide element with 20px padding and a 2px border becomes 344px total (300 + 20×2 + 2×2).
With border-box, width and height include padding and border. The same 300px-wide element stays exactly 300px — the browser subtracts padding and border from the content area automatically.
border-box is almost universally preferred for layout work because it makes widths predictable. When you set an element to 50% width, it actually takes up 50% of its parent — regardless of padding or border.
Margin Collapse
One of CSS's most confusing behaviors is margin collapsing: when two vertical margins touch, they merge into one. With two positive margins the result is the larger of the two, not their sum; if one margin is negative, the result is the largest positive margin plus the most negative one (CSS 2.2, section 8.3.1). In our Chromium test, 30px and 20px collapsed to a 30px gap, while 30px and −10px gave 20px.
This only happens with vertical margins (top and bottom), never horizontal ones. It occurs between adjacent siblings, between a parent and its first/last child (if there's no padding or border separating them), and even on empty elements whose top and bottom margins touch.
Margin collapsing is intentional — it prevents doubled spacing between paragraphs and headings. It catches developers off guard when a child's margin "leaks" out of its parent: in our test a parent containing a 10px-tall child with a 25px top margin measured just 10px tall, with the margin pushed outside it. Giving the parent display: flow-root (made for exactly this: it creates a new block formatting context with no side effects) contained the margin and the parent grew to 35px. Padding or a border on the parent works too; overflow: hidden does, but it also clips shadows and dropdowns.
Debugging Box Model Issues
When an element isn't the size you expect, the debugging process is straightforward. Open DevTools, select the element, and look at the box model diagram. It shows the computed content, padding, border, and margin values in a visual stack.
The most common issues are: unexpected total width from content-box sizing, margin collapse causing elements to be closer or farther apart than expected, percentage widths not accounting for padding, and auto margins not working because the element lacks a defined width.
Check the computed box-sizing value first — it explains almost every sizing surprise. Then verify whether margins are collapsing by inspecting the computed margin values on adjacent elements.
The Universal Box-Sizing Reset
The modern best practice is to set border-box globally at the start of every stylesheet:
This ensures every element — including pseudo-elements — uses the intuitive sizing model. Tailwind's Preflight and Bootstrap 5's Reboot both include it. Normalize.css does not: version 8.0.1 sets box-sizing only on hr (content-box), legend and checkboxes/radios (border-box), so if you use Normalize you add the global rule yourself. If you're starting a project from scratch, this should be the first line in your CSS.
Using DevTools to Inspect the Box Model
Every modern browser includes a box model inspector. In Chrome and Edge, select an element in the Elements panel and the box model diagram appears in the Styles sidebar. Firefox has a dedicated "Box Model" tab that's even more detailed, showing each layer with pixel values.
Hover over any box model value to highlight that specific layer on the page. Click a value to edit it live. This is the fastest way to diagnose spacing issues — you can see exactly where unexpected space is coming from.
Common Layout Patterns
Centering a block element: Set a defined width and margin: 0 auto. The auto margins distribute remaining space equally on both sides.
Full-bleed sections: Use negative margins equal to the parent's padding to break out of a container: margin-left: -24px; margin-right: -24px; padding-left: 24px; padding-right: 24px.
Equal-height columns: Flexbox and Grid automatically equalize heights, but padding differences between columns can create visual imbalance. Use consistent padding tokens from a design system.
Outline without layout shift: Use outline (with outline-offset) or box-shadow: 0 0 0 2px #color instead of border for focus rings — neither changes the element's size. Pair a box-shadow ring with outline: 2px solid transparent, because forced-colors mode removes box-shadows.
Box Model in Flexbox & Grid
Flexbox and Grid change some box model behaviors. Margin collapsing does not occur between flex or grid children — their margins always add up. In the same test, the 30px and 20px margins that collapsed to 30px between blocks gave a 50px gap inside a column flex container. This is actually one of the reasons many developers prefer flex layouts for vertical spacing.
The gap property in Flexbox and Grid provides spacing between children without margins at all, which is cleaner and more predictable. When you use gap: 16px, there's no need to manage margin on individual children or worry about removing margin from the last item.
In Grid layouts, fr units distribute available space after padding and gap are subtracted — which means your column widths always add up correctly without manual math.
Frequently Asked Questions
What is the CSS box model?
What is the difference between content-box and border-box?
Why do vertical margins collapse?
Does padding affect element size?
How do I center a block element horizontally?
- [1]W3C CSS 2.2: 8.3.1 Collapsing margins (negative margins rule) ↗
- [2]W3C CSS Display Level 3: display: flow-root ↗
- [3]W3C CSS Box Sizing Level 3: box-sizing ↗
- [4]normalize.css 8.0.1 source (box-sizing only on hr, legend, checkbox/radio) ↗
- [5]Bootstrap 5.3 Reboot (global border-box) ↗
- [6]Tailwind CSS Preflight ↗