CSS: CSS Box Model

🗓 Last updated: 2026-11-30👤 Written by: Blake✓ Reviewed by: MOTO🏷 Category: CSS Tutorial

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 How wide does a box with width: 200px actually render? Do padding and border count?
A With default content-box, actual width = width + left/right padding + left/right border — padding: 10px plus border: 1px makes it 222px. Switch to box-sizing: border-box and padding/border squeeze inside the 200px. If your layout mysteriously gets wider, nine times out of ten border-box is missing.
Q What's the difference between margin and padding, and when do I use each?
A Use the background as your guide: padding sits inside the element, so the background color covers it; margin sits outside and is transparent. Gap between text and border = padding; gap between two elements = margin. When unsure, ask "should this gap have a background color?"
Q What does box-sizing: border-box mean, and why does everyone set it globally?
A It changes width/height from "content only" to "content + padding + border total", so padding never inflates the element. Bootstrap and Tailwind default to border-box. Global reset: *, *::before, *::after { box-sizing: border-box; } — add it at the start of every new project.
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 need strict "content width" semantics, don't use the default content-box.
Q How do I inspect an element's box model in browser DevTools?
A Press F12, select the element in the Elements panel, and the bottom of Styles/Computed shows a color-coded diagram: blue content, green padding, yellow border, orange margin. Hover each layer for exact values; the page highlights matching regions. Always start here when debugging spacing — faster than guessing.
Q Adding padding stretched my element and broke the layout — how do I fix it?
A That's default content-box behavior: padding and border are added outside width. Two fixes: add box-sizing: border-box, or manually reduce width (target − 2×padding − 2×border). A global border-box rule makes this class of bugs disappear.
Q How do negative margins work, and when are they useful?
A A negative margin pulls the element in that direction — margin-left: -15px cancels a parent's 15px padding for a full-bleed effect. It's also used in equal-height multi-column tricks. Use sparingly: lots of negative margins usually means the wrong layout tool; flex or grid reads better.
Q My image has width: 100% but still overflows its container — is that box model related?
A Yes. Images default to content-box; with any padding, actual footprint = 100% + padding, guaranteed overflow. Standard fix: img { max-width: 100%; height: auto; box-sizing: border-box; }. Check these three whenever an image breaks its container.
Q Spacing looks wrong and things won't align — how do I quickly check if it's a box model issue?
A Hover the element in DevTools and the page shows color layers — blue (content), green (padding), yellow (border), orange (margin). Wrong spacing usually means margin collapse, stacked padding, or missing border-box inflating sizes. Then verify the actual box-sizing value and total size in the Computed panel.

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

Content reviewed by MOTO · Found an error? Report it

100%

🙏 帮我们做得更好

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

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