Box Sizing
Content-Box
The default box model in CSS is content-box. This means that when you specify a width or height on an element, you’re only defining the content area (the area inside the padding and border). The padding and border are added to the total size of the element.
For example:
<code class="language-css">.element {
<p> width: 100px;</p>
<p> padding: 10px;</p>
<p> border: 5px solid black;</p>
<p>}</code>
Here, the element’s total width (including padding and border) will be 100px + 10px (padding-left) + 10px (padding-right) + 5px (border-left) + 5px (border-right) = 130px. The width: 100px only refers to the content area.
This model is the default for most browsers and is used when you want precise control over the content area without unexpected padding/border additions.
Border-Box
In contrast, border-box is a model where the width and height you specify include the padding and border. This is a powerful model for creating consistent layouts, especially when you want to set a total width that includes all decorations.
To use border-box, you set box-sizing: border-box on the element. This is especially useful for responsive design and avoiding complex width calculations.
Why Border-Box Matters
Border-box is a game-changer for layout consistency. For instance, when building a responsive card component, you might want a fixed width that includes padding and border so the card maintains the same visual size across devices. Without border-box, you’d have to subtract padding and border from your width calculations, leading to inconsistent behavior and harder-to-maintain code.
Practical Example: Border-Box in Action
Here’s a real-world comparison:
<code class="language-html"><div class="content-box"> <p> Content area only</p> <p></div></p> <p><div class="border-box"></p> <p> Content area + padding + border = 100px total</p> <p></div></code>
<code class="language-css">.content-box {
<p> width: 100px;</p>
<p> padding: 10px;</p>
<p> border: 5px solid black;</p>
<p> box-sizing: content-box;</p>
<p>}</p>
<p>.border-box {</p>
<p> width: 100px;</p>
<p> padding: 10px;</p>
<p> border: 5px solid black;</p>
<p> box-sizing: border-box;</p>
<p>}</code>
In this example:
- The
content-boxelement has a total width of130px(100px content + 10px left + 10px right + 5px top + 5px bottom) - The
border-boxelement has a total width of100px(the specified width includes all padding/border)
Comparing Content-Box and Border-Box
| Property | Content-Box (Default) | Border-Box |
|---|---|---|
width definition |
Only content area | Content + padding + border |
| Total width | width + padding + border |
width (specified value) |
| Common use case | Traditional layouts, precise content control | Responsive design, consistent sizing |
| Example | width: 100px → content = 100px, total = 130px |
width: 100px → total = 100px |
Summary
In this section, we’ve explored the two fundamental box sizing models: content-box (the default) and border-box. Understanding when to use each is key to creating robust and responsive layouts. Content-box gives precise control over the content area, while border-box simplifies sizing by including padding and borders in the specified width. ✅