CSS Box Sizing 属性

CSS 的 box-sizing 属性指定了元素的 widthheight 如何计算。

box-sizing 属性让我们控制盒子模型的工作方式。它允许我们将 paddingborder 包含在元素的整体 widthheight 中。

根据盒子模型,网页上的每个元素都被视为一个矩形框。下图显示了盒子模型的布局。

CSS Box Sizing Layout

CSS box-sizing 语法

box-sizing 属性的语法如下:

box-sizing: content-box | border-box | initial | inherit;

这里,

  • content-box:根据元素的內容定义 widthheight,不包括 paddingborder
  • border-box:根据边框定义 widthheight,包括 paddingborder
  • initial:设置为默认值,即 content-box
  • inherit:继承父元素的值。

无 Box-sizing 时的宽度和高度

默认情况下,widthheight 属性应用于元素的內容。实际的 widthheight 计算如下:

actual width: border-left + padding-left + width + padding-right + border-right

actual height: border-top + padding-top + height + padding-bottom + border-bottom

添加 paddingborder 后,这会导致 widthheight 大于实际指定的数值。

让我们看一个例子,

<!DOCTYPE html>
<html lang="en">

    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <link rel="stylesheet" href="style.css" />
        <title>CSS box-model</title>
    </head>

    <body>
        <div>
            This div element has a width of 300px and a height of 200px. By
            default, the width and height are applied to the content of an element
            i.e. box-sizing is set to content-box.
        </div>
    </body>

</html>
div {
    width: 300px;
    height: 200px;
    padding: 20px;
    border: 10px solid black;
    margin: 20px;

    /* default value */
    box-sizing: content-box;

    background-color: greenyellow;
    /* clips the background color to content only */
    background-clip: content-box;
}

浏览器输出

CSS Box Sizing Content Box Example

在上面的示例中,尽管我们将 width 设置为 300pxheight 设置为 200px,但实际渲染的 widthheight 为:

actual width = 10px + 20px + 300px + 20px + 10px
actual height = 10px + 20px + 200px + 20px + 10px

因此,实际的 widthheight 分别设置为 360px260px


有 Box-sizing 时的宽度和高度

box-sizing 属性的 border-box 值允许我们将 paddingborder 包含在指定的 widthheight 值中。例如:

<!DOCTYPE html>
<html lang="en">

    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <link rel="stylesheet" href="style.css" />
        <title>CSS box-model</title>
    </head>

    <body>
        <div>
            This div element has a width of 300px and a height of 200px. By
            default, the width and height are applied to the content of an
            element i.e. box-sizing is set to content-box.
        </div>
    </body>

</html>
div {
    width: 300px;
    height: 200px;
    padding: 20px;
    border: 10px solid black;
    margin: 20px;

    box-sizing: border-box;
    background-color: greenyellow;

    /* clips the background color to content only */
    background-clip: content-box;
}

浏览器输出

CSS Box Sizing Border Box Example

在上面的示例中,box-sizing: border-box 允许将 paddingborder 包含在指定的 widthheight 中。

因此,divwidthheight 将精确地保持为 300px200px

开发人员通常更喜欢使用 border-box 值,因为它有助于控制元素的大小,从而为设计提供一致且可预测的结果。

注意:我们可以使用通用选择器 (*) 将 box-sizing 设置为所有元素的 border-box。例如:

* {
    box-sizing: border-box;
}

我们的高级学习平台,凭借十多年的经验和数千条反馈创建。

以前所未有的方式学习和提高您的编程技能。

试用 Programiz PRO
  • 交互式课程
  • 证书
  • AI 帮助
  • 2000+ 挑战