CSS Box Model

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

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

HTML
<style>
.box {
  padding: 20px;  /* 20px on all sides */
  background: #fce4ec;
  border: 2px solid #e91e63;
}
</style>
<div class="box">This box element has padding creating 20px of space between content and border</div>
▶ Try it Yourself

3. Border

The border line that wraps around the padding and content area.

Example

HTML
<style>
.box {
  border: 2px solid #333;
  padding: 16px;
  background: #f9f9f9;
}
</style>
<div class="box">This box element has a border wrapping around the outside of padding</div>
▶ Try it Yourself

4. Margin

The space between the border and other elements, used to control distance between elements.

Example

HTML
<style>
.box {
  margin: 10px;  /* 10px on all sides */
  background: #e8f5e9;
  border: 1px solid #66bb6a;
  padding: 10px;
}
</style>
<div class="box">Box 1 - has 10px margin</div>
<div class="box">Box 2 - 20px gap from Box 1</div>
<div class="box">Box 3 - 20px gap from Box 2</div>
▶ Try it Yourself
💡 Note: Top and bottom margins of block-level elements "collapse" (take the maximum value instead of adding). Left and right margins don't collapse.

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

HTML
<style>
.box {
  width: 200px;
  height: 100px;
  padding: 20px;
  border: 2px solid black;
  background: #e3f2fd;
  text-align: center;
  line-height: 1.5;
}
</style>
<div class="box">
  Content area 200×100px<br>
  Actual rendering 244×144px
</div>
▶ Try it Yourself

This box's actual rendering size is:

If you thought this box was only 200×100, you're wrong! Many beginners stumble here.

box-sizing Property

To solve the "actual size is larger than set" problem, CSS3 introduced the box-sizing property.

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."

border-box

width and height include content + padding + border; border and padding won't "expand" the element:

Example: content-box vs border-box Comparison

HTML
<style>
.container {
  display: flex;
  justify-content: space-around;
}
.box {
  width: 300px;
  height: 200px;
  border: 10px solid indianred;
}
.item {
  width: 100%;
  background: rgba(49, 115, 187, 0.3);
  border: solid #5B6DCD 10px;
  padding: 15px;
}
.s1 { box-sizing: content-box; }
.s2 { box-sizing: border-box; }
</style>
<div class="container">
  <div class="box">
    <h3>content-box (default)</h3>
    <div class="item s1">width: 100% + padding + border = overflows parent</div>
  </div>
  <div class="box">
    <h3>border-box</h3>
    <div class="item s2">width: 100% includes padding + border = fits perfectly</div>
  </div>
</div>
▶ Try it Yourself
💡 Comparison: In content-box mode, child elements overflow the parent; in border-box mode, child elements fit perfectly. This is why modern projects recommend globally setting box-sizing: border-box.

Global border-box Setting

Many projects set this globally in their reset:

Example

HTML
<style>
*, *::before, *::after {
  box-sizing: border-box;
}
</style>
<div class="box-demo">Demo box</div>
▶ Try it Yourself

Debugging the Box Model

Use browser developer tools to visually inspect the box model:

💡 Debugging tip: Press F12 to open Developer Tools, then in the Elements panel's Computed tab, you can visually see each element's content, padding, border, and margin — this is the fastest way to diagnose layout issues.

  1. Right-click an element on the page, select "Inspect"
  2. Select the element in the Elements panel
  3. Switch to Computed or Layout panel
  4. You'll see a box model diagram showing content, padding, border, and margin values

Box Model Differences Across Element Types


❓ FAQ

Q Should I use content-box or border-box?
A Most projects choose `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.
Q How do I view the box model in a browser?
A Press F12 to open Developer Tools, select an element in the Elements panel, and the right-side Computed or Styles panel will show the box model's four-layer structure (content/padding/border/margin). This is the fastest way to diagnose layout issues — strongly recommended to make it a habit.
Q Both margin and padding create spacing — when should I use which?
A Remember one principle: should the background color cover this area? With padding, the background covers it; with margin, it doesn't. Simply put: spacing inside an element uses padding; spacing between elements uses margin.

📖 Summary

📝 Exercises

Basic

  1. 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.
  2. Copy the box above, add box-sizing: border-box, observe actual size changes, and compare with the previous exercise.

Intermediate

  1. 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).
  2. Use border-box to create a "two-column layout with 50% width each and 20px gap between them."

Challenge

  1. Create a card component with:
    • Fixed width of 300px
    • 20px padding
    • 1px solid #ddd border
    • 8px border-radius
    • Use border-box to ensure total width is always 300px
  2. Create a form layout using box model calculations to perfectly align input fields and buttons.
100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