The CSS box-sizing property controls how the
width and height of an element are calculated.
It is especially important when an element contains padding or borders because
those layers can change the element's final rendered dimensions.
Understanding box-sizing makes CSS sizing much easier to predict.
Instead of wondering why a box is wider than its declared width, you can determine
exactly which parts of the box are included in the calculation.
The Two Main box-sizing Values
content-box
The declared width and height apply to the content area. Padding and border are added outside those dimensions.
border-box
The declared width and height include the content, padding, and border.
What Is the Default box-sizing Value?
For most elements, the default value of box-sizing is
content-box.
This means that when you write a width such as 300px,
that width applies to the content box rather than the complete border box.
.box {
width: 300px;
padding: 20px;
border: 5px solid black;
}
With content-box, the content area is 300px wide.
The left and right padding add another 40px, and the two 5px borders add another 10px.
Total width = 300px + 40px + 10px = 350px
This is one of the most common reasons a developer sees an element become larger than the width written in CSS.
What Does content-box Mean?
With box-sizing: content-box, the declared dimensions belong only to
the content area.
Padding and border are outside that area.
The important idea is that the declared width does not represent the entire visible box when using the content-box model.
What Does border-box Mean?
With box-sizing: border-box, the declared width and height represent
the entire border box.
Padding and border are included inside the specified dimensions.
.box {
box-sizing: border-box;
width: 300px;
padding: 20px;
border: 5px solid black;
}
The outer width remains exactly 300px. The padding and border consume part of that 300px, leaving the remaining space for the content area.
The exact content area depends on the declared dimensions and the amount of padding and border, but the outer border box remains controlled by the declared width.
content-box vs border-box
| Behavior | content-box | border-box |
|---|---|---|
| Declared width includes | Content only | Content + padding + border |
| Padding added outside width | Yes | No |
| Border added outside width | Yes | No |
| Predictable fixed component width | Less convenient | More convenient |
Why Is border-box Commonly Used?
Developers often prefer border-box because it makes component dimensions
easier to reason about.
A card declared as 320px wide remains 320px wide even after padding and borders are added.
This becomes especially useful in responsive layouts, where several elements may need to fit within a known container width.
.card {
box-sizing: border-box;
width: 320px;
padding: 24px;
border: 1px solid #cbd5e1;
}
The complete border box is still 320px wide.
Using border-box Globally
A common CSS setup applies border-box to every element,
including pseudo-elements.
*,
*::before,
*::after {
box-sizing: border-box;
}
This creates a consistent sizing model across the application. It is especially convenient for forms, cards, grids, navigation elements, and other components where padding and borders are frequently used.
Does box-sizing Affect Margin?
No.
The box-sizing calculation concerns the element's content,
padding, and border dimensions.
Margin remains outside the border box.
Includes content + padding + border.
Remains outside the border box.
Does box-sizing Affect Height?
Yes.
The same content-box and border-box behavior applies to the declared
height as well.
.panel {
box-sizing: border-box;
height: 200px;
padding: 20px;
border: 4px solid #334155;
}
With border-box, the full border edge remains 200px high.
The content area uses whatever space remains after padding and border are accounted for.
box-sizing and Responsive Widths
The difference becomes particularly noticeable when using
width: 100%.
.problem {
width: 100%;
padding: 20px;
}
With content-box, the content width can already consume the entire
available width before padding is added.
The resulting outer box can therefore become wider than its container.
Using border-box keeps the padding inside the declared 100% width.
.responsive-box {
box-sizing: border-box;
width: 100%;
padding: 20px;
}
box-sizing with max-width
A combination of width, max-width, padding,
and border-box works well for responsive components.
.container {
box-sizing: border-box;
width: 100%;
max-width: 900px;
padding: 24px;
margin-inline: auto;
}
The component can shrink on smaller screens while never exceeding its maximum width.
box-sizing and CSS Grid
Grid layouts often contain cards with padding and borders. Consistent box sizing helps those components stay within the grid tracks you expect.
.grid {
display: grid;
grid-template-columns:
repeat(3, 1fr);
gap: 1rem;
}
.card {
box-sizing: border-box;
padding: 20px;
border: 1px solid #cbd5e1;
}
This avoids confusing situations where internal padding causes a component to exceed the dimensions you expected from the grid track.
box-sizing and Flexbox
The same principle applies to flex items.
When an item has a declared size and also has padding or borders,
box-sizing affects the dimensions used by that item.
.layout {
display: flex;
gap: 1rem;
}
.sidebar {
box-sizing: border-box;
width: 280px;
padding: 20px;
border: 1px solid #cbd5e1;
}
.content {
min-width: 0;
flex: 1;
}
The sizing model becomes part of the overall layout behavior, so understanding it helps when debugging flex-based interfaces.
A Real-World Form Example
Forms are a common place where box-sizing prevents unexpected widths.
Inputs often need padding and borders while still fitting perfectly inside their parent container.
input {
box-sizing: border-box;
width: 100%;
padding: 12px 16px;
border: 1px solid #cbd5e1;
}
The input remains within the parent's width while padding and border are included in its dimensions.
Common box-sizing Mistakes
Assuming width Means Total Width
With content-box, width applies to the content area rather than the complete border box.
Adding Padding to width: 100%
Without border-box, padding can make a supposedly 100% wide element overflow its container.
Forgetting Pseudo-Elements
When using a global box-sizing rule, include ::before and ::after
so generated content follows the same sizing model.
Mixing Sizing Assumptions
Different box-sizing behavior across components can make layout calculations harder to understand.
How to Debug box-sizing Problems
When an element is larger than expected, inspect it using browser DevTools.
Look at its computed box-sizing, width, padding, and border values.
A Practical CSS Reset with border-box
/* Predictable box sizing */
*,
*::before,
*::after {
box-sizing: border-box;
}
/* Keep media responsive */
img,
svg,
video {
display: block;
max-width: 100%;
}
/* Remove default page margin */
body {
margin: 0;
}
This is a common foundation for modern projects. The important part is understanding why the sizing rule exists rather than treating it as magic CSS.
When Should You Use content-box?
content-box is still a valid CSS sizing model.
There are situations where you may intentionally want the declared width or height
to represent only the content area.
The important point is consistency. Choose the sizing behavior that matches the component's purpose and understand how padding and borders will affect its final dimensions.
When Should You Use border-box?
border-box is often convenient for application UI because components
frequently have padding and borders while still needing predictable overall dimensions.
It is particularly useful for:
- Responsive containers.
- Cards and panels.
- Form controls.
- Navigation components.
- Grid items.
- Flex layouts.
- Reusable UI components.
box-sizing Best Practices
Prefer Predictable Sizing
Use a consistent box-sizing strategy across components.
Include Pseudo-Elements
Apply the same sizing model to ::before and ::after.
Think About Padding
Always consider how padding contributes to the final dimensions.
Think About Borders
Borders also affect dimensions under content-box sizing.
Test Responsive Widths
Pay special attention to 100% widths inside narrow containers.
Use DevTools
Inspect actual computed dimensions instead of guessing.
Key Takeaways
box-sizing determines how CSS calculates an element's declared width and height.
The default content-box model applies width and height to the content area.
With content-box, padding and border increase the final outer dimensions.
With border-box, the declared width and height include content, padding, and border.
Margin remains outside the border box regardless of the box-sizing value.
A global border-box rule often makes component sizing more predictable.
Understanding box-sizing is especially important for responsive layouts, forms, grids, cards, and reusable components.