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

Height (高度)

設定元素高度的功能

Default class reference

Class
屬性
h-0height: 0px;
h-pxheight: 1px;
h-0.5height: 0.125rem;
h-1height: 0.25rem;
h-1.5height: 0.375rem;
h-2height: 0.5rem;
h-2.5height: 0.625rem;
h-3height: 0.75rem;
h-3.5height: 0.875rem;
h-4height: 1rem;
h-5height: 1.25rem;
h-6height: 1.5rem;
h-7height: 1.75rem;
h-8height: 2rem;
h-9height: 2.25rem;
h-10height: 2.5rem;
h-11height: 2.75rem;
h-12height: 3rem;
h-14height: 3.5rem;
h-16height: 4rem;
h-20height: 5rem;
h-24height: 6rem;
h-28height: 7rem;
h-32height: 8rem;
h-36height: 9rem;
h-40height: 10rem;
h-44height: 11rem;
h-48height: 12rem;
h-52height: 13rem;
h-56height: 14rem;
h-60height: 15rem;
h-64height: 16rem;
h-72height: 18rem;
h-80height: 20rem;
h-96height: 24rem;
h-autoheight: auto;
h-1/2height: 50%;
h-1/3height: 33.333333%;
h-2/3height: 66.666667%;
h-1/4height: 25%;
h-2/4height: 50%;
h-3/4height: 75%;
h-1/5height: 20%;
h-2/5height: 40%;
h-3/5height: 60%;
h-4/5height: 80%;
h-1/6height: 16.666667%;
h-2/6height: 33.333333%;
h-3/6height: 50%;
h-4/6height: 66.666667%;
h-5/6height: 83.333333%;
h-fullheight: 100%;
h-screenheight: 100vh;

自動設定高度

h-auto 讓瀏覽器決定元素的高度。

h-auto
<div class="h-auto ...">h-auto</div>

設定為螢幕高度

使用 h-screen 讓元素佔據整個可視高度。

h-screen
<div class="h-screen p-6 ...">h-screen</div>

固定高度

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

h-8

h-12

h-16

h-24

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

設定為父元素高度

Use h-full to set an element’s height to 100% of its parent, as long as the parent has a defined height. 若父元素有定義高度,則子元素可以使用 h-full 將高度設定為父元素高度的100%。

h-full
<div class="h-48">
  <div class="h-full ...">h-full</div>
</div>

響應式

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

<div class="h-8 md:h-full"></div>

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


客製化

Height Scale

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

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

  // tailwind.config.js
  module.exports = {
    theme: {
      spacing: {
+       sm: '8px',
+       md: '16px',
+       lg: '24px',
+       xl: '48px',
      }
    }
  }

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

  // tailwind.config.js
  module.exports = {
    theme: {
      height: {
+       sm: '8px',
+       md: '16px',
+       lg: '24px',
+       xl: '48px',
      }
    }
  }

變化模式

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

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

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

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

停用

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

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