1. 尺寸
  2. 宽度

Quick reference

Class
Properties
w-0width: 0px;
w-pxwidth: 1px;
w-0.5width: 0.125rem; /* 2px */
w-1width: 0.25rem; /* 4px */
w-1.5width: 0.375rem; /* 6px */
w-2width: 0.5rem; /* 8px */
w-2.5width: 0.625rem; /* 10px */
w-3width: 0.75rem; /* 12px */
w-3.5width: 0.875rem; /* 14px */
w-4width: 1rem; /* 16px */
w-5width: 1.25rem; /* 20px */
w-6width: 1.5rem; /* 24px */
w-7width: 1.75rem; /* 28px */
w-8width: 2rem; /* 32px */
w-9width: 2.25rem; /* 36px */
w-10width: 2.5rem; /* 40px */
w-11width: 2.75rem; /* 44px */
w-12width: 3rem; /* 48px */
w-14width: 3.5rem; /* 56px */
w-16width: 4rem; /* 64px */
w-20width: 5rem; /* 80px */
w-24width: 6rem; /* 96px */
w-28width: 7rem; /* 112px */
w-32width: 8rem; /* 128px */
w-36width: 9rem; /* 144px */
w-40width: 10rem; /* 160px */
w-44width: 11rem; /* 176px */
w-48width: 12rem; /* 192px */
w-52width: 13rem; /* 208px */
w-56width: 14rem; /* 224px */
w-60width: 15rem; /* 240px */
w-64width: 16rem; /* 256px */
w-72width: 18rem; /* 288px */
w-80width: 20rem; /* 320px */
w-96width: 24rem; /* 384px */
w-autowidth: auto;
w-1/2width: 50%;
w-1/3width: 33.333333%;
w-2/3width: 66.666667%;
w-1/4width: 25%;
w-2/4width: 50%;
w-3/4width: 75%;
w-1/5width: 20%;
w-2/5width: 40%;
w-3/5width: 60%;
w-4/5width: 80%;
w-1/6width: 16.666667%;
w-2/6width: 33.333333%;
w-3/6width: 50%;
w-4/6width: 66.666667%;
w-5/6width: 83.333333%;
w-1/12width: 8.333333%;
w-2/12width: 16.666667%;
w-3/12width: 25%;
w-4/12width: 33.333333%;
w-5/12width: 41.666667%;
w-6/12width: 50%;
w-7/12width: 58.333333%;
w-8/12width: 66.666667%;
w-9/12width: 75%;
w-10/12width: 83.333333%;
w-11/12width: 91.666667%;
w-fullwidth: 100%;
w-screenwidth: 100vw;
w-svwwidth: 100svw;
w-lvwwidth: 100lvw;
w-dvwwidth: 100dvw;
w-minwidth: min-content;
w-maxwidth: max-content;
w-fitwidth: fit-content;

基本用法

¥Basic usage

固定宽度

¥Fixed widths

使用 w-pxw-1w-64 等工具将元素设置为固定宽度。

¥Use utilities like w-px, w-1, and w-64 to set an element to a fixed width.

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>

百分比宽度

¥Percentage widths

使用 w-fullw-1/2w-2/5 等工具将元素设置为基于百分比的宽度。

¥Use utilities like w-full, w-1/2, and w-2/5 to set an element to 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>

视口宽度

¥Viewport width

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

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

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

重置宽度

¥Resetting the width

如果你需要在特定条件下(例如在特定断点处)删除元素的分配宽度,则 w-auto 工具会很有用:

¥The w-auto utility can be useful if you need to remove an element’s assigned width under a specific condition, like at a particular breakpoint:

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

有条件地应用

悬停、聚焦和其他状态

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

<div class="w-1/2 hover:w-full">
  <!-- ... -->
</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:w-full to apply the w-full utility at only medium screen sizes and above.

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

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


使用自定义值

¥Using custom values

自定义主题

¥Customizing your theme

默认情况下,Tailwind 的宽度比例是 默认间距比例 以及一些特定于宽度的附加值的组合。

¥By default, Tailwind’s width scale is a combination of the default spacing scale as well as some additional values specific to widths.

你可以通过编辑 tailwind.config.js 文件中的 theme.spacingtheme.extend.spacing 来自定义间距比例。

¥You can customize your spacing scale by editing theme.spacing or theme.extend.spacing in your tailwind.config.js file.

tailwind.config.js
module.exports = {
  theme: {
    extend: {
      spacing: {
        '128': '32rem',
      }
    }
  }
}

要单独自定义宽度,请使用 tailwind.config.js 文件的 theme.width 部分。

¥To customize width separately, use the theme.width section of your tailwind.config.js file.

tailwind.config.js
module.exports = {
  theme: {
    extend: {
      width: {
        '128': '32rem',
      }
    }
  }
}

主题定制 文档中了解有关自定义默认主题的更多信息。

¥Learn more about customizing the default theme in the theme customization documentation.

任意值

¥Arbitrary values

If you need to use a one-off width value that doesn’t make sense to include in your theme, use square brackets to generate a property on the fly using any arbitrary value.

<div class="w-[32rem]">
  <!-- ... -->
</div>

Learn more about arbitrary value support in the arbitrary values documentation.