CSSApril 2026 · 11 min read

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.

📦
Try the Box Model Visualizer
Free, no signup
DG
Derek Giordano
Designer & Developer
In this guide
01What Is the Box Model?02The Four Layers03content-box vs border-box04Margin Collapse05Debugging Box Model Issues06The Universal Box-Sizing Reset07Using DevTools08Common Layout Patterns09Box Model in Flexbox & Grid
⚡ Key Takeaways
  • Understand the CSS box model — content, padding, border, margin — plus box-sizing, margin collapse, and layout debugging techniques with interactive.
  • What Is the Box Model?.
  • The Four Layers.
  • Covers content-box vs border-box.
  • Covers margin collapse.

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:

💡 Tip
Use gap instead of margin hacks for flexbox spacing. It keeps layout code cleaner and is supported in all modern browsers.

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. It inherits the element's background color 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:

⚠ Warning
CSS gradients used as backgrounds cannot be animated with standard transitions. Use background-size animation or @property registered custom properties instead.

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 a single margin equal to the larger of the two — they don't add together.

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 actually intentional — it prevents doubled spacing between paragraphs and headings. But it catches developers off guard when a child's margin "leaks" out of its parent. The fix is usually adding padding or a border to the parent, or using overflow: hidden to create a new block formatting context.

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:

*, *::before, *::after {\n box-sizing: border-box;\n}

This ensures every element — including pseudo-elements — uses the intuitive sizing model. Every major CSS framework and reset (Normalize, Tailwind, Bootstrap) includes this rule. 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 `box-shadow: 0 0 0 2px #color` instead of `border` for focus rings — it doesn't change the element's size or trigger layout recalculation.

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 rather than collapsing. 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?+
The CSS box model is the set of rules that determines how every HTML element is rendered as a rectangular box with four layers: content (the actual text or media), padding (space between content and border), border (the visible edge), and margin (space separating the element from its neighbors).
What is the difference between content-box and border-box?+
In content-box (the CSS default), width and height apply only to the content area — padding and border are added on top. In border-box, width and height include padding and border, making layouts more predictable. Most developers use border-box globally.
Why do vertical margins collapse?+
CSS margin collapsing is an intentional behavior where adjacent vertical margins merge into the larger of the two, rather than adding together. This prevents doubled spacing between paragraphs and headings. Horizontal margins never collapse, and flex/grid children don't collapse either.
Does padding affect element size?+
It depends on box-sizing. With content-box, padding adds to the element's total width and height beyond the declared values. With border-box, padding is included inside the declared width and height.
How do I center a block element horizontally?+
Give the element a defined width (or max-width) and set margin: 0 auto. The auto value on left and right margins distributes remaining horizontal space equally.
Try it yourself

Use the Box Model Visualizer — free, no signup required.

⚡ Open Box Model Visualizer
DG
Derek Giordano
Written by the creator of Ultimate Design Tools. BA in Business Marketing.