尺寸

width

用于设置元素宽度的工具。

样式
w-<number>
width: calc(var(--spacing) * <number>);
w-<fraction>
width: calc(<fraction> * 100%);
w-3xs
width: var(--container-3xs); /* 16rem (256px) */
w-2xs
width: var(--container-2xs); /* 18rem (288px) */
w-xs
width: var(--container-xs); /* 20rem (320px) */
w-sm
width: var(--container-sm); /* 24rem (384px) */
w-md
width: var(--container-md); /* 28rem (448px) */
w-lg
width: var(--container-lg); /* 32rem (512px) */
w-xl
width: var(--container-xl); /* 36rem (576px) */
w-2xl
width: var(--container-2xl); /* 42rem (672px) */

示例(Examples)

¥Examples

基本示例(Basic example)

¥Basic example

使用 w-<number> 工具(如 w-24w-64)根据间距比例将元素设置为固定的宽度:

¥Use w-<number> utilities like w-24 and w-64 to set an element to a fixed width based on the spacing scale:

w-48
w-40
w-32
w-24
<div class="w-96 ...">w-96</div>
<div class="w-80 ...">w-80</div>
<div class="w-64 ...">w-64</div>
<div class="w-48 ...">w-48</div>
<div class="w-40 ...">w-40</div>
<div class="w-32 ...">w-32</div>
<div class="w-24 ...">w-24</div>

使用百分比(Using a percentage)

¥Using a percentage

使用 w-fullw-<fraction> 工具(如 w-1/2w-2/5)为元素提供基于百分比的宽度:

¥Use w-full or w-<fraction> utilities like w-1/2 and w-2/5 to give an element a percentage-based width:

w-1/2
w-1/2
w-2/5
w-3/5
w-1/3
w-2/3
w-full
<div class="flex ...">
<div class="w-1/2 ...">w-1/2</div>
<div class="w-1/2 ...">w-1/2</div>
</div>
<div class="flex ...">
<div class="w-2/5 ...">w-2/5</div>
<div class="w-3/5 ...">w-3/5</div>
</div>
<div class="flex ...">
<div class="w-1/3 ...">w-1/3</div>
<div class="w-2/3 ...">w-2/3</div>
</div>
<div class="flex ...">
<div class="w-1/4 ...">w-1/4</div>
<div class="w-3/4 ...">w-3/4</div>
</div>
<div class="flex ...">
<div class="w-1/5 ...">w-1/5</div>
<div class="w-4/5 ...">w-4/5</div>
</div>
<div class="flex ...">
<div class="w-1/6 ...">w-1/6</div>
<div class="w-5/6 ...">w-5/6</div>
</div>
<div class="w-full ...">w-full</div>

使用容器比例(Using the container scale)

¥Using the container scale

使用 w-smw-xl 等工具根据容器比例将元素设置为固定的宽度:

¥Use utilities like w-sm and w-xl to set an element to a fixed width based on the container scale:

w-sm
w-xs
w-2xs
w-3xs
<div class="w-xl ...">w-xl</div>
<div class="w-lg ...">w-lg</div>
<div class="w-md ...">w-md</div>
<div class="w-sm ...">w-sm</div>
<div class="w-xs ...">w-xs</div>
<div class="w-2xs ...">w-2xs</div>
<div class="w-3xs ...">w-3xs</div>

匹配视口(Matching the viewport)

¥Matching the viewport

使用 w-screen 工具使元素跨越视口的整个宽度:

¥Use the w-screen utility to make an element span the entire width of the viewport:

<div class="w-screen">
<!-- ... -->
</div>

或者,你可以使用 w-lvww-svww-dvw 工具匹配大、小或动态视口的宽度。

¥Alternatively, you can match the width of the large, small or dynamic viewports using the w-lvw, w-svw, and w-dvw utilities.

重置宽度(Resetting the width)

¥Resetting the width

使用 w-auto 工具在特定条件下(例如在特定断点处)删除元素的指定宽度:

¥Use the w-auto utility to remove an element's assigned width under a specific condition, like at a particular breakpoint:

<div class="w-full md:w-auto">
<!-- ... -->
</div>

设置宽度和高度(Setting both width and height)

¥Setting both width and height

使用 size-pxsize-4size-full 等工具同时设置元素的宽度和高度:

¥Use utilities like size-px, size-4, and size-full to set both the width and height of an element at the same time:

size-16
size-20
size-24
<div class="size-16 ...">size-16</div>
<div class="size-20 ...">size-20</div>
<div class="size-24 ...">size-24</div>
<div class="size-32 ...">size-32</div>
<div class="size-40 ...">size-40</div>

使用自定义值(Using a custom value)

¥Using a custom value

使用 w-[<value>] 语法 根据完全自定义的值设置 width

<div class="w-[5px] ...">
<!-- ... -->
</div>

对于 CSS 变量,还可以使用 w-(<custom-property>) 语法:

<div class="w-(--my-width) ...">
<!-- ... -->
</div>

这只是简写,用于 w-[var(<custom-property>)] 它会自动为你添加 var() 函数。

响应式设计(Responsive design)

¥Responsive design

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

<div class="w-1/2 md:w-full ...">
<!-- ... -->
</div>

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

自定义主题(Customizing your theme)

¥Customizing your theme

w-<number> and size-<number> 工具由 --spacing 主题变量驱动,可以在你自己的主题中进行自定义:

@theme {
--spacing: 1px;
}

主题变量文档中了解有关自定义间距比例的更多信息。

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