1. 布局
  2. 盒尺寸

Quick reference

Class
Properties
box-borderbox-sizing: border-box;
box-contentbox-sizing: content-box;

基本用法

¥Basic usage

包括边框和填充

¥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.

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

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

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

排除边框和填充

¥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 h-32 w-32 p-4 border-4 ...">
  <!-- ... -->
</div>

有条件地应用

悬停、聚焦和其他状态

Tailwind lets you conditionally apply utility classes in different states using variant modifiers. For example, use hover:box-content to only apply the box-content utility on hover.

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

For a complete list of all available state modifiers, check out the Hover, Focus, & Other States documentation.

断点和媒体查询

You can also use variant modifiers to target media queries like responsive breakpoints, dark mode, prefers-reduced-motion, and more. For example, use md:box-content to apply the box-content utility at only medium screen sizes and above.

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

To learn more, check out the documentation on Responsive Design, Dark Mode and other media query modifiers.