Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

How do box-sizing rules affect an element’s final width?

Quick answer

With content-box, the declared width is the content width, so horizontal padding and borders increase the rendered width. With border-box, the declared width includes the content, padding, and borders. [S1] For a 300px width with 20px padding and 2px borders on each side, that gives 344px under content-box and 300px under border-box, before margins.

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

I set an element to width: 300px, but it looks wider than 300px. How can I work out its actual width?

Geez

First check its box-sizing value, then add the horizontal padding and borders if it uses content-box. Under that rule, width sets the content width; padding and borders add to the rendered width. Under border-box, the declared width includes the content, padding, and borders, but not margins. [S1]

Use this formula for the width including borders, but excluding margins:

  • content-box: declared width + left and right padding + left and right border
  • border-box: declared width

For example, assuming width: 300px, padding: 20px on each side, and a 2px border on each side, content-box gives 300 + 20 + 20 + 2 + 2 = 344px. With border-box, the result is 300px. [S1] The declared width means different parts of the box in these two cases.

Follow-up question

Can you show the same example in CSS so I can compare both rules?

Geez

Here are two otherwise identical examples. The box-sizing line is the difference:

.content-example {
  box-sizing: content-box;
  width: 300px;
  padding: 20px;
  border: 2px solid;
}

.border-example {
  box-sizing: border-box;
  width: 300px;
  padding: 20px;
  border: 2px solid;
}

For .content-example, the content is 300px wide, and the padding and border add 44px, making the width including borders 344px. For .border-example, the declared 300px includes the content, padding, and border, so the width including borders is 300px. [S1]

These figures assume the shown values apply on both horizontal sides. They exclude margins, which are not included in the declared width under either rule. [S1]

Follow-up question

What if I know the rendered width I want instead of the content width?

Geez

If you want the width including padding and borders to match your declared width, use box-sizing: border-box. For example, with width: 300px, padding: 20px, and border: 2px solid, the outside width including borders is 300px under border-box. [S1]

With content-box, you can calculate a declared width for a target outside width: subtract both horizontal padding values and both border widths. For a 300px target with 20px padding on each side and a 2px border on each side, calculate 300 - 20 - 20 - 2 - 2 = 256px for the declared content width. That calculation assumes those exact values on both sides and excludes margins. The source explains the box-sizing rules; it does not determine the dimensions of any particular element in your page.

Follow-up question

What’s a quick way to check my own CSS without mixing up the numbers?

Geez

Write down the declared width, the box-sizing value, and the left and right padding and border separately. Then apply the matching formula rather than treating width as the same measurement in every case. If a number still differs from what you expect, recheck the values you used in the calculation; this guide’s arithmetic covers padding and borders, not margins.

For a compact note, copy and fill in:

box-sizing: [content-box or border-box]
width: [number]
padding: [left] + [right]
border: [left] + [right]
rendered width including borders, excluding margins: [result]

For content-box, add the four side values to the declared width. For border-box, the declared width already includes padding and borders. [S1] This is a calculation guide, not an inspection of your specific page, so use the actual CSS values for your element.

A practical checklist

  • Find the element’s box-sizing value.

  • Record its declared width.

  • Add the left and right padding and border widths.

  • For content-box, add those side values to the declared width.

  • For border-box, use the declared width as the width including padding and borders.

  • Keep margins separate from this calculation.

A few more useful answers

Does `border-box` include margins in the declared width?

No. The declared width under border-box includes content, padding, and borders, but not margins. [S1]

Why does `content-box` make the rendered width larger than `width`?

Because width applies to the content, while horizontal padding and borders are added to get the width including borders. [S1]

How do I calculate the content width with `border-box`?

Subtract the left and right padding and border widths from the declared width. For example, a 300px width with 20px padding and 2px borders on both sides leaves 256px for content, assuming those values on each side.

Sources

References used for this guide.

  1. [S1] box-sizing CSS property - CSS | MDN
    developer.mozilla.org · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work