CSS Box Model & Layout

Difficulty: Beginner

Question

Explain the CSS box model. What is the difference between content-box and border-box, and what is margin collapsing?

Answer

Every element in CSS is rendered as a rectangular box made of four layers: content, padding, border and margin. The box-sizing property decides what width and height measure: with the default content-box they size only the content, so padding and border are added on top; with border-box they include padding and border, so the box is exactly the width you set.

The four layers, inside out

  • Content: where text and children live. Sized by width/height (under content-box).
  • Padding: space inside the border. Takes the element's background colour.
  • Border: the visible edge. Its width counts towards the box size.
  • Margin: transparent space outside the border that pushes other boxes away. Margins can be negative; padding cannot.

Open DevTools, select an element and look at the box diagram in the Computed tab to see all four with real pixel values.

content-box vs border-box

content-box (default)border-box
width coversContent onlyContent + padding + border
width: 200px; padding: 20px; border: 5px renders as250px wide200px wide (content shrinks to 150px)
Mental modelAdd everything upWhat you set is what you get
Typical useLegacy defaultAlmost every modern reset

border-box makes layouts predictable. A width: 50% column with padding still fits two per row, and a width: 100% input with padding no longer overflows its container. That is why most resets (Tailwind's preflight, modern-normalize and others) apply box-sizing: border-box to every element and its pseudo-elements.

Margin collapsing

In normal block flow, vertical margins between blocks can merge into one margin equal to the larger of the two (or the sum if one is negative). It happens in three cases:

  1. Adjacent siblings: margin-bottom: 20px on one paragraph and margin-top: 30px on the next give a 30px gap, not 50px.
  2. Parent and first/last child: if the parent has no border, padding or inline content separating them, a child's top margin "leaks" out and appears above the parent.
  3. Empty blocks: an element with no height, padding or border collapses its own top and bottom margins.

Collapsing does not happen for horizontal margins, for flex or grid items, for floated or absolutely positioned elements, or across a new block formatting context. display: flow-root on the parent is the cleanest way to stop the parent-child case.

Gotchas worth mentioning

  • Inline elements like <span> ignore vertical margins and width/height; set display: inline-block or block if you need them.
  • Percentage padding and margin, even top and bottom, are calculated from the containing block's width.
  • outline and box-shadow do not take space in the box model, which is why they are safe for focus rings and hover effects.
  • Many teams now use gap in flex and grid layouts instead of margins between children, which sidesteps collapsing entirely.
The CSS box, outside in. Margin sits outside the border; with border-box, width stops at the border.
  1. Margin: Transparent space that pushes neighbours away.
  2. Border: The visible edge; counts toward border-box width.
  3. Padding: Inner spacing that shows the background.
  4. Content: Text and children; sized by width under content-box.

Code examples

content-box vs border-box

.card-a {
  box-sizing: content-box; /* the default */
  width: 200px;
  padding: 20px;
  border: 5px solid #333;
  /* Rendered width: 200 + 20*2 + 5*2 = 250px */
}

.card-b {
  box-sizing: border-box;
  width: 200px;
  padding: 20px;
  border: 5px solid #333;
  /* Rendered width: 200px, content area shrinks to 150px */
}

/* The reset almost every project uses */
*, *::before, *::after {
  box-sizing: border-box;
}

With border-box the number you write is the number you see on screen, which is why it is applied globally in nearly every modern codebase.

When margins collapse and how to stop it

/* 1. Siblings: gap is 30px, not 50px */
.intro   { margin-bottom: 20px; }
.details { margin-top: 30px; }

/* 2. Parent/child: child's margin leaks above .panel */
.panel h2 { margin-top: 24px; }

/* Fix: give the parent its own block formatting context */
.panel { display: flow-root; }

/* 3. Flex and grid items never collapse */
.stack {
  display: flex;
  flex-direction: column;
  gap: 16px; /* prefer gap over margins between children */
}

Collapsing is a block-flow behaviour only. Creating a new formatting context with flow-root, or switching to flex or grid with gap, removes the surprise.

Key points

  • Box = content + padding + border + margin; outline and box-shadow take no space.
  • content-box (default) adds padding and border on top of width; border-box includes them.
  • Apply box-sizing: border-box to *, *::before and *::after in your reset.
  • Only vertical margins collapse, and only in normal block flow.
  • Flex items, grid items, floats and absolutely positioned elements do not collapse margins.
  • Percentage padding and margin are resolved against the containing block's width.

Common follow-up questions

  • Why does a child's margin-top sometimes push the parent down instead?
  • What is a block formatting context and how do you create one?
  • Why can't you set a vertical margin on a span?
  • How would you build a full-width input with padding that never overflows?

Interview tips

  • Do the arithmetic out loud (200 + 40 + 10 = 250) to show you really understand content-box.
  • Mention margin collapsing even if not asked; it is the follow-up interviewers love.
  • Say how you would debug it: the DevTools box model panel shows every layer in pixels.

Concepts covered

Box model, box-sizing, content-box, border-box, Margin collapsing

More HTML & CSS Interview questions