# The Box Model & box-sizing

> Every element is a box. Content, padding, border, and margin layer outward, and box-sizing controls what width actually measures.

Subject: CSS | Lesson 6 of 44 | 5 min read
Interactive lesson: https://codewithanimation.in/css/box-model-and-box-sizing
Previous: [The Cascade, Specificity & Inheritance](https://codewithanimation.in/css/cascade-specificity-inheritance.md) | Next: [CSS Units: px, rem, em, %, viewport](https://codewithanimation.in/css/units-and-values.md)

## Four layers around every element

The **content area** is the innermost rectangle: where text and child elements live. Its dimensions are set by `width` and `height`.

**Padding** is transparent space between the content and the border. It inherits the element's background. **Border** is a line (with its own color, style, and width) drawn outside the padding. **Margin** is transparent space outside the border, separating the element from its neighbors. Margins do not take the element's background.

In the default `box-sizing: content-box` model, the `width` and `height` you set apply to the content area alone. Total rendered width = `width + padding-left + padding-right + border-left + border-right`. This makes arithmetic painful.

## box-sizing: border-box makes sizing predictable

With `border-box`, `width` includes padding and border. If you set `width: 300px; padding: 20px; border: 2px solid`, the element is still exactly 300px wide. The content area shrinks to 256px to accommodate padding and border.

This is why nearly every modern CSS reset (and Tailwind) applies `*, *::before, *::after { box-sizing: border-box; }` globally. You almost never want `content-box` on anything that has both padding and a fixed width.

## Working with the box model

Set padding and margin individually (`padding-top`, `margin-left`) or with shorthand. Shorthand accepts 1-4 values: `padding: 8px` (all sides), `padding: 8px 16px` (top/bottom, left/right), `padding: 8px 16px 4px 12px` (top, right, bottom, left, clockwise from top).

Use `box-sizing: border-box` via a global reset. Then `width: 100%` means the full parent width including padding, exactly as you would expect.

**Margin collapse:** when two block elements stack vertically, their adjacent margins collapse into one (the larger of the two). This does not happen in flex or grid containers, or when there is padding or border between parent and child.

**Outline** is similar to border but draws outside the element without affecting layout. It does not participate in the box model and will not push other elements around. Use it freely for focus rings.

## Common mistake: Adding padding to a 100%-wide element without border-box

The classic box-model trap: you set `width: 100%; padding: 20px` with `content-box`, and the element becomes 100% + 40px wide, causing horizontal overflow. The fix is either `box-sizing: border-box` or `width: calc(100% - 40px)`. Apply `border-box` globally and you never face this.

## Tip: Use the browser DevTools box model diagram

In Chrome or Firefox DevTools, selecting an element shows a color-coded box model diagram in the Computed panel: margin (orange), border (black), padding (green), content (blue). Hover each segment to highlight it in the viewport. This is the fastest way to debug unexpected element sizes.

## Try it: Try it

Drag the padding, border, and margin sliders and watch the box model layers resize. Toggle border-box to see how it reflows.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/css/box-model-and-box-sizing

## Check yourself

### Question 1

An element has `width: 200px; padding: 20px; border: 5px solid black; box-sizing: content-box`. What is its total rendered width?

- A. 250px
- B. 200px
- C. 160px
- D. 245px

Correct answer: A

With `box-sizing: content-box`, `width` sets the content area only. Total = content width + horizontal padding + horizontal border = 200 + 40 + 10 = 250px.

- Why not B: With `content-box`, padding and border are added on top of `width`. 200 + 20 + 20 + 5 + 5 = 250px.
- Why not C: 160px would be if padding were subtracted FROM `width`. That is what `border-box` does to the content area, not the total width.
- Why not D: Remember to count BOTH sides: padding-left + padding-right = 40px, border-left + border-right = 10px. Total = 200 + 40 + 10 = 250px.

### Question 2

Same element, now with `box-sizing: border-box`. What is its total rendered width?

- A. 200px
- B. 250px
- C. 160px
- D. Depends on browser

Correct answer: A

With `box-sizing: border-box`, `width: 200px` is the total rendered width. Padding and border are included. The content area is 200 - 40 - 10 = 150px.

- Why not B: 250px is the `content-box` answer. With `border-box`, padding and border are included in the declared `width`.
- Why not C: 160px is the content area width (200 - 40px padding), but the total rendered width remains 200px.
- Why not D: `box-sizing: border-box` is fully standardized and consistent across all modern browsers.

### Question 3

Two vertically stacked block elements: the top has `margin-bottom: 24px`, the bottom has `margin-top: 16px`. What is the gap between them?

- A. 24px (margins collapse)
- B. 40px (margins add)
- C. 16px
- D. 0px

Correct answer: A

Margin collapse: adjacent vertical margins between sibling block elements merge into a single margin equal to the larger of the two. 24px > 16px, so the gap is 24px. This does not occur in flex or grid containers.

- Why not B: Adjacent vertical margins between block elements collapse to the larger value, not the sum.
- Why not C: Collapsed margins take the larger value, which is 24px (not the smaller 16px).
- Why not D: Margins do not cancel out. They collapse to the larger of the two values.

## Remember this

- Box model layers outward: content + padding + border + margin.
- `content-box` (default): `width` = content only. Total = width + padding + border.
- `border-box`: `width` = content + padding + border. The total IS the `width`.
- Adjacent vertical block margins collapse to the larger value. Does not happen in flex/grid.
