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

Properties in this section: width height padding border margin box-sizing

1. Four Parts of the Box Model

From inside to outside:

TEXT
┌────────────────────────────────────┐  ← 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 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.


2. 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):

TEXT
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 Yourself

This box's actual rendering size is:

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

(3) Global border-box Setting

Many projects set this globally in their reset:

▶ Example

▶ Try it Yourself

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

5. Box Model Differences Across Element Types


Properties in this section: width height padding border margin box-sizing

❓ 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.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

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

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