Skip to content

CSS Box Model

理解 CSS box model — 内容, padding, 边框, margin — and box-sizing.

你将学到

  • four layers 的 box model: 内容, padding, 边框, margin
  • 如何 box-sizing: 边框-box 更改宽度 calculations
  • difference 之间 margin collapse and padding behavior
  • 如何 调试 spacing 使用浏览器 DevTools

概念讲解

CSS Box Model

每个element on a web page is a rectangular box. The box model describes the four layers of that box, from innermost to outermost: content, padding, border, and margin. Understanding these layers is essential — almost every layout issue a beginner faces (things looking squished, misaligned, or overflowing) traces back to the box model.

The Four Layers

  1. Content — the actual text or image. Its size is determined by 宽度 and 高度 (or by the content itself if not set).
  2. Padding — space *inside* the border, between the content and the border. Padding is part of the element's clickable area.
  3. Border — the line around the padding. It has a width, style, and color.
  4. Margin — space *outside* the border, pushing neighboring elements away. Margin is transparent and not part of the element's own area.

box-sizing: border-box

By default, 宽度 sets only the *content* width. Padding and border are *added on top*, so a 宽度: 100px element with padding: 10px and 边框: 1px is actually 122px wide. This causes layouts to overflow unexpectedly. Setting box-sizing: 边框-box makes 宽度 include padding and border, so 宽度: 100px means the *total* box is 100px. Nearly every project applies box-sizing: 边框-box globally with a reset.

Margin Collapse

When two vertical margins meet, they collapse — the space between them is the *larger* of the two, not their sum. A 20px margin below an element and a 30px margin above the next element produce 30px of space, not 50px. Margin collapse does not happen with horizontal margins, padding, or flex/grid children. It is a common source of confusion when spacing does not add up as expected.

Debugging with DevTools

The browser DevTools Elements panel shows the box model visually — a color-coded diagram with the content size in the center and padding, border, and margin as concentric rectangles. You can edit the values live and see the result instantly. When spacing looks wrong, this diagram is the first place to look.

示例代码

              /* Universal reset: border-box for everything */
*, *::before, *::after {
  box-sizing: border-box;
}

.card {
  width: 300px;          /* total width, includes padding + border */
  padding: 20px;         /* space inside, part of the 300px */
  border: 2px solid #e5e7eb;
  margin: 24px;          /* space outside, pushes neighbors away */
  background: #ffffff;
  border-radius: 8px;
}

.card-content {
  padding: 12px 16px;    /* vertical | horizontal */
  margin-bottom: 16px;   /* space below, may collapse with next margin */
}
            

universal reset applies 边框-box 所以宽度包含 padding and 边框. card 是精确地 300px wide 总计. Padding 创建内部空间; margin 创建外部 separation. shorthand padding: 12px 16px 设置 vertical and horizontal 值.

动手试一试

  • 镜像颜色 Picker

    选取边框 and background 颜色 for 你的 box model 元素.

  • SVG Optimizer

    SVG 元素 follow box model 也 — optimize and inspect 它们的维度.

常见错误

错误写法

Forgetting box-sizing: 边框-box and wondering 为什么元素 overflow 它们的容器.

正确做法

Apply *, *::之前, *::之后 { box-sizing: 边框-box; } at 顶部的 你的 CSS. 这个制作宽度包含 padding and 边框, preventing unexpected 大小 growth.

错误写法

Confusing margin (之外) 使用 padding (内部) 当 spacing 元素.

正确做法

Padding 添加空间内部边框, growing clickable 区域. Margin 添加空间之外, pushing neighbors 远离. Use padding for 内部 breathing 空间 and 轻触目标; use margin for separation 之间 distinct 元素.

相关代码片段

相关速查表

练习 CSS Box Model

7 个练习

练习 CSS Box Model

查阅不认识的术语

新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。

查看术语表

动手做项目

通过引导式项目应用所学,含起始代码与解决方案。

查看项目

解读错误信息

用大白话解释常见错误——什么意思、为什么发生、如何修复。

查看错误