← Back to HTML & CSS, page 1

html-css-0011 · Easy fresher

What is the CSS box model?

Every element is a box made of content, padding, border and margin. By default width sets only the content box, so padding and border make the box larger. box-sizing: border-box makes width include padding and border, which is why most projects apply it to all elements.

Full answer

Every element on a page is drawn as a rectangular box. The box has four layers, from the inside out: content, padding, border and margin.

Think of a parcel on a shelf. The item is the content. The bubble wrap around it is the padding. The carton is the border. The empty space between your carton and the next one is the margin.

By default width and height set the size of the content box only (box-sizing: content-box). Padding and border are added on top, so the box ends up larger than the number you wrote. With box-sizing: border-box, width covers content, padding and border together, which matches how most people count.

Running this in Chrome gives an outer width of 250px for the first box and 200px for the second. The usable inner width (content plus padding) is 240px and 190px.

Most projects add one reset so every element behaves the same way:

html
<style>
  *, *::before, *::after { box-sizing: border-box; }
  .card { width: 100%; padding: 1rem; border: 1px solid #999; }
</style>
<div class="card" id="card">With border-box, 100% width plus padding does not overflow.</div>

Margin is outside the box. It has no background colour of its own, and vertical margins between block siblings can collapse into one. An outline is drawn outside the border but is not part of the box model, so it never changes layout.

html
<style>
  .box { width: 200px; padding: 20px; border: 5px solid black; margin-bottom: 10px; }
  .fixed { box-sizing: border-box; }
</style>
<div class="box" id="a">content-box: 200 + 40 + 10 = 250px wide</div>
<div class="box fixed" id="b">border-box: 200px wide in total</div>

Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.

Key points

  • The layers are content, padding, border and margin, from inside to outside.
  • content-box: width is the content only. border-box: width includes padding and border.
  • A common reset is *, *::before, *::after { box-sizing: border-box }.
  • Vertical margins between blocks can collapse.
  • Outline is not part of the box model and does not affect layout.

Common mistakes

  • setting width: 100% on an element with padding and border while still on content-box. The element becomes wider than its parent and causes a horizontal scrollbar.
  • expecting margin to be clickable or to carry the background colour. Only padding and content show the background.

Follow-ups

  • what is margin collapsing?
  • How does box-sizing affect an element with width: 100% and padding?
  • Is padding or margin better for spacing inside a clickable area?

Tips

  • Open DevTools, select an element and look at the box diagram in the Computed tab. It shows the exact numbers for all four layers.
  • Do the arithmetic aloud: 200 + 40 + 10 = 250. Interviewers like to hear a number.
  • Mention the reset and why it makes percentage widths with padding safe.