Skip to content
Tutorials Center
My learning

Topics

Lesson 29 of 139 ยท The Box Model

box-sizing

Learn how box-sizing changes what width and height measure, why border-box is easier to work with, and how to apply the universal border-box reset safely.

In the previous lesson you saw that width: 200px plus some padding and a border gives you a box wider than 200 pixels. That is the browser's default, and it makes everyday layout harder than it needs to be. Want two columns at 50% each with some padding? They will not fit side by side.

The box-sizing property fixes this. It decides which box width and height describe: just the content, or the content plus padding and border. Almost every modern stylesheet changes it with a single rule near the top, and by the end of this lesson you will know exactly why.

The two values

box-sizing has two values you will use:

  • content-box (the default): width and height set the content area. Padding and border are added outside it.
  • border-box: width and height set the border box. Padding and border are carved out of that size, and the content area shrinks to make room.
CSS
.a { box-sizing: content-box; width: 200px; padding: 20px; border: 5px solid; }
/* visible width: 200 + 40 + 10 = 250px */

.b { box-sizing: border-box; width: 200px; padding: 20px; border: 5px solid; }
/* visible width: 200px; content area: 200 - 40 - 10 = 150px */

Same declarations, different results. The example below puts the two boxes on top of each other and measures them.

content-box vs border-box

The orange bar is exactly 200 pixels wide. The border-box element lines up with it; the content-box element overshoots by 50 pixels. The script measures the content area too: clientWidth is content plus padding, so subtracting the padding leaves the content width.

Why border-box is easier

With border-box, the number you write is the number you see. That pays off in three common situations.

Percentages add up. Two columns at width: 50% with padding fit side by side, because the padding is inside the 50%.

Changing padding does not break the layout. You can tweak padding or border thickness on a card without recalculating its width.

Full-width form fields fit. A text input with width: 100% plus padding and a border will overflow its container with content-box. With border-box, it fits exactly.

Full-width inputs with each model

The first input is 100% of the panel's content width plus 26 pixels of padding and border, so it pokes out of the dashed panel. The second fits.

Some form controls already use border-box by default in certain browsers (buttons, for example, and some input types), while text inputs use content-box. That inconsistency is another reason to set the value yourself.

The universal border-box reset

Because border-box is so much easier, most projects switch every element to it at the start of the stylesheet. The modern, widely used version looks like this:

CSS
*,
*::before,
*::after {
  box-sizing: border-box;
}
  • * matches every element.
  • *::before and *::after include generated content, because the universal selector does not match pseudo-elements on its own. (You will meet them in Pseudo-elements.)

The inheriting variant

You will also see this version in many codebases:

CSS
html {
  box-sizing: border-box;
}

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

Here the root sets border-box, and every other element inherits it from its parent. The result is the same everywhere, with one advantage: if a third-party widget needs content-box, you can set it on the widget's outer element and everything inside follows.

CSS
.legacy-calendar {
  box-sizing: content-box; /* the whole widget inherits this */
}

box-sizing does not inherit by default; the inherit keyword is what makes it flow down here. You will learn more about that keyword in Inheritance.

Either version is fine. Pick one and put it at the very top of your stylesheet, usually as part of a reset, which you will build in Resets and Base Styles.

What border-box does not change

border-box only changes what width, height, min-* and max-* refer to. A few things stay exactly the same:

  • Margins are still outside. A border-box element with width: 200px and margin: 20px still takes 240 pixels of horizontal space.
  • Content can still shrink to nothing. If padding plus border is larger than the width, the content area becomes zero (never negative) and the box grows to fit the padding and border.
  • Elements with no width set are unaffected. A block with width: auto fills its container either way; the difference only appears when you set a size.

The next example shows the second point. Drag the slider to change the padding on a 100-pixel border-box box and watch what happens when the padding gets too big.

When padding is bigger than the box

Up to 46 pixels of padding, the box stays 100 pixels wide and the content area shrinks. Past that, the content area hits zero and the box grows: padding and border always win.

Picking the right model

Use border-box everywhere by default. Reach for content-box only when the content size is the thing you truly care about, for example:

  • A canvas or image frame that must show exactly 640 ร— 360 pixels of content, with a decorative border of any thickness around it.
  • Code from an older library that was written for the default model.

In both cases, scoping content-box to that one component (as shown above) is cleaner than leaving the whole page on the default.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. With box-sizing: border-box, width: 300px, padding: 20px and border: 5px solid, how wide is the content area?

    The border box stays at 300 pixels. Subtract 40 pixels of padding and 10 pixels of border to get 250 pixels of content.
  2. 2. Why does the reset include *::before and *::after?

    * only matches elements. Pseudo-elements generate boxes too, so they need to be listed separately.
  3. 3. What is the main benefit of setting border-box on html and inherit on everything else?

    Every element inherits from its parent, so setting content-box on one wrapper changes the whole subtree.
  4. 4. Does box-sizing: border-box include margins in the width?

    border-box includes content, padding and border. Margins always sit outside the box, whichever model you use.
  5. 5. A border-box element is 60px wide with 40px of padding on each side. What happens?

    The content area cannot go below zero. Padding and border always fit, so the box becomes as wide as they need.

Summary

  • box-sizing decides what width and height measure.
  • content-box (the default) sizes the content; padding and border are added outside.
  • border-box sizes the border box; padding and border are taken from inside it.
  • Most stylesheets start with a universal border-box reset, covering *, *::before and *::after.
  • The inheriting variant lets a single component opt back into content-box.
  • Margins are never included, and the content area can shrink to zero but never below.

Next: Width and Height, where you learn about auto, minimums, maximums and sizes that come from the content itself.