CSS: CSS Box Model
Last updated: 2026-10-13
The Box Model is the foundation of CSS layout. In the CSS world, every HTML element is treated as a rectangular "box" composed of content area, padding, border, and margin.
Box Model Properties Quick Reference
| Property | Common Values | Purpose |
|---|---|---|
width |
300px, 50%, auto |
Content area width |
height |
200px, auto |
Content area height |
padding |
20px, 10px 20px, 10px 20px 15px 5px |
Inner spacing (content to border) |
border |
2px solid #333 |
Border (width + style + color) |
margin |
10px, 20px auto, 0 auto |
Outer spacing (element to other elements) |
box-sizing |
content-box (default), border-box |
Width/height calculation scope |
1. Four Parts of the Box Model
From inside to outside:
┌────────────────────────────────────┐ ← Margin
│ ┌──────────────────────────────┐ │ ← Border
│ │ ┌─────────────────────────┐ │ │ ← Padding
│ │ │ │ │ │
│ │ │ Content Area │ │ │
│ │ │ │ │ │
│ │ └────────────────────────┘ │ │
│ └──────────────────────────────┘ │
└────────────────────────────────────┘
(1) Content Area
The area where actual content resides, such as text and images. Width and height are controlled by the width and height properties.
(2) Padding
The space between the content area and the border, used to create whitespace between content and border.
▶ Example
▶ Try it Yourself(3) Border
The border line that wraps around the padding and content area.
▶ Example
▶ Try it Yourself(4) Margin
The space between the border and other elements, used to control distance between elements.
▶ Example
▶ Try it Yourself2. Actual Element Size
This is the most confusing part — an element's width and height don't equal the content area dimensions.
By default (box-sizing: content-box):
Total element width = width + padding-left + padding-right + border-left + border-right
Total element height = height + padding-top + padding-bottom + border-top + border-bottom
For example:
▶ Example
▶ Try it YourselfThis box's actual rendering size is:
- Total width = 200 + 20×2 + 2×2 = 244px
- Total height = 100 + 20×2 + 2×2 = 144px
If you thought this box was only 200×100, you're wrong! Many beginners stumble here.
3. box-sizing Property
To solve the "actual size is larger than set" problem, CSS3 introduced the box-sizing property.
(1) content-box (default)
width and height only include the content area; padding and border are not included. This is the default and the source of the "trap."
(2) border-box
width and height include content + padding + border; border and padding won't "expand" the element:
▶ Example: content-box vs border-box Comparison
▶ Try it Yourselfbox-sizing: border-box.
(3) Global border-box Setting
Many projects set this globally in their reset:
▶ Example
▶ Try it Yourself4. Debugging the Box Model
Use browser developer tools to visually inspect the box model:
- Right-click an element on the page, select "Inspect"
- Select the element in the Elements panel
- Switch to Computed or Layout panel
- You'll see a box model diagram showing content, padding, border, and margin values
5. Box Model Differences Across Element Types
- Block-level elements: Default width is 100% of parent container; height determined by content
- Inline elements: Don't accept width and height; padding/margin only effective horizontally
- Inline-block elements: Can set width/height, but don't occupy a full line
❓ FAQ
border-box. With border-box, padding and border won't expand element dimensions, making layout calculations more intuitive. Many frameworks (Bootstrap, Tailwind) globally set border-box. Unless you have special needs, don't use the default content-box.📖 Summary
- The Box Model is the most fundamental and important CSS concept
- Understanding the four-layer structure of content, padding, border, and margin — and how
box-sizingaffects dimension calculations — is a prerequisite for writing reliable layouts - Setting
box-sizing: border-boxas a global default at the start of a project saves much trouble with dimension calculations
📝 Exercises
Basic
- Create a 200×200 box with padding of 30px and border of 5px solid. Use browser developer tools to check actual rendering dimensions and verify the box model formula.
- Copy the box above, add
box-sizing: border-box, observe actual size changes, and compare with the previous exercise.
Intermediate
- Create a page with two side-by-side boxes, adjust their margins, and observe the "margin collapse" phenomenon (block-level elements' top/bottom margins take the maximum instead of adding).
- Use
border-boxto create a "two-column layout with 50% width each and 20px gap between them."
Challenge
- Create a card component with:
- Fixed width of 300px
- 20px padding
- 1px solid #ddd border
- 8px border-radius
- Use
border-boxto ensure total width is always 300px
- Create a form layout using box model calculations to perfectly align input fields and buttons.