CSS box-sizing Property
The box-sizing property determines whether an element's specified width and height include its padding and borders.
By default, most elements use content-box, which applies width and height to the content area only. The border-box value includes padding and borders within the specified dimensions.
Using border-box often makes responsive layouts easier to manage because adding padding or borders does not automatically increase an element's declared width.
box-sizing Syntax
Specify how the browser should interpret an element's width and height.
selector {
box-sizing: border-box;
}
The general syntax is:
selector {
box-sizing: content-box | border-box;
}
content-box
.box {
box-sizing: content-box;
}
Width and height apply to the content area. Padding and borders are added outside those dimensions.
border-box
.box {
box-sizing: border-box;
}
Width and height include the content area, padding, and borders.
CSS-Wide Keywords
.inherited {
box-sizing: inherit;
}
.reset {
box-sizing: initial;
}
.unset {
box-sizing: unset;
}
.reverted {
box-sizing: revert;
}
.layer-reverted {
box-sizing: revert-layer;
}
The property accepts one sizing keyword or one CSS-wide keyword per declaration.
Property Values
The box-sizing property accepts two primary values.
| Value | Description |
|---|---|
content-box |
Width and height apply to the content box. Padding and borders increase the outer dimensions. This is the initial value. |
border-box |
Width and height include content, padding, and borders. The content area is reduced to accommodate padding and borders. |
CSS-Wide Keywords
| Value | Description |
|---|---|
inherit |
Uses the computed value from the parent element. |
initial |
Resets the property to content-box. |
unset |
Resets the property to content-box because it is not inherited. |
revert |
Reverts the property according to cascade origin rules. |
revert-layer |
Reverts the property according to cascade layer rules. |
Property Characteristics
| Characteristic | Value |
|---|---|
| Initial value | content-box |
| Inherited | No |
| Applies to | Elements that accept width or height |
| Computed value | Specified keyword |
| Animation type | Discrete |
| Percentages | Not applicable to this property |
| Property type | Individual property |
Although the initial value is content-box, browser default styles apply border-box to certain elements, including many form controls.
How It Works
The box-sizing property changes how specified dimensions relate to an element's content, padding, and borders.
Step 1: Create a Box
<div class="example-box">Example Content</div>
Step 2: Set Dimensions
.example-box {
width: 200px;
padding: 20px;
border: 5px solid #1769aa;
}
Step 3: Use content-box
.example-box {
box-sizing: content-box;
}
The content width is 200 pixels. Padding adds 40 pixels, and borders add another 10 pixels, producing a total border-box width of 250 pixels.
Step 4: Use border-box
.example-box {
box-sizing: border-box;
}
The total border-box width is 200 pixels. Padding and borders occupy 50 pixels, leaving 150 pixels for the content width.
Step 5: Compare the Results
| Measurement | content-box | border-box |
|---|---|---|
| Declared width | 200px | 200px |
| Horizontal padding | 40px | 40px |
| Horizontal borders | 10px | 10px |
| Content width | 200px | 150px |
| Total border-box width | 250px | 200px |
The difference is how the declared width is interpreted, not whether padding and borders exist.
The CSS Box Model
The CSS box model describes the rectangular areas that make up an element.
Content
The content area contains text, images, or other content.
Padding
Padding creates space between the content and the border.
Border
The border surrounds the padding and content.
Margin
The margin creates space outside the border.
Box Model Example
.box {
width: 200px;
padding: 20px;
border: 5px solid #1769aa;
margin: 15px;
}
With content-box, the border-box width is 250 pixels. The left and right margins add another 30 pixels to the horizontal space occupied in this simple calculation.
Using border-box
.box {
box-sizing: border-box;
width: 200px;
padding: 20px;
border: 5px solid #1769aa;
margin: 15px;
}
The border-box width is 200 pixels, while the margins remain outside that width.
The box-sizing property changes the sizing relationship between content, padding, and borders. It does not include margins in the declared dimensions.
The content-box Value
The content-box value applies specified width and height to the content area only.
.box {
box-sizing: content-box;
}
Width Example
.box {
box-sizing: content-box;
width: 300px;
padding: 20px;
border: 5px solid #1769aa;
}
The content width is 300 pixels, while the total border-box width is 350 pixels.
Height Example
.box {
box-sizing: content-box;
height: 100px;
padding: 15px;
border: 5px solid #1769aa;
}
The content height is 100 pixels, while the total border-box height is 140 pixels.
Percentage Width
.box {
box-sizing: content-box;
width: 100%;
padding: 20px;
border: 2px solid #1769aa;
}
The content width fills the containing block's available width. Padding and borders are then added, potentially causing horizontal overflow.
Content-Based Sizing
.box {
box-sizing: content-box;
width: 25rem;
padding: 1rem;
}
This value is useful when the content area itself must have a specific dimension.
Because padding and borders increase the outer dimensions, developers must account for them when calculating available layout space.
The border-box Value
The border-box value includes padding and borders within the specified width and height.
.box {
box-sizing: border-box;
}
Width Example
.box {
box-sizing: border-box;
width: 300px;
padding: 20px;
border: 5px solid #1769aa;
}
The total border-box width is 300 pixels, leaving 250 pixels for the content area.
Height Example
.box {
box-sizing: border-box;
height: 100px;
padding: 15px;
border: 5px solid #1769aa;
}
The total border-box height is 100 pixels, leaving 60 pixels for the content area.
Full-Width Element
.box {
box-sizing: border-box;
width: 100%;
padding: 20px;
border: 2px solid #1769aa;
}
Padding and borders remain within the declared width, making the element easier to fit inside its container.
Responsive Card
.card {
box-sizing: border-box;
width: 100%;
max-width: 400px;
padding: 24px;
border: 1px solid #dddddd;
}
Important Limitation
If padding and borders require more space than the declared dimensions allow, the element's used border-box size can exceed the specified width or height. The content box cannot have a negative size.
.box {
box-sizing: border-box;
width: 40px;
padding: 30px;
border: 5px solid #1769aa;
}
The horizontal padding and borders total 70 pixels, so the used border-box width cannot remain 40 pixels.
For most interface layouts, border-box provides more predictable sizing when padding and borders are added.
content-box vs. border-box
The difference between the two values is which part of the box the declared dimensions describe.
| Feature | content-box | border-box |
|---|---|---|
| Initial value | Yes | No |
| Declared width | Content width | Border-box width |
| Declared height | Content height | Border-box height |
| Padding | Added outside declared dimensions | Included within declared dimensions |
| Borders | Added outside declared dimensions | Included within declared dimensions |
| Margins | Outside the box | Outside the box |
| Typical use | Explicit content-area sizing | General interface and responsive layouts |
Identical Dimensions
.content-box {
box-sizing: content-box;
width: 200px;
padding: 20px;
border: 5px solid #1769aa;
}
.border-box {
box-sizing: border-box;
width: 200px;
padding: 20px;
border: 5px solid #1769aa;
}
The first element has a total border-box width of 250 pixels. The second has a total border-box width of 200 pixels.
Choosing a Value
Use content-box when the content area must have an exact specified size. Use border-box when the complete element, including padding and borders, should fit within a declared size.
Width Calculations
For a simple element with an explicit width, padding, and borders, the total border-box width depends on the box-sizing value.
content-box Formula
Total border-box width =
width + padding-left + padding-right
+ border-left-width + border-right-width
Example Calculation
.box {
box-sizing: content-box;
width: 240px;
padding: 15px;
border: 3px solid #1769aa;
}
240px + 15px + 15px + 3px + 3px = 276px
The total border-box width is 276 pixels.
border-box Formula
Content width =
width - padding-left - padding-right
- border-left-width - border-right-width
Example Calculation
.box {
box-sizing: border-box;
width: 240px;
padding: 15px;
border: 3px solid #1769aa;
}
240px - 15px - 15px - 3px - 3px = 204px
The content width is 204 pixels, and the total border-box width remains 240 pixels.
Percentage Width
.box {
box-sizing: border-box;
width: 50%;
padding: 20px;
border: 2px solid #1769aa;
}
The declared border-box width is 50% of the applicable containing block width. Padding and borders are included within that amount.
These calculations assume sufficient space for the content box and do not account for additional minimum-size constraints.
Height Calculations
The same sizing principles apply to explicitly specified heights.
content-box Height
.box {
box-sizing: content-box;
height: 120px;
padding: 20px;
border: 5px solid #1769aa;
}
120px + 20px + 20px + 5px + 5px = 170px
The total border-box height is 170 pixels.
border-box Height
.box {
box-sizing: border-box;
height: 120px;
padding: 20px;
border: 5px solid #1769aa;
}
120px - 20px - 20px - 5px - 5px = 70px
The content area receives 70 pixels of height.
Minimum Height
.panel {
box-sizing: border-box;
min-height: 150px;
padding: 20px;
border: 2px solid #1769aa;
}
The minimum height applies according to the selected box-sizing model.
Automatic Height
.panel {
box-sizing: border-box;
height: auto;
padding: 20px;
}
With automatic height, the element generally grows to accommodate its content and padding. The difference between the sizing models is most apparent when explicit or constrained dimensions are used.
Content can overflow a fixed-height box if insufficient space is available. The box-sizing property does not automatically prevent overflow.
Padding and Borders
Padding and borders are the dimensions directly affected by the box-sizing calculation.
Adding Padding with content-box
.box {
box-sizing: content-box;
width: 250px;
padding: 25px;
}
The total width becomes 300 pixels when no borders are present.
Adding Padding with border-box
.box {
box-sizing: border-box;
width: 250px;
padding: 25px;
}
The total width remains 250 pixels, leaving 200 pixels for content.
Adding a Border
.box {
box-sizing: border-box;
width: 250px;
padding: 20px;
border: 5px solid #1769aa;
}
The padding and border occupy 50 pixels horizontally, leaving 200 pixels for content.
Different Side Values
.box {
box-sizing: border-box;
width: 300px;
padding: 10px 25px;
border-left: 4px solid #1769aa;
border-right: 6px solid #1769aa;
}
The horizontal padding totals 50 pixels, and the borders total 10 pixels. The content width is 240 pixels.
Changing Border Width on Hover
.button {
box-sizing: border-box;
width: 180px;
padding: 10px;
border: 2px solid #1769aa;
}
.button:hover {
border-width: 4px;
}
The declared border-box width remains 180 pixels while the content area becomes smaller to accommodate the thicker border.
Using border-box can reduce unexpected changes to outer dimensions when padding or border widths change.
Margins
Margins remain outside an element's border box regardless of the box-sizing value.
content-box with Margins
.box {
box-sizing: content-box;
width: 200px;
padding: 20px;
border: 5px solid #1769aa;
margin: 15px;
}
The border-box width is 250 pixels. Including both horizontal margins, the total horizontal extent is 280 pixels.
border-box with Margins
.box {
box-sizing: border-box;
width: 200px;
padding: 20px;
border: 5px solid #1769aa;
margin: 15px;
}
The border-box width is 200 pixels. Including both horizontal margins, the total horizontal extent is 230 pixels.
Centered Element
.box {
box-sizing: border-box;
width: 300px;
margin-inline: auto;
padding: 20px;
}
Automatic inline margins can center a block when the layout provides available horizontal space.
Margin and Width
.box {
box-sizing: border-box;
width: 100%;
margin: 20px;
}
The width can fill the containing block while the margins extend beyond it, potentially causing overflow.
Neither content-box nor border-box includes margins in the declared width or height.
Global box-sizing Reset
A common CSS reset applies border-box throughout a website so element dimensions are easier to manage.
Universal Selector Reset
*,
*::before,
*::after {
box-sizing: border-box;
}
This applies border-box sizing to elements and their ::before and ::after pseudo-elements.
Inheritance-Based Reset
html {
box-sizing: border-box;
}
*,
*::before,
*::after {
box-sizing: inherit;
}
This approach sets border-box on the root element and makes other elements inherit their parent's sizing model.
Overriding a Component
.special-box {
box-sizing: content-box;
}
An individual element can still use content-box when a specific content-area dimension is required.
Why Include Pseudo-Elements?
*::before,
*::after {
box-sizing: border-box;
}
Pseudo-elements can have their own dimensions, padding, and borders. Including them keeps sizing behavior consistent.
Reset Placement
Place the reset near the beginning of the stylesheet, before component-specific rules.
A global border-box reset is a common choice for modern layouts, but it can change the dimensions of existing components designed around content-box sizing.
Form Controls
Many form controls use border-box in browser default styles, making their declared dimensions include padding and borders.
Full-Width Input
input[type="text"] {
box-sizing: border-box;
width: 100%;
padding: 12px;
border: 1px solid #cccccc;
}
Textarea
textarea {
box-sizing: border-box;
width: 100%;
min-height: 120px;
padding: 12px;
border: 1px solid #cccccc;
}
Select Element
select {
box-sizing: border-box;
width: 100%;
padding: 10px;
border: 1px solid #cccccc;
}
Form Button
button {
box-sizing: border-box;
min-width: 140px;
padding: 10px 20px;
border: 2px solid #1769aa;
}
Consistent Form Fields
.form-field {
box-sizing: border-box;
width: 100%;
padding: 12px;
border: 1px solid #cccccc;
border-radius: 4px;
}
Applying consistent box sizing helps text inputs, select controls, and textareas align within the same form layout.
Browser default styles can differ between control types, so explicit declarations are useful when consistent sizing is important.
Flexbox and Grid
The box-sizing property works with Flexbox and Grid, although those layout systems also apply their own sizing and space-distribution rules.
Flexbox Cards
.flex-container {
display: flex;
gap: 20px;
}
.flex-card {
box-sizing: border-box;
flex: 1;
min-width: 0;
padding: 20px;
border: 1px solid #dddddd;
}
Border-box sizing includes padding and borders within specified dimensions, while Flexbox determines how available space is distributed.
Grid Cards
.grid-container {
display: grid;
grid-template-columns: repeat(3, minmax(0, 1fr));
gap: 20px;
}
.grid-card {
box-sizing: border-box;
padding: 20px;
border: 1px solid #dddddd;
}
Responsive Grid
.grid-container {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(min(100%, 220px), 1fr));
gap: 20px;
}
.grid-card {
box-sizing: border-box;
padding: 20px;
}
Fixed-Width Flex Item
.sidebar {
box-sizing: border-box;
flex: 0 0 250px;
padding: 20px;
border: 1px solid #dddddd;
}
The sidebar's preferred flex basis is 250 pixels, and its padding and borders are handled according to the border-box sizing model.
Avoiding Overflow
.flex-card {
box-sizing: border-box;
min-width: 0;
overflow-wrap: anywhere;
}
Border-box sizing alone does not prevent all overflow. Long content, minimum-size rules, and layout constraints may still require additional styling.
Minimum and Maximum Dimensions
The box-sizing property also affects how specified minimum and maximum dimensions relate to padding and borders.
Maximum Width
.card {
box-sizing: border-box;
width: 100%;
max-width: 400px;
padding: 24px;
border: 2px solid #1769aa;
}
The border-box width is constrained by the 400-pixel maximum under normal sizing conditions.
Minimum Width
.button {
box-sizing: border-box;
min-width: 160px;
padding: 12px 24px;
border: 2px solid #1769aa;
}
Maximum Height
.panel {
box-sizing: border-box;
max-height: 300px;
padding: 20px;
overflow: auto;
}
Minimum Height
.card {
box-sizing: border-box;
min-height: 180px;
padding: 24px;
}
Content That Cannot Fit
.box {
box-sizing: border-box;
width: 50px;
padding: 30px;
border: 5px solid #1769aa;
}
The combined horizontal padding and borders exceed the declared width. The used border-box width must be large enough to contain them.
Minimum and maximum constraints still apply according to CSS sizing rules, and content may overflow when a constrained box is too small.
Responsive Design
Border-box sizing is useful for responsive layouts because padding and borders remain within declared dimensions.
Full-Width Responsive Card
.card {
box-sizing: border-box;
width: 100%;
max-width: 600px;
padding: 24px;
border: 1px solid #dddddd;
}
Responsive Padding
.card {
box-sizing: border-box;
width: 100%;
padding: 32px;
}
@media (max-width: 600px) {
.card {
padding: 16px;
}
}
The card's declared width remains unchanged while its content area adjusts to the padding.
Responsive Columns
.layout {
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
gap: 20px;
}
.layout-item {
box-sizing: border-box;
padding: 20px;
}
@media (max-width: 600px) {
.layout {
grid-template-columns: 1fr;
}
}
Fluid Width
.panel {
box-sizing: border-box;
width: min(100%, 800px);
padding: clamp(16px, 3vw, 32px);
}
Responsive Form
.form-control {
box-sizing: border-box;
width: 100%;
padding: 12px;
border: 1px solid #cccccc;
}
Container Query
.card-container {
container-type: inline-size;
}
.card {
box-sizing: border-box;
padding: 16px;
}
@container (min-width: 500px) {
.card {
padding: 32px;
}
}
Using border-box helps keep component dimensions predictable as padding changes across screen and container sizes.
Cascade and Inheritance
The box-sizing property follows normal CSS cascade rules and is not inherited automatically.
Later Declaration
.box {
box-sizing: content-box;
box-sizing: border-box;
}
The later declaration wins when cascade priority is otherwise equal.
Higher Specificity
.box {
box-sizing: border-box;
}
.box.special {
box-sizing: content-box;
}
Not Inherited
.parent {
box-sizing: border-box;
}
.child {
width: 200px;
padding: 20px;
}
The child does not automatically inherit border-box. Its sizing model is determined by its own applicable declarations and browser defaults.
Explicit Inheritance
.parent {
box-sizing: border-box;
}
.child {
box-sizing: inherit;
}
Reset to Initial
.box {
box-sizing: initial;
}
The value returns to content-box.
CSS Layers
@layer base, components;
@layer base {
.box {
box-sizing: border-box;
}
}
@layer components {
.box {
box-sizing: content-box;
}
}
For normal declarations, the later layer takes precedence.
When debugging unexpected dimensions, inspect the computed box-sizing value along with width, padding, border, and minimum-size constraints.
CSS Custom Properties
CSS custom properties can store sizing values for components that need different box-sizing behavior.
Shared Sizing Value
:root {
--component-box-sizing: border-box;
}
.component {
box-sizing: var(--component-box-sizing);
}
Fallback Value
.component {
box-sizing: var(--component-box-sizing, border-box);
}
Component Override
.component {
--component-box-sizing: border-box;
box-sizing: var(--component-box-sizing);
}
.component.content-sized {
--component-box-sizing: content-box;
}
Shared Width and Padding
:root {
--card-width: 320px;
--card-padding: 20px;
}
.card {
box-sizing: border-box;
width: var(--card-width);
padding: var(--card-padding);
}
Custom properties can help maintain consistent dimensions, although a global border-box reset is usually sufficient for most websites.
Practical Uses
The box-sizing property is useful whenever elements need predictable dimensions with padding and borders.
Global Reset
*,
*::before,
*::after {
box-sizing: border-box;
}
Responsive Card
.card {
box-sizing: border-box;
width: 100%;
max-width: 400px;
padding: 24px;
border: 1px solid #dddddd;
}
Full-Width Input
.form-input {
box-sizing: border-box;
width: 100%;
padding: 12px;
border: 1px solid #cccccc;
}
Fixed-Width Sidebar
.sidebar {
box-sizing: border-box;
width: 260px;
padding: 20px;
border-right: 1px solid #dddddd;
}
Equal-Sized Buttons
.button {
box-sizing: border-box;
width: 180px;
padding: 12px 20px;
border: 2px solid #1769aa;
}
Grid Items
.grid-item {
box-sizing: border-box;
width: 100%;
padding: 20px;
border: 1px solid #dddddd;
}
Fixed-Height Panel
.panel {
box-sizing: border-box;
height: 200px;
padding: 24px;
border: 2px solid #1769aa;
overflow: auto;
}
Content-Sized Element
.content-area {
box-sizing: content-box;
width: 500px;
padding: 20px;
}
Use border-box for predictable overall dimensions and content-box when an exact content-area size is required.
box-sizing Example
The following example compares two elements with identical width, padding, and border declarations. Only the box-sizing value changes.
<div class="box-sizing-demo">
<div class="demo-group">
<h3>content-box</h3>
<div class="demo-box content">
Width: 200px<br>
Total: 250px
</div>
</div>
<div class="demo-group">
<h3>border-box</h3>
<div class="demo-box border">
Width: 200px<br>
Total: 200px
</div>
</div>
</div>
.box-sizing-demo {
display: flex;
flex-wrap: wrap;
gap: 30px;
}
.box-sizing-demo .demo-group {
min-width: 0;
}
.box-sizing-demo h3 {
margin-bottom: 15px;
font-size: 16px;
}
.box-sizing-demo .demo-box {
width: 200px;
min-height: 110px;
padding: 20px;
border: 5px solid #1769aa;
background-color: #d8ecfa;
color: #173b5e;
text-align: center;
}
.box-sizing-demo .content {
box-sizing: content-box;
}
.box-sizing-demo .border {
box-sizing: border-box;
}
content-box
Total: 250px
border-box
Total: 200px
The content-box element has a total border-box width of 250 pixels because padding and borders are added to its declared width.
The border-box element remains 200 pixels wide because padding and borders are included within the declared width.
Important Considerations
Keep the following points in mind when using box-sizing:
- Purpose: Controls how specified width and height include padding and borders.
- Initial value:
content-box. - Inheritance: The property is not inherited automatically.
- Accepted values:
content-boxandborder-box. - content-box: Applies dimensions to the content area only.
- border-box: Includes padding and borders within specified dimensions.
- Padding: Included within declared dimensions only with
border-box. - Borders: Included within declared dimensions only with
border-box. - Margins: Always remain outside the border box.
- Default browser styles: Certain elements, including many form controls, use
border-boxby default. - Global reset: A universal border-box reset is common in modern stylesheets.
- Minimum dimensions: Padding and borders cannot force the content box to become negative.
- Automatic dimensions: The effect is most noticeable with explicitly specified or constrained dimensions.
- Flexbox and Grid: The property works alongside their layout and sizing rules.
- Responsive layouts:
border-boxhelps keep declared dimensions predictable. - Overflow: Box sizing alone does not prevent content or margin overflow.
- Animation: Changes between values are discrete.
- Browser support: Widely available in modern browsers.
For most modern website layouts, border-box simplifies dimension calculations and reduces unexpected changes caused by padding and borders.
Browser Support
The box-sizing property is widely available and supported in Chrome, Edge, Firefox, Safari, and other major browsers.
Modern browsers support both content-box and border-box without vendor prefixes.
.box {
box-sizing: border-box;
}
This declaration uses the standard property syntax and works in modern browsers.
Older browser versions may have required vendor prefixes, but these are generally unnecessary for current browser targets.
For current compatibility details, visit Can I Use? .
Specifications
The box-sizing property is defined in the CSS Sizing Module Level 3 specification.
The specification defines how content-box and border-box affect specified dimensions, including the relationship between content, padding, and borders.
