CSS box-sizing Property

Beginner
⏱️ 6 min read
📚 Updated: Jun 2026
🎯 4 Examples
Layout & Box Model

What You’ll Learn

The box-sizing property controls how an element’s total width and height are calculated. Understanding content-box vs border-box helps you build layouts that behave predictably — especially when padding and borders are involved.

01

Box Model

How sizes are counted.

02

content-box

Default CSS behavior.

03

border-box

Padding inside width.

04

Width Math

Avoid overflow bugs.

05

Global Reset

border-box everywhere.

06

Responsive UI

Grids, forms, cards.

Definition and Usage

The box-sizing CSS property controls how the total width and height of an element are calculated. By default, the width and height apply to the content area only — padding and border are added on top, which can make elements wider than you expect.

The box-sizing property lets you change this behavior to simplify layout calculations and avoid common issues with elements that have padding and borders. It is especially important for responsive web design and front-end layouts.

💡
Beginner Tip

If you set width: 200px and add padding: 20px, a content-box element becomes 240px wide. With border-box, it stays 200px total — the padding fits inside.

📝 Syntax

The syntax for box-sizing is simple. It can be applied to any block-level or inline-level element:

syntax.css
selector {
  box-sizing: content-box | border-box;
}

Basic Example

box-sizing.css
.card {
  box-sizing: border-box;
  width: 100%;
  padding: 1rem;
  border: 1px solid #e2e8f0;
}

Syntax Rules

  • The initial value is content-box.
  • Only two main keywords: content-box and border-box.
  • The property is not inherited by default — set it on each element or use a global reset.
  • Margin is never included in either box-sizing mode.
  • Works on all elements that accept width and height.

⚡ Quick Reference

QuestionAnswer
Initial valuecontent-box
Applies toAll elements that accept width or height
InheritedNo
AnimatableNo
Common useGlobal border-box reset for predictable layouts

💎 Property Values

The box-sizing property accepts two keyword values that change how width and height are measured.

ValueExampleMeaning
content-boxbox-sizing: content-box;Width and height include only the content. Padding and border are added outside the set width.
border-boxbox-sizing: border-box;Width and height include content, padding, and border. The declared width is the total visible width.
content-box — default border-box — inclusive

How Width Is Calculated

Both boxes below have width: 200px, padding: 20px, and border: 5px solid black. Only the box-sizing value differs:

  • content-box — Total width = 200px (content) + 40px (padding) + 10px (border) = 250px
  • border-box — Total width = 200px (content, padding, and border all fit inside)

This is why columns in a grid sometimes overflow unexpectedly with content-box, and why border-box is popular for full-width layouts.

👀 Live Preview

Compare content-box and border-box side by side. Both use the same declared width, padding, and border.

content-box

Width is content only — padding and border add extra size.

border-box

Total width stays fixed — padding and border fit inside.

Examples Gallery

See content-box vs border-box, a global reset, grid columns, and form inputs.

📦 Box Model Basics

The clearest way to understand box-sizing is to compare both values on identical elements — just like the reference example.

Example 1 — content-box vs. border-box

Demonstrate how box-sizing affects the width of a <div> with padding and border.

compare-box-sizing.html
<style>
  .box-content {
    box-sizing: content-box;
    width: 200px;
    padding: 20px;
    border: 5px solid black;
  }

  .box-border {
    box-sizing: border-box;
    width: 200px;
    padding: 20px;
    border: 5px solid black;
  }
</style>

<div class="box-content">
  Content-box: width is 200px, plus padding and border.
</div>
<div class="box-border">
  Border-box: total width is 200px, including padding and border.
</div>
Try It Yourself

How It Works

Both divs declare width: 200px, but the content-box version renders wider because padding and border sit outside that 200px content area.

🌐 Global Patterns

Many CSS frameworks and developers apply border-box to all elements so sizing stays predictable across the whole page.

Example 2 — Global border-box Reset

Apply border-box to every element with a popular universal reset pattern.

global-border-box.css
*,
*::before,
*::after {
  box-sizing: border-box;
}
Try It Yourself

How It Works

Setting border-box on *, *::before, and *::after ensures pseudo-elements follow the same sizing rules. This is one of the most common CSS resets in modern projects.

📐 Layout Use Cases

border-box prevents grid and form elements from breaking out of their containers when padding is added.

Example 3 — Grid Columns Without Overflow

