你正在瀏覽的是 Tailwind CSS v2. 的技術文件
Tailwind CSS 的 GitHub

Width (寬度)

設定元素寬度的功能

Default class reference

Class
屬性
w-0width: 0px;
w-pxwidth: 1px;
w-0.5width: 0.125rem;
w-1width: 0.25rem;
w-1.5width: 0.375rem;
w-2width: 0.5rem;
w-2.5width: 0.625rem;
w-3width: 0.75rem;
w-3.5width: 0.875rem;
w-4width: 1rem;
w-5width: 1.25rem;
w-6width: 1.5rem;
w-7width: 1.75rem;
w-8width: 2rem;
w-9width: 2.25rem;
w-10width: 2.5rem;
w-11width: 2.75rem;
w-12width: 3rem;
w-14width: 3.5rem;
w-16width: 4rem;
w-20width: 5rem;
w-24width: 6rem;
w-28width: 7rem;
w-32width: 8rem;
w-36width: 9rem;
w-40width: 10rem;
w-44width: 11rem;
w-48width: 12rem;
w-52width: 13rem;
w-56width: 14rem;
w-60width: 15rem;
w-64width: 16rem;
w-72width: 18rem;
w-80width: 20rem;
w-96width: 24rem;
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-minwidth: min-content;
w-maxwidth: max-content;

自動設定寬度

使用 w-auto 讓瀏覽器自動為元素計算並選擇寬度。你可以用他還原已設定的寬度。

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

設定為螢幕寬度

使用 w-screen 讓元素佔據整個可視寬度。

<div class="h-12 w-screen"></div>

固定寬度

使用 w-{number}w-px 來設定元素的固定寬度。

w-8

w-12

w-16

w-24

<div>
  <div class="w-8 ..."></div>
  <div class="w-12 ..."></div>
  <div class="w-16 ..."></div>
  <div class="w-24 ..."></div>
</div>

浮動寬度

使用 w-{fraction}w-full 設定元素寬度比例。

w-1/2
w-1/2
w-2/5
w-3/5
w-1/3
w-2/3
w-1/4
w-3/4
w-1/5
w-4/5
w-1/6
w-5/6
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>

響應式

要在特定斷點控制元素寬度,可在寬度功能前加上前綴詞 {screen}:。例如:中等以上尺寸螢幕使用 md:w-full 以套用 w-full 功能。

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

更多有關 Tailwind 響應式設計功能的資訊,可參考 響應式設計 文件。

客製化

Width scale

Tailwind 的寬度預設是由 預設間距 及額外寬度設定組合而成。

tailwind.config.js 裡頭, theme.spacingtheme.extend.spacing 可同時設定間距裡的 padding, margin, 寬度和高度。

  // tailwind.config.js
  module.exports = {
    theme: {
      extend: {
+       spacing: {
+         '72': '18rem',
+         '84': '21rem',
+         '96': '24rem',
+       }
      }
    }
  }

tailwind.config.js 裡頭, theme.width 可另行設定寬度。

  // tailwind.config.js
  module.exports = {
    theme: {
      extend: {
+       width: {
+         '1/7': '14.2857143%',
+         '2/7': '28.5714286%',
+         '3/7': '42.8571429%',
+         '4/7': '57.1428571%',
+         '5/7': '71.4285714%',
+         '6/7': '85.7142857%',
+       }
      }
    }
  }

更多有關 theme 的資訊,可參考客製化 theme 文件

變化模式

默認情況下, 只有 響應式 的 width 變化模式 (variants) 會產生。

tailwind.config.jsvariants 區塊中變更 width 屬性來決定有哪些變化模式會生成。

舉個例子來說,這個設定將會生成 響應式 和 hover 的變化模式。

  // tailwind.config.js
  module.exports = {
    variants: {
      extend: {
        // ...
+       width: ['responsive', 'hover'],
      }
    }
  }

停用

如果你不打算在專案中使用 width 功能,可以在你的設定檔裡的 corePlugins 屬性中將 width 設定為 false 來停用功能:

  // tailwind.config.js
  module.exports = {
    corePlugins: {
      // ...
+     width: false,
    }
  }