CSS Box Sizing
In this page:
selector {
box-sizing: content-box | border-box;
}
Understanding content-box (Default)
By default, browsers calculate element widths using the content-box model. This means that any padding or borders you add are added to the outside of the width, making the element physically wider than specified.
- Avoid using content-box for layouts with padding, as it can cause element dimensions to overflow.
- Accepted values:
- content-box — width and height apply to the content only (initial value)
- border-box — width and height include padding and border
- inherit | initial | unset | revert — CSS-wide keywords accepted by every property
Example: Understanding content-box (Default)
<style>
.box {
box-sizing: content-box;
width: 200px;
padding: 20px;
border: 5px solid black;
}
</style>
<div class="box">Renders wider than 200px: padding and border add on top</div>
Understanding border-box
The border-box model forces borders and padding to sit inside your specified width, shrinking the content area to fit. This makes calculating element sizes and building clean layouts much easier.
- Use border-box to keep your element widths predictable and layout calculations simple.
- Accepted values:
- content-box — width and height apply to the content only (initial value)
- border-box — width and height include padding and border
- inherit | initial | unset | revert — CSS-wide keywords accepted by every property
Example: Understanding border-box
<style>
.box {
box-sizing: border-box;
width: 200px;
padding: 20px;
border: 5px solid black;
}
</style>
<div class="box">Stays exactly 200px: padding and border included</div>
Why border-box is Preferred
When building responsive grid layouts, columns are typically defined in percentage widths (like two 50% columns).
If you add padding or borders to these columns under the default content-box model, they will overflow 100% and break. The border-box model solves this by keeping your column widths exact and predictable.
Example: Why border-box is Preferred
<style>
.col {
box-sizing: border-box;
width: 50%;
padding: 10px;
background: lightblue;
border: 1px solid #666;
float: left;
}
</style>
<div class="col">50%</div>
<div class="col">50% — stays exact, doesn't overflow 100%</div>
The Global Box Reset
To avoid writing box-sizing: border-box on every single class selector in your stylesheets, you can use the global reset selector (*) combined with pseudo-elements to apply border-box to all elements on your page automatically.
Example: The Global Box Reset
<style>
*, *::before, *::after {
box-sizing: border-box;
}
</style>
<div style="width:200px; padding:20px; border:4px solid teal; background:#e0f2f1">border-box keeps this at 200px wide.</div>
Sizing Custom Grid Cards
Using border-box is essential when building complex card grids that have borders and padding, keeping your columns perfectly aligned and preventing layout shifts.
Example: Sizing Custom Grid Cards
<style>
.card {
box-sizing: border-box;
width: 33.33%;
padding: 16px;
border: 1px solid #ddd;
float: left;
}
</style>
<div class="card">Card</div>
<div class="card">Card</div>
<div class="card">Card</div>
- Using the default content-box model, causing elements with 100% width and padding to overflow.
- Forgetting to apply box-sizing to pseudo-elements (::before and ::after) in your global resets.
- Attempting to manually subtract padding sizes in your stylesheets instead of using border-box.
- The content-box model calculates element widths by adding padding and borders to the outside of the width, causing layouts to overflow.
- The border-box model forces borders and padding to sit inside the specified width, making layout sizing predictable.
- Include a global box reset ( { box-sizing: border-box; }) at the very top of your stylesheets to apply border-box globally.
The box-sizing property and border-box sizing are supported natively by all modern web browsers.
Chapter Quiz — Complete all 22 topics to unlock
0/22 topics done
Complete these topics first:
- CSS Combinators
- CSS Pseudo-classes
- CSS Pseudo-elements
- CSS Opacity
- CSS Navigation Bar
- CSS Vertical Navbar
- CSS Horizontal Navbar
- CSS Dropdowns
- CSS Advanced Dropdowns
- CSS Image Gallery
- CSS Image Sprites
- CSS Attribute Selectors
- CSS Forms
- CSS Counters
- CSS Specificity
- CSS Specificity Hierarchy
- CSS Variables
- CSS !important
- CSS Box Sizing
- CSS Media Queries
- CSS Pointer Events
- CSS Outline