12. CSS Box Model
What you will learn
- The four layers of every element’s box
- How to calculate an element’s real size
- What
box-sizing: border-boxdoes and why it is so popular
Everything is a box
The browser treats every HTML element as a rectangular box. Each box has four layers, from the inside out:
- Content: the text or image itself.
- Padding: space around the content, inside the border.
- Border: a line around the padding.
- Margin: space outside the border, separating the box from its neighbours.
The layers (a diagram built with CSS)
Total size, the default way
With the default setting (box-sizing: content-box), width and height apply to the content only. The total space used is:
Formula
total width = width + left padding + right padding + left border + right border (+ left and right margin)
Example
<style>
.box {
width: 300px;
padding: 20px;
border: 5px solid #082b59;
margin: 10px;
background-color: #cfe6ff;
}
</style>
<div class="box">Content box</div>
| Part | Calculation |
|---|---|
| Visible box width | 300 + 20 + 20 + 5 + 5 = 350px |
| Space used with margins | 350 + 10 + 10 = 370px |
You asked for 300px but the box is 350px wide. That surprise is why box-sizing exists.
border-box: the friendlier way
With box-sizing: border-box, the width and height include the padding and border. A width: 300px box is then exactly 300px wide, and the browser shrinks the content area to fit.
Example
<style>
.box {
box-sizing: border-box;
width: 300px;
padding: 20px;
border: 5px solid #082b59;
background-color: #cfe6ff;
}
</style>
<div class="box">Border box: exactly 300px wide</div>
Output
The common global reset
Most developers apply border-box to everything at the top of their stylesheet:
Example
*, *::before, *::after {
box-sizing: border-box;
}
After this, sizes behave the way you would expect: you set the width, and padding and borders no longer make the box bigger.
Inspect the box in your browser
- Open any page and press F12.
- Click the element picker, then click an element.
- Find the Computed or Layout tab. It draws the box model with the real numbers for margin, border, padding, and content.
This is the best way to debug spacing. When something looks wrong, inspect it and read the numbers.
Common mistakes
- Setting
width: 100%plus padding on acontent-boxelement, causing sideways scrolling. - Forgetting that borders add to the size.
- Confusing margin (outside) with padding (inside).
- Not using
border-boxand fighting with sizes.
Exercise
As written, the box is 250px wide in total (200 + 20 + 20 + 5 + 5). Change the CSS so it is exactly 200px wide, without changing the width, padding, or border.
Show solution
<style>
.box {
box-sizing: border-box;
width: 200px;
padding: 20px;
border: 5px solid navy;
background-color: #eaf5ff;
}
</style>
<div class="box">How wide am I?</div>
Knowledge check
1. Name the four layers of the box model, from inside to outside.
Content, padding, border, margin.
2. A content-box element has width 100px, padding 10px, and border 2px. What is its visible width?
100 + 10 + 10 + 2 + 2 = 124px.
3. What does box-sizing: border-box change?
Width and height include the padding and border.
Summary
- Every element is a box: content, padding, border, margin.
- By default, padding and border add to the width.
box-sizing: border-boxmakes sizes predictable, so use it everywhere.