How Does the CSS Box Model Calculate Width and Height?

The CSS box model is the foundational layout engine of the web, dictating how every HTML element is sized, spaced, and rendered on a page. To determine the total physical space an element occupies, the browser computes the dimensions across four concentric layers: the content area, padding, borders, and margins. Crucially, the exact mathematical formula used to determine an element's final rendered width and height depends on the box-sizing property applied in the stylesheet.

The Four Layers of the Box Model

Every element on a web page is treated as a rectangular box composed of four distinct layers:

Standard Box Sizing: content-box

By default, web browsers apply box-sizing: content-box to HTML elements. Under this model, the CSS width and height properties set only the dimensions of the innermost content rectangle.

Any padding or borders assigned to the element are added onto the outside of the specified width and height, causing the element to expand outward.

The Standard Calculation Formula

\[\text{Rendered Width} = \text{width} + \text{padding-left} + \text{padding-right} + \text{border-left-width} + \text{border-right-width}\]

\[\text{Rendered Height} = \text{height} + \text{padding-top} + \text{padding-bottom} + \text{border-top-width} + \text{border-bottom-width}\]

To calculate the total page layout space consumed by the element, margins must also be included:

\[\text{Total Layout Width} = \text{Rendered Width} + \text{margin-left} + \text{margin-right}\]

\[\text{Total Layout Height} = \text{Rendered Height} + \text{margin-top} + \text{margin-bottom}\]

Calculation Example

Consider an element with the following styles:

.card {
  box-sizing: content-box;
  width: 300px;
  height: 150px;
  padding: 20px;
  border: 5px solid #000;
  margin: 15px;
}

Alternative Box Sizing: border-box

Because adding padding and borders can make responsive layout calculations cumbersome in content-box, modern web development widely relies on box-sizing: border-box.

Under border-box, the declared width and height properties represent the element's entire visible boundary, including content, padding, and borders. As padding and border widths increase, the inner content area shrinks automatically to maintain the fixed total dimensions.

The Border-Box Calculation Formula

\[\text{Rendered Width} = \text{width}\]

\[\text{Rendered Height} = \text{height}\]

The browser derives the inner content area dynamically:

\[\text{Content Width} = \text{width} - (\text{padding-left} + \text{padding-right} + \text{border-left-width} + \text{border-right-width})\]

\[\text{Content Height} = \text{height} - (\text{padding-top} + \text{padding-bottom} + \text{border-top-width} + \text{border-bottom-width})\]

The total footprint in the layout is calculated by adding margins to the explicit width:

\[\text{Total Layout Width} = \text{width} + \text{margin-left} + \text{margin-right}\]

\[\text{Total Layout Height} = \text{height} + \text{margin-top} + \text{margin-bottom}\]

Calculation Example

Using the same values with border-box:

.card {
  box-sizing: border-box;
  width: 300px;
  height: 150px;
  padding: 20px;
  border: 5px solid #000;
  margin: 15px;
}

Factors Affecting Total Calculations

Beyond the primary mathematical layers, specific CSS layout behaviors influence final dimension calculations: