Codectionary / Developer documentation / CSS

box-sizing

box-sizing controls how an element's total width and height are calculated. The default, content-box, means padding and border are added on top of the declared width, making sizing math confusing. border-box includes padding and border within the declared width instead, which is why nearly every modern CSS reset sets it globally.

Syntax

box-sizing: content-box | border-box;

Examples

content-box vs border-box

The same declared width producing different rendered sizes.

.content-box {
  box-sizing: content-box; /* default */
  width: 200px;
  padding: 20px;
  border: 5px solid black;
  /* Rendered width: 200 + 40 + 10 = 250px */
}

.border-box {
  box-sizing: border-box;
  width: 200px;
  padding: 20px;
  border: 5px solid black;
  /* Rendered width: exactly 200px - padding/border eat into the content area instead */
}

The Universal Reset

The standard way nearly every project applies border-box globally.

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

Parameters and values

  • content-box (keyword): Width/height apply to content only (default, less intuitive)
  • border-box (keyword): Width/height include padding and border (recommended)

Best practices

  • Apply box-sizing: border-box globally at the start of every project - it makes sizing predictable and is nearly universal practice
  • Remember border-box does not affect margin - margin is always added outside the declared width regardless of this setting
  • Set this once in a reset rather than per-component, so all elements behave consistently

At a glance

Purpose
Presentation and layout
File extension
.css
Runs in
Web browsers
Usually used with
HTML and JavaScript

Specifications & further reading

Related CSS documentation