CSS Box Model
In CSS, every element is a rectangular box, and the box model describes the layers of that box.
The Four Layers
From the inside to the outside, every box has:
- Content: the text, image or other things inside
- Padding: clear space around the content
- Border: a line around the padding
- Margin: clear space outside the border, separating it from other boxes
Example
<!DOCTYPE html>
<html>
<head>
<style>
.box {
width: 200px;
padding: 20px;
border: 5px solid #264de4;
margin: 25px;
background-color: #e7ecff;
}
</style>
</head>
<body>
<div class="box">Content area. The light blue covers the content and the padding. The margin is the empty space outside the blue border.</div>
</body>
</html>Try it Yourself »Right-click the box in Chrome and choose Inspect. In the Computed tab you will see a diagram of the box with its margin, border, padding and content sizes. It is the best way to understand spacing problems.
Width and Height
width and height set the size of the box. You can use pixels (px), percentages (%) of the parent, or other units.
Example
.photo-frame {
width: 50%;
height: 200px;
}max-width stops a box from getting wider than a limit. It is very useful for text and images on large screens:
Example
.article {
max-width: 700px;
}Working Out the Total Size
By default, width is the width of the content only. Padding and border are added on top. For the box above:
| Part | Left + right |
|---|---|
| Content width | 200px |
| Padding | 20px + 20px = 40px |
| Border | 5px + 5px = 10px |
| Total visible width | 250px |
| Margin (outside) | 25px + 25px = 50px |
So a box with width: 200px actually takes 250px of the screen, plus its margin. This surprises many beginners.
box-sizing: border-box
With box-sizing: border-box, the width includes the padding and border. A box with width: 200px is then exactly 200px wide, and the content gets smaller to make room.
Example
<!DOCTYPE html>
<html>
<head>
<style>
div {
width: 200px;
padding: 20px;
border: 5px solid black;
margin-bottom: 10px;
}
.normal {
box-sizing: content-box;
background-color: #ffe0e0;
}
.border-box {
box-sizing: border-box;
background-color: #e0ffe6;
}
</style>
</head>
<body>
<div class="normal">content-box: 250px wide</div>
<div class="border-box">border-box: 200px wide</div>
</body>
</html>Try it Yourself »Most developers turn this on for every element at the start of their CSS:
Example
*, *::before, *::after {
box-sizing: border-box;
}Now sizes are much easier to plan. If you want two boxes side by side at 50% each, padding will no longer push the second box onto a new line.
Overflow
If the content is bigger than the box, it spills out. The overflow property decides what happens:
| Value | Result |
|---|---|
visible | Content spills outside (the default) |
hidden | Extra content is cut off |
scroll | Scroll bars are always shown |
auto | Scroll bars appear only when needed |
Exercise
Exercise
A box has width: 300px, padding: 10px and border: 2px solid, with the default box-sizing. How wide does it look on the screen?
