Tailwind CSS 寬度

2022-08-08 10:04 更新

寬度

用來設(shè)置元素寬度的功能類

Class
Properties
w-0 width: 0px;
w-px width: 1px;
w-0.5 width: 0.125rem;
w-1 width: 0.25rem;
w-1.5 width: 0.375rem;
w-2 width: 0.5rem;
w-2.5 width: 0.625rem;
w-3 width: 0.75rem;
w-3.5 width: 0.875rem;
w-4 width: 1rem;
w-5 width: 1.25rem;
w-6 width: 1.5rem;
w-7 width: 1.75rem;
w-8 width: 2rem;
w-9 width: 2.25rem;
w-10 width: 2.5rem;
w-11 width: 2.75rem;
w-12 width: 3rem;
w-14 width: 3.5rem;
w-16 width: 4rem;
w-20 width: 5rem;
w-24 width: 6rem;
w-28 width: 7rem;
w-32 width: 8rem;
w-36 width: 9rem;
w-40 width: 10rem;
w-44 width: 11rem;
w-48 width: 12rem;
w-52 width: 13rem;
w-56 width: 14rem;
w-60 width: 15rem;
w-64 width: 16rem;
w-72 width: 18rem;
w-80 width: 20rem;
w-96 width: 24rem;
w-auto width: auto;
w-1/2 width: 50%;
w-1/3 width: 33.333333%;
w-2/3 width: 66.666667%;
w-1/4 width: 25%;
w-2/4 width: 50%;
w-3/4 width: 75%;
w-1/5 width: 20%;
w-2/5 width: 40%;
w-3/5 width: 60%;
w-4/5 width: 80%;
w-1/6 width: 16.666667%;
w-2/6 width: 33.333333%;
w-3/6 width: 50%;
w-4/6 width: 66.666667%;
w-5/6 width: 83.333333%;
w-1/12 width: 8.333333%;
w-2/12 width: 16.666667%;
w-3/12 width: 25%;
w-4/12 width: 33.333333%;
w-5/12 width: 41.666667%;
w-6/12 width: 50%;
w-7/12 width: 58.333333%;
w-8/12 width: 66.666667%;
w-9/12 width: 75%;
w-10/12 width: 83.333333%;
w-11/12 width: 91.666667%;
w-full width: 100%;
w-screen width: 100vw;
w-min width: min-content;
w-max width: max-content;

Auto

使用 ?w-auto? 讓瀏覽器計(jì)算并選擇元素的寬度。您可以用它來取消設(shè)置的一個(gè)特定的寬度。

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

Screen Width

使用 ?w-screen? 使一個(gè)元素跨越整個(gè)視口的寬度。

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

Fixed Width

使用 ?w-{number}? 或 ?w-px? 將元素設(shè)置為固定寬度。


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

Fluid Width

使用 ?w-{fraction}? 或 ?w-full? 將元素設(shè)置為基于百分比的寬度。


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

響應(yīng)式

要在特定的斷點(diǎn)處控制元素的寬度,可以在任何現(xiàn)有的寬度功能類前添加 ?{screen}:? 前綴。例如,將 ?md:w-full? 類添加到一個(gè)元素中,就可以在中等及以上尺寸屏幕的情況下應(yīng)用用 ?w-full? 功能類。

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

關(guān)于 Tailwind 的響應(yīng)式設(shè)計(jì)功能的更多信息,請(qǐng)查看 響應(yīng)式設(shè)計(jì) 文檔。

自定義

Width scale

默認(rèn)情況下,Tailwind 的寬度比例是 默認(rèn)間距比例 以及一些特定于寬度的附加值的組合。

您可以在您的 ?tailwind.config.js? 文件中的 ?theme.spacing? 或 ?theme.extend.spacing? 部分一次性自定義 padding、margin、width 和 height 的間距比例。

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

要單獨(dú)定制寬度,請(qǐng)使用您的 ?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%',
       }
      }
    }
  }

在 主題自定義文檔 中了解更多關(guān)于自定義默認(rèn)主題的信息。

變體

默認(rèn)情況下, 針對(duì) width 功能類,只生成 responsive 變體。

您可以通過修改您的 ?tailwind.config.js? 文件中的 ?variants ?部分中的 ?width ?屬性來控制為 width 功能生成哪些變體。

例如,這個(gè)配置也將生成 hover and focus 變體:

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

禁用

如果您不打算在您的項(xiàng)目中使用 width 功能,您可以通過在配置文件的 ?corePlugins ?部分將 ?width ?屬性設(shè)置為 ?false ?來完全禁用它們:

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


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)