🎨 CSS Fundamentals

The CSS Box Model Explained: Margin, Border, Padding, and box-sizing

By Shubham Sharma··8 min read
Diagram of the CSS box model with content, padding, border, and margin layers

The very first CSS bug I ever spent an entire night on was a box-model bug — I just didn't know it. I had a .card set to width: 300px sitting inside a 300px column, and it kept spilling out and breaking the whole row on mobile. I blamed Bootstrap, then Flexbox, then my browser. The real culprit? The default box-sizing. Once someone on my team dropped one line — box-sizing: border-box — into the reset, every layout I'd ever written suddenly behaved. That's the kind of small thing that separates 'CSS is broken' from 'oh, that makes total sense.'

Every element on a page is really just a rectangle made of four layers: content, padding, border, and margin. Once you can picture those four layers in your head, honestly, about 90% of your CSS layout surprises disappear. Let me walk you through them the way I wish someone had explained it to me on day one.

The four layers from inside out

  • Content — the actual text or image
  • Padding — space inside the border
  • Border — the line around the padding
  • Margin — space outside the border, between elements

The default that trips everyone up

By default, width: 200px sets the content width. Padding and border are added on top, so the element actually takes 200px + padding + border on screen. That's the classic 'why is my layout overflowing?' bug.

/* default — width is content only */
.box { width: 200px; padding: 20px; border: 2px solid; }
/* actual rendered width: 244px */

The fix: box-sizing: border-box

*, *::before, *::after { box-sizing: border-box; }
.box { width: 200px; padding: 20px; border: 2px solid; }
/* actual rendered width: 200px */

Margin collapsing — the other gotcha

Vertical margins between block elements collapse into the larger of the two. Two stacked elements with margin-top: 20px and margin-bottom: 30px end up 30px apart, not 50px.

Inspecting the box model in DevTools

Open DevTools, select any element, and check the Computed panel. You'll see a colour-coded diagram with the exact pixel values for each layer — the fastest way to debug spacing issues.

Common mistakes

  • Forgetting box-sizing: border-box on inputs and buttons
  • Using padding when margin is correct (or vice versa)
  • Fighting margin collapse instead of using gap in flex/grid

The one habit that fixed this for me

In my engineering workflow now, the very first line of every new project's CSS is a universal box-sizing: border-box reset — before any variables, before any resets, before anything. It takes ten seconds and it saves you from the exact overflow bug that ate my evening as a junior. Do that, keep the four-layer picture in your head, and reach for DevTools the moment something looks 4 pixels off. That's really the whole game.

Frequently asked questions

What does box-sizing: border-box actually do?

It makes width and height include padding and border, so the element renders at exactly the size you set.

Do flexbox and grid still use the box model?

Yes. Every element on the page is still a box — flex and grid just control how those boxes are arranged.

External references

Enjoyed this article?

Share it with a fellow developer or explore more tutorials in our blog.

More articles

Related articles