Skip to main content

CSS Foundations

Box Sizing

Written by Updated Box Sizing exercises

Control how padding and borders participate in declared sizes and prevent accidental overflow. You will see how content-box and border-box calculate dimensions, why controls overflow, and how minimum sizes affect Flexbox and Grid children.

You set a box to width: 300px, add 20px of padding and a border, and it comes out 342px wide. box-sizing: border-box fixes that, which is why almost every modern stylesheet starts with it.

The default model, content-box, applies the width to the content only and adds padding and border on top. border-box keeps padding and border inside the width you set, so 300px means 300px. Sizes are covered in CSS Width and Height, and content-based sizes in Intrinsic Sizing.

The reset most sites use

Example

css

html { box-sizing: border-box; }

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

This makes every element use border-box. Writing it as inherit from html, instead of setting border-box on * directly, lets a component switch back to content-box for all of its children with one line.

Box-sizing Values

ValueDeclared size includesExample
content-boxContent onlybox-sizing: content-box
border-boxContent, padding, and borderbox-sizing: border-box

When a box still overflows

border-box solves the padding maths, but it is not the only reason things overflow. The other common cause is a flex or grid item that refuses to get narrower than its content, like a long URL. That one is fixed with min-width: 0 on the item.

Limits help too:

Example

css

.article {
  width: min(100% - 2rem, 70ch);
  margin-inline: auto;
}

.sidebar {
  min-width: 14rem;
  max-width: 22rem;
}

The article is never wider than 70 characters of text, and on a phone it leaves a 1rem gap on each side. The sidebar stays between 14rem and 22rem.

Complete Example

Complete runnable example

html

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Box Sizing example</title>
  <style>
    * { box-sizing: border-box; }
    body {
      margin: 0;
      padding: 2rem;
      font-family: system-ui, sans-serif;
      line-height: 1.5;
    }
    main { max-width: 70rem; margin-inline: auto; }
    article, section, aside, form, .card, .panel {
      padding: 1rem;
      border: 1px solid #cbd5e1;
      border-radius: 0.6rem;
    }

html { box-sizing: border-box; }

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

.article {
  width: min(100% - 2rem, 70ch);
  margin-inline: auto;
}

.sidebar {
  min-width: 14rem;
  max-width: 22rem;
}
  </style>
</head>
<body>
  <main class="article"><h1>Border-box sizing</h1><aside class="sidebar">This box keeps padding and borders inside its declared width.</aside></main>
</body>
</html>

Browser Support

Feature
Chrome
Edge
Firefox
Safari
box-sizing1012295.1

Versions show the first stable desktop release with unprefixed support. Data source: MDN Browser Compatibility Data 8.0.8. A partial-support note is included where it changes how the example behaves.

Box Sizing exercises

Padding inside the width

Make the card 200px wide in total, with 20px padding on every side. Use box-sizing so that the padding is part of the 200px.

The HTML your code works on
<div class="card">Card</div>
<div class="wide">Wide</div>

Result

Check your answer to see it here.