CSS box-sizing Property

Beginner
6 min read
Updated: Oct 2026
3 examples
Live preview

Definition and Usage

The box-sizing property sets how an element’s width and height are calculated: content only (content-box), or content plus padding and border (border-box).

The initial value is content-box. Margin is never included. Many layouts use border-box so a set width stays the final outer size when padding or border is added.

Standard-track.

Not deprecated and not experimental. Baseline widely available. Some form controls already use border-box by default in browsers.

CSS Syntax

syntax.css
box-sizing: content-box | border-box | initial | inherit;

Only two keywords. Pick how width and height count padding and border.

Property Values

ValueDescriptionTry it
content-boxDefault. Width/height = content only. Padding and border add extra sizeTry it
border-boxWidth/height include content, padding, and border. Margin still outsideTry it
initialResets to content-boxTry it
inheritTakes the parent valueTry it

How to Use box-sizing

Decide whether width is “content only” or “final outer size.”

  1. Set width (and often height).
  2. Add padding and border as needed.
  3. Choose content-box if those extras should grow the box beyond the width.
  4. Choose border-box if the width should stay the total size and content should shrink instead.
ModeWidth meansTotal size example
content-boxContent area160px + padding + border > 160px
border-boxContent + padding + borderTotal stays 160px; content shrinks

Live Preview

Width, padding, and border stay fixed. Change only box-sizing and watch the outer size.

Scaffold: width 160px, padding 20px, border 8px. Margin is not part of box-sizing.

Box model

160px wide

box-sizing: content-box

CSS Examples

Same width, padding, and border in each demo. Only box-sizing changes. Use Try It Yourself for a live editor.

Example 1 — content-box (Default)

Padding and border are added outside the width.

CSS
<style>
  .box {
    width: 160px;
    padding: 20px;
    border: 8px solid #1d4ed8;
    box-sizing: content-box;
  }
</style>

<div class="box">Content box</div>
Try It Yourself

How It Works

  1. Width is content only. The content area is 160px wide.
  2. Padding adds space. 20px on each side adds 40px.
  3. Border adds more. 8px on each side adds 16px.
  4. Total outer width. 160 + 40 + 16 = 216px.

Example 2 — border-box

The same numbers, but the outer width stays 160px.

CSS
<style>
  .box {
    width: 160px;
    padding: 20px;
    border: 8px solid #1d4ed8;
    box-sizing: border-box;
  }
</style>

<div class="box">Border box</div>
Try It Yourself

How It Works

  1. Width is the outer size. The border box stays 160px wide.
  2. Padding and border fit inside. They take space from the content area.
  3. Content shrinks. Content width becomes 160 - 40 - 16 = 104px.
  4. Easier layout math. width: 100% with padding rarely overflows the parent.

Example 3 — Same Rules, Different Totals

Two boxes share width, padding, and border. Only box-sizing differs.

CSS
<style>
  .box {
    width: 160px;
    padding: 20px;
    border: 8px solid #1d4ed8;
  }
  .a { box-sizing: content-box; }
  .b { box-sizing: border-box; }
</style>

<div class="box a">content-box</div>
<div class="box b">border-box</div>
Try It Yourself

How It Works

  1. Shared scaffold. Both use 160px, 20px padding, and an 8px border.
  2. content-box grows. Outer width becomes 216px.
  3. border-box stays put. Outer width stays 160px.
  4. Same CSS numbers, different totals. That is the whole point of box-sizing.

Edge Cases & Pitfalls

Remember what is inside the width — and what never is.

margin

Margin is never included

box-sizing does not touch margin. Space outside the border always adds to layout separately.

overflow

width: 100% + padding

With content-box, padding can push a 100% child past its parent. border-box usually prevents that.

min

Content cannot go negative

With border-box, huge padding cannot erase the box. Content size floors at 0.

forms

Some controls start as border-box

Browsers often use border-box for certain buttons and inputs. Do not assume every element is content-box.

global

Common reset pattern

Many projects set *, *::before, *::after { box-sizing: border-box; } once so sizing stays predictable. Try it in tryit 4.

Browser Support

box-sizing is a browser style property. Node.js, Deno, and Bun do not apply it.

✓ Baseline · Widely available

box-sizing

Chrome 10+, Firefox 29+, Safari 5.1+, Edge 12+, Opera 7+, Internet Explorer 8+. Unprefixed in modern browsers.

98% global support
Google Chrome 10+
Full support
Mozilla Firefox 29+
Full support
Apple Safari 5.1+
Full support
Microsoft Edge 12+
Full support
Opera 7+
Full support
Samsung Internet 1+
Full support
Internet Explorer 8+
Full support
box-sizing Universally supported in modern browsers

Bottom line: Safe for production. No prefix is required in current browsers for content-box and border-box.

Key Takeaways

  • Role: decide whether width/height include padding and border.
  • Default: content-box — extras grow the box.
  • border-box: width stays the outer size; content shrinks.
  • Margin: always outside; never part of box-sizing.

One line: box-sizing chooses whether padding and border sit inside or outside your width.

Frequently Asked Questions

It controls whether width and height apply only to the content area, or also include padding and border.
The initial value is content-box. Padding and border are added outside the width and height you set.
content-box: width is content only. border-box: width includes content, padding, and border. Margin is never included.
No. Margin always sits outside the box and is not part of the box-sizing calculation.
With border-box, a declared width stays the final outer size when you add padding or border, which makes layout math easier.
It is Baseline widely available: Chrome 10+, Firefox 29+, Safari 5.1+, Edge 12+, Opera 7+, Internet Explorer 8+. It is a browser style property, not a Node.js feature.

Next: break-after

Control how page, column, or region breaks behave after an element.

break-after tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful