Skip to content

Box Model

一句话解释

box model 描述如何每个 HTML 元素是 rectangular box 使用内容, padding, 边框, and margin.

用大白话说

每个HTML element on a page is a rectangular box. The CSS box model describes the parts of this box: the content area (where text or images go), padding (space inside the border), border (the edge), and margin (space outside the border).

The total size of an element is: content + padding + border + margin. By default (box-sizing: 内容-box), the 宽度 property only sets the content width, so padding and border add to the total. Setting box-sizing: 边框-box includes padding and border in the declared width, which is usually more intuitive.

Understanding the box model is essential for controlling spacing, alignment, and sizing in CSS. Most CSS resets set box-sizing: 边框-box on all elements to make sizing more predictable.

示例

css
div {
  box-sizing: border-box;
  width: 200px;
  padding: 20px;
  border: 5px solid black;
  margin: 10px;
}
/* Total width with border-box: 200px (content+padding+border) */
/* Plus 20px margin (10px each side) outside the border */

使用边框-box, 200px 宽度包含内容, padding, and 边框. Margin 是总是之外.

相关术语

相关资源

相关课程

解读错误信息

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

查看错误

← 返回术语表