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.
Fundamentals
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.
Foundation
📝 Syntax
The syntax for box-sizing is simple. It can be applied to any block-level or inline-level element:
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.
Preview
👀 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.
Hands-On
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><divclass="box-content">
Content-box: width is 200px, plus padding and border.
</div><divclass="box-border">
Border-box: total width is 200px, including padding and border.
</div>
With global border-box, both boxes honor width: 50% without overflowing:
Col 1
Col 2
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.
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.
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.
Compatibility
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 Chrome10+ · Desktop & Mobile
Full support
Mozilla Firefox29+ · Desktop & Mobile
Full support
Apple Safari5.1+ · macOS & iOS
Full support
Microsoft Edge12+ · All versions
Full support
Opera7+ · Modern versions
Full support
box-sizing property99% supported
Bottom line: Use box-sizing freely in any project. The global border-box reset is safe for modern browsers.
Wrap Up
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.
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
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about box-sizing
Use these points when sizing your next layout.
5
Core concepts
📦01
Size Control
Sets width math rules.
Purpose
📄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.