CSS Box Model
The CSS box model describes how every element on a page takes up space. The browser treats each element as a rectangle made of four layers: the content itself, padding around it, a border around the padding, and margin outside the border. Understanding these layers is what lets you predict an element's real size and fix gaps that seem to appear from nowhere.
The four layers from the inside out
- Content: the text, image or child elements. Its size is set by
widthandheight, or by the content itself. - Padding: space between the content and the border. It takes the element's background colour.
- Border: a line around the padding, with its own width, style and colour.
- Margin: transparent space outside the border that pushes neighbouring elements away.
Open your browser's developer tools, select any element and look for the box diagram in the Styles or Computed panel. It shows the exact pixels used by each layer and is the fastest way to debug spacing.
How the total width adds up
By default, CSS uses box-sizing: content-box. In that mode, width applies only to the content, and padding and border are added on top:
.card {
width: 300px;
padding: 20px;
border: 5px solid #333;
margin: 10px;
}
/* content-box: visible width = 300 + 20*2 + 5*2 = 350px
space taken including margin = 350 + 10*2 = 370px */
.card-fixed {
box-sizing: border-box;
width: 300px;
padding: 20px;
border: 5px solid #333;
}
/* border-box: visible width stays 300px,
content shrinks to 300 - 40 - 10 = 250px */
Most developers prefer border-box because the width you write is the width you see. A common habit is to set it for everything at the top of the stylesheet:
*, *::before, *::after {
box-sizing: border-box;
}
Shorthand values for each side
Padding and margin accept one to four values, read clockwise starting from the top:
| Value | Meaning |
|---|---|
margin: 10px; | All four sides 10px. |
margin: 10px 20px; | Top and bottom 10px, left and right 20px. |
margin: 10px 20px 30px; | Top 10px, sides 20px, bottom 30px. |
margin: 10px 20px 30px 40px; | Top, right, bottom, left. |
The value margin: 0 auto; on a block element with a set width centres it horizontally, because the browser splits the leftover space equally.
The surprise of collapsing margins
Here is the pitfall that confuses nearly every beginner. When two block elements sit one above the other, their vertical margins do not add together. A paragraph with margin-bottom: 20px followed by one with margin-top: 30px ends up 30px apart, not 50px. The larger margin wins. The same thing happens between a parent and its first child: a child's top margin can "leak" out and push the parent down instead of creating space inside it.
Margin collapsing only affects vertical margins of normal block elements. It does not happen with horizontal margins, inside flex or grid containers, or when the parent has padding, a border or display: flow-root. If a gap behaves oddly, check whether collapsing is at work before adding more margin.
Inline elements are different
Inline elements such as <span> and <a> ignore width and height, and their vertical margins do not push other lines away. Vertical padding paints a background but does not move surrounding text. To make an inline element respect the full box model, change it to display: inline-block or block, a topic covered in the display lesson.
What to keep in mind
Every element is content plus padding, border and margin. Use border-box so widths stay predictable, read shorthand values clockwise from the top, and remember that vertical margins between blocks collapse to the larger value. The developer tools box diagram will confirm all of this on any page you inspect.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.