1. 布局
  2. box-sizing

布局

box-sizing

用于控制浏览器如何计算元素总大小的工具。

样式
box-border
box-sizing: border-box
box-content
box-sizing: content-box

示例(Examples)

¥Examples

包括边框和填充(Including borders and padding)

¥Including borders and padding

使用 box-border 工具将元素的 box-sizing 设置为 border-box,告诉浏览器在指定高度或宽度时包含元素的边框和填充。

¥Use the box-border utility to set an element's box-sizing to border-box, telling the browser to include the element's borders and padding when you give it a height or width.

这意味着一个 100px × 100px 元素,其所有边都有 2px 边框和 4px 填充,实际上将被渲染为 100px × 100px,其内部内容区域为 88px × 88px:

¥This means a 100px × 100px element with a 2px border and 4px of padding on all sides will be rendered as 100px × 100px, with an internal content area of 88px × 88px:

128px
128px
<div class="box-border size-32 border-4 p-4 ...">
<!-- ... -->
</div>

Tailwind 使它成为我们 预检基础样式 中所有元素的默认值。

¥Tailwind makes this the default for all elements in our preflight base styles.

排除边框和填充(Excluding borders and padding)

¥Excluding borders and padding

使用 box-content 工具将元素的 box-sizing 设置为 content-box,告诉浏览器在元素的指定宽度或高度之上添加边框和填充。

¥Use the box-content utility to set an element's box-sizing to content-box, telling the browser to add borders and padding on top of the element's specified width or height.

这意味着一个 100px × 100px 元素,其所有边都有 2px 边框和 4px 填充,实际上将被渲染为 112px × 112px,其内部内容区域为 100px × 100px:

¥This means a 100px × 100px element with a 2px border and 4px of padding on all sides will actually be rendered as 112px × 112px, with an internal content area of 100px × 100px:

128px
128px
<div class="box-content size-32 border-4 p-4 ...">
<!-- ... -->
</div>

响应式设计(Responsive design)

¥Responsive design

box-sizing 工具前面使用断点变体如 md: 仅在 medium 屏幕尺寸及以上时应用工具:

<div class="box-content md:box-border ...">
<!-- ... -->
</div>

请参阅 变体文档 详细了解如何使用变体。

TailwindCSS v4.0 中文网 - 粤ICP备13048890号