box-sizing

Topic 2 of 4 in The box model5 min readBeginnerFree course

Overview

The previous lesson showed that, by default, width only sets the size of the content, and padding and border are added on top. That’s rarely what you want. One property fixes it: box-sizing.

The two models

With width: 300px, padding: 20px and border: 5px solid — content-box, the default, makes the box 350px wide with 300px of content. border-box makes the box 300px wide with 250px of content. Most resets start with *, *::before, *::after { box-sizing: border-box; }.

CSS
.box {
  width: 300px;
  padding: 20px;
  border: 5px solid;
}
content-box (default) border-box
width applies to the content only content + padding + border
Rendered width 300 + 40 + 10 = 350px 300px
Content width 300px 300 − 40 − 10 = 250px

These numbers are measured in Chrome. With border-box, the box is exactly as wide as you said; the padding and border are carved out of the inside.

Why border-box is easier

Designs are usually specified by their outer size: “the sidebar is 280px wide”, “each column is half the width”. With content-box, every time you add padding or a border, you have to recalculate the width:

CSS
/* content-box: two 50% columns with padding overflow the container */
.col { width: 50%; padding: 1rem; }   /* each is 50% + 2rem */

/* the old workaround */
.col { width: calc(50% - 2rem); padding: 1rem; }

With border-box, padding no longer changes the outer size, so the maths disappears:

CSS
.col { box-sizing: border-box; width: 50%; padding: 1rem; }   /* exactly 50% */

The universal reset

Because border-box is almost always what you want, nearly every modern stylesheet starts by applying it to everything:

CSS
*,
*::before,
*::after {
  box-sizing: border-box;
}
  • * matches every element.
  • ::before and ::after pseudo-elements aren’t matched by *, so they’re listed separately.

Some teams prefer an inheritable version, which lets a third-party widget opt back into content-box for its whole subtree:

CSS
html { box-sizing: border-box; }
*, *::before, *::after { box-sizing: inherit; }

Either is fine. CSS frameworks such as Tailwind include this reset by default, which is why width “just works” in them.

What border-box doesn’t change

  • Margins are never included. box-sizing only decides whether padding and border are inside or outside width. Margins always sit outside.
  • Content can still overflow. If padding and border add up to more than the width, the content area becomes zero, and the box grows to fit the padding and border. A 100px-wide border-box with 60px of padding on each side is 120px wide.
  • Elements without a set width aren’t affected in normal flow: a block with width: auto fills its container either way — auto already accounts for padding and border.

Do you still need widths?

Modern layout often avoids explicit widths altogether: flex items grow and shrink, grid tracks divide space with fr, and blocks fill their container by default. But you’ll still set sizes — a sidebar of 15rem, a max width for readable text, a fixed-size avatar — and border-box makes every one of them predictable.

Start every project with the reset. It’s one of the few CSS rules that’s genuinely universal. The rest of this course assumes it.

Exercise

Two columns that fit

Two .col elements are each given width: 50%, padding: 1rem and a 2px border, and displayed side by side with display: inline-block… but the second column drops to a new line. Explain why, then fix it using box-sizing (keep the padding and border). Finally, add a global rule so every element on the page uses the same box model, including ::before and ::after pseudo-elements.

HTML
<div class="row"><div class="col">Left</div><div class="col">Right</div></div>

<style>
  .col {
    display: inline-block;
    width: 50%;
    padding: 1rem;
    border: 2px solid #2d5bd7;
  }
  /* your code here */
</style>

Summary

  • content-box (the default) adds padding and border on top of width and height.
  • border-box makes width and height include padding and border, so the box is exactly the size you set.
  • Most projects apply box-sizing: border-box to every element and pseudo-element in their reset.
  • With border-box, the content area shrinks as padding grows — the outside size stays fixed.
  • Flex and grid often make fixed widths unnecessary, but border-box still makes any width you do set predictable.