返回介绍

Width

发布于 2024-07-30 22:16:07 字数 34108 浏览 0 评论 0 收藏 0

Utilities for setting the width of an element.

Quick reference

ClassProperties
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

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

w-96 w-80 w-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>

Percentage widths

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

Viewport width

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

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

Resetting the width

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>

Applying conditionally

Hover, focus, and other states

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.

Breakpoints and media queries

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

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

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',
      }
    }
  }
}

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文