Use border-box so two 50% columns with padding sit side by side without wrapping.

grid-border-box.html
<style>
  .col {
    box-sizing: border-box;
    width: 50%;
    padding: 1rem;
    border: 1px solid #cbd5e1;
  }

  .row {
    display: flex;
  }
</style>

<div class="row">
  <div class="col">Left</div>
  <div class="col">Right</div>
</div>
Try It Yourself

How It Works

Without border-box, two width: 50% columns plus padding would exceed 100% and cause layout overflow. Border-box keeps each column exactly half the row.

Example 4 — Form Input at Full Width

Make text inputs fill their container without busting past 100% width when padding and border are added.

input-border-box.html
<style>
  input[type="text"] {
    box-sizing: border-box;
    width: 100%;
    padding: 0.65rem 0.75rem;
    border: 1px solid #cbd5e1;
    border-radius: 0.5rem;
  }
</style>

<label for="email">Email</label>
<input type="text" id="email" placeholder="you@example.com">
Try It Yourself

How It Works

Form inputs often default to content-box. Setting border-box with width: 100% keeps the input aligned with its parent container.

🧠 How box-sizing Works

1

You set width or height

Declare dimensions on an element — for example width: 200px with padding and border.

CSS rule
2

box-sizing picks the formula

content-box counts only content. border-box includes padding and border inside the declared size.

Calculation mode
3

The browser lays out the box

The final rendered size follows your chosen box-sizing model, affecting alignment, overflow, and column fit.

Layout engine
=

Predictable sizing

Elements behave the way you expect — especially in grids, forms, and responsive layouts.

Universal Browser Support

The box-sizing property is widely supported across all modern browsers, including Chrome, Firefox, Safari, Edge, and Opera. It is a reliable property for controlling element sizing and layout.

Baseline · All browsers

Dependable box model control everywhere

Chrome, Firefox, Safari, Edge, and Opera all support both content-box and border-box.

99% Universal support
Google Chrome 10+ · Desktop & Mobile
Full support
Mozilla Firefox 29+ · Desktop & Mobile
Full support
Apple Safari 5.1+ · macOS & iOS
Full support
Microsoft Edge 12+ · All versions
Full support
Opera 7+ · Modern versions
Full support
box-sizing property 99% supported

Bottom line: Use box-sizing freely in any project. The global border-box reset is safe for modern browsers.

Conclusion

The box-sizing property is a useful tool for controlling how the dimensions of an element are calculated. By choosing between content-box and border-box, you can simplify your layout calculations and create more predictable, consistent designs.

Experiment with both values to see which works best for your layout needs. For most modern responsive projects, a global border-box reset is the easiest path to predictable sizing.

💡 Best Practices

✅ Do

  • Use border-box for grids, cards, and full-width inputs
  • Apply a global border-box reset at the start of your stylesheet
  • Include *::before and *::after in the reset
  • Test column layouts with padding and borders applied
  • Remember margin is always outside the box-sizing calculation

❌ Don’t

  • Assume width: 50% columns will fit without border-box
  • Mix content-box and border-box unintentionally in the same layout
  • Forget that the CSS default is content-box
  • Expect box-sizing to include margin in the width calculation
  • Override border-box on child elements unless you have a specific reason

Key Takeaways

Knowledge Unlocked

Five things to remember about box-sizing

Use these points when sizing your next layout.

5
Core concepts
📄 02

content-box

Default CSS behavior.

Default
📦 03

border-box

Padding inside width.

Keyword
📐 04

No Margin

Margin stays outside.

Box model
05

Universal

All modern browsers.

Support

❓ Frequently Asked Questions

box-sizing controls how an element's total width and height are calculated. It decides whether padding and border are added on top of the width you set, or included inside it.
The initial value is content-box. With content-box, the width and height apply only to the content area — padding and border increase the element's total size.
With content-box, padding and border are added outside the set width. With border-box, padding and border are included inside the set width, so the total visible width stays exactly what you declared.
Many developers apply border-box globally because it makes layout math easier and more predictable. It is a common, safe pattern for modern responsive design.
No. box-sizing only affects how width and height include content, padding, and border. Margin always sits outside the element box and is not part of the box-sizing calculation.

Practice in the Live Editor

Open the HTML editor, apply box-sizing, and compare content-box vs border-box instantly.

HTML Editor →

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.

5 people found this page helpful