1. 尺寸
  2. 宽度

尺寸

宽度

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

ClassStyles
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) */

示例

基本示例

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

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>

使用百分比

使用 w-fullw-<fraction> 实用工具,如 w-1/2w-2/5 给元素设置基于百分比的宽度:

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>

使用容器比例

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

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>

匹配视口

使用 w-screen 实用工具使元素延伸到视口的整个宽度:

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

另外,您可以使用 w-lvww-svww-dvw 实用工具来匹配大、小或动态视口的宽度。

重置宽度

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

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

同时设置宽度和高度

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

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>

使用自定义值

Use the w-[<value>] syntax to set the width based on a completely custom value:

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

For CSS variables, you can also use the w-(<custom-property>) syntax:

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

This is just a shorthand for w-[var(<custom-property>)] that adds the var() function for you automatically.

响应式设计

Prefix a width utility with a breakpoint variant like md: to only apply the utility at medium screen sizes and above:

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

Learn more about using variants in the variants documentation.

定制您的主题

The w-<number> and size-<number> utilities are driven by the --spacing theme variable, which can be customized in your own theme:

@theme {
--spacing: 1px;
}

Learn more about customizing the spacing scale in the theme variable documentation.