1. 布局
  2. box-sizing

布局

box-sizing

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

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

示例(Examples)

包括边框和填充(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.

这意味着一个100像素 × 100像素的元素,带有2像素的边框和四周4像素的内边距,将显示为100像素 × 100像素,其内部内容区域为88像素 × 88像素:

🌐 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:

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

Tailwind 使其成为我们 preflight 基础样式 中所有元素的默认设置。

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

排除边框和填充(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:

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

响应式设计(Responsive design)

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

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

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

TailwindCSS 中文网 - 粤ICP备13048890号