快速參考

類別
屬性
space-x-0 > * + *margin-left: 0px;
space-y-0 > * + *margin-top: 0px;
space-x-0.5 > * + *margin-left: 0.125rem; /* 2px */
space-y-0.5 > * + *margin-top: 0.125rem; /* 2px */
space-x-1 > * + *margin-left: 0.25rem; /* 4px */
space-y-1 > * + *margin-top: 0.25rem; /* 4px */
space-x-1.5 > * + *margin-left: 0.375rem; /* 6px */
space-y-1.5 > * + *margin-top: 0.375rem; /* 6px */
space-x-2 > * + *margin-left: 0.5rem; /* 8px */
space-y-2 > * + *margin-top: 0.5rem; /* 8px */
space-x-2.5 > * + *margin-left: 0.625rem; /* 10px */
space-y-2.5 > * + *margin-top: 0.625rem; /* 10px */
space-x-3 > * + *margin-left: 0.75rem; /* 12px */
space-y-3 > * + *margin-top: 0.75rem; /* 12px */
space-x-3.5 > * + *margin-left: 0.875rem; /* 14px */
space-y-3.5 > * + *margin-top: 0.875rem; /* 14px */
space-x-4 > * + *margin-left: 1rem; /* 16px */
space-y-4 > * + *margin-top: 1rem; /* 16px */
space-x-5 > * + *margin-left: 1.25rem; /* 20px */
space-y-5 > * + *margin-top: 1.25rem; /* 20px */
space-x-6 > * + *margin-left: 1.5rem; /* 24px */
space-y-6 > * + *margin-top: 1.5rem; /* 24px */
space-x-7 > * + *margin-left: 1.75rem; /* 28px */
space-y-7 > * + *margin-top: 1.75rem; /* 28px */
space-x-8 > * + *margin-left: 2rem; /* 32px */
space-y-8 > * + *margin-top: 2rem; /* 32px */
space-x-9 > * + *margin-left: 2.25rem; /* 36px */
space-y-9 > * + *margin-top: 2.25rem; /* 36px */
space-x-10 > * + *margin-left: 2.5rem; /* 40px */
space-y-10 > * + *margin-top: 2.5rem; /* 40px */
space-x-11 > * + *margin-left: 2.75rem; /* 44px */
space-y-11 > * + *margin-top: 2.75rem; /* 44px */
space-x-12 > * + *margin-left: 3rem; /* 48px */
space-y-12 > * + *margin-top: 3rem; /* 48px */
space-x-14 > * + *margin-left: 3.5rem; /* 56px */
space-y-14 > * + *margin-top: 3.5rem; /* 56px */
space-x-16 > * + *margin-left: 4rem; /* 64px */
space-y-16 > * + *margin-top: 4rem; /* 64px */
space-x-20 > * + *margin-left: 5rem; /* 80px */
space-y-20 > * + *margin-top: 5rem; /* 80px */
space-x-24 > * + *margin-left: 6rem; /* 96px */
space-y-24 > * + *margin-top: 6rem; /* 96px */
space-x-28 > * + *margin-left: 7rem; /* 112px */
space-y-28 > * + *margin-top: 7rem; /* 112px */
space-x-32 > * + *margin-left: 8rem; /* 128px */
space-y-32 > * + *margin-top: 8rem; /* 128px */
space-x-36 > * + *margin-left: 9rem; /* 144px */
space-y-36 > * + *margin-top: 9rem; /* 144px */
space-x-40 > * + *margin-left: 10rem; /* 160px */
space-y-40 > * + *margin-top: 10rem; /* 160px */
space-x-44 > * + *margin-left: 11rem; /* 176px */
space-y-44 > * + *margin-top: 11rem; /* 176px */
space-x-48 > * + *margin-left: 12rem; /* 192px */
space-y-48 > * + *margin-top: 12rem; /* 192px */
space-x-52 > * + *margin-left: 13rem; /* 208px */
space-y-52 > * + *margin-top: 13rem; /* 208px */
space-x-56 > * + *margin-left: 14rem; /* 224px */
space-y-56 > * + *margin-top: 14rem; /* 224px */
space-x-60 > * + *margin-left: 15rem; /* 240px */
space-y-60 > * + *margin-top: 15rem; /* 240px */
space-x-64 > * + *margin-left: 16rem; /* 256px */
space-y-64 > * + *margin-top: 16rem; /* 256px */
space-x-72 > * + *margin-left: 18rem; /* 288px */
space-y-72 > * + *margin-top: 18rem; /* 288px */
space-x-80 > * + *margin-left: 20rem; /* 320px */
space-y-80 > * + *margin-top: 20rem; /* 320px */
space-x-96 > * + *margin-left: 24rem; /* 384px */
space-y-96 > * + *margin-top: 24rem; /* 384px */
space-x-px > * + *margin-left: 1px;
space-y-px > * + *margin-top: 1px;
space-y-reverse > * + *--tw-space-y-reverse: 1;
space-x-reverse > * + *--tw-space-x-reverse: 1;

基本用法

在子元素之間加入水平間距

使用 space-x-{amount} 工具程式控制元素之間的水平間距。

01
02
03
<div class="flex space-x-4 ...">
  <div>01</div>
  <div>02</div>
  <div>03</div>
</div>

在子元素之間加入垂直間距

使用 space-y-{amount} 輔助工具控制元素之間的垂直空間。

01
02
03
<div class="flex flex-col space-y-4 ...">
  <div>01</div>
  <div>02</div>
  <div>03</div>
</div>

反轉子項目順序

如果你的元素順序是反的(例如使用 flex-row-reverseflex-col-reverse),請使用 space-x-reversespace-y-reverse 輔助工具,以確保空間新增到每個元素的正確側邊。

01
02
03
<div class="flex flex-row-reverse space-x-4 space-x-reverse ...">
  <div>01</div>
  <div>02</div>
  <div>03</div>
</div>

使用負值

若要使用負空間值,請在類別名稱前加上連字號,將其轉換為負值。

<div class="flex -space-x-4 ...">
  <!-- ... -->
</div>

限制

這些輔助工具實際上只是新增邊距到群組中所有項目(第一個項目除外)的捷徑,並不設計用於處理複雜的情況,例如網格、會換行的版面,或子項目以複雜的自訂順序呈現,而非其自然 DOM 順序的情況。

對於這些情況,如果可行,最好使用 間距工具程式,或在每個元素中加入外框距,並在父元素中加入相應的負外框距

<div class="flow-root">
  <div class="-m-2 flex flex-wrap">
    <div class="m-2 ..."></div>
    <div class="m-2 ..."></div>
    <div class="m-2 ..."></div>
  </div>
</div>

無法與區隔工具程式配對

space-* 工具程式並非設計為與 區隔工具程式 搭配使用。對於這些情況,請考慮改為在子元素中加入外框距/內距工具程式。


有條件地套用

游標移入、焦點及其他狀態

Tailwind 讓你可以使用變異修飾詞,在不同的狀態中,有條件地套用工具程式類別。例如,使用 hover:space-x-8 來僅在游標移入時套用 space-x-8 工具程式。

<div class="flex space-x-2 hover:space-x-8">
  <!-- ... -->
</div>

如需所有可用狀態修飾詞的完整清單,請查看 游標移入、焦點及其他狀態 文件。

斷點和媒體查詢

您也可以使用變異修改器來針對媒體查詢,例如回應式斷點、暗色模式、偏好減少動作等。例如,使用 md:space-x-8 僅在中型螢幕大小及以上套用 space-x-8 實用程式。

<div class="flex space-x-2 md:space-x-8">
  <!-- ... -->
</div>

若要深入了解,請查看關於 回應式設計 暗色模式 其他媒體查詢修改器 的文件。


使用自訂值

自訂您的主題

預設情況下,Tailwind 的間距比例使用 預設間距比例。您可以透過編輯 tailwind.config.js 檔案中的 theme.spacingtheme.extend.spacing 來自訂間距比例。

tailwind.config.js
module.exports = {
  theme: {
    extend: {
      spacing: {
        '5px': '5px',
      }
    }
  }
}

或者,您也可以透過編輯 tailwind.config.js 檔案中的 theme.spacetheme.extend.space 來自訂間距比例。

tailwind.config.js
module.exports = {
  theme: {
    extend: {
      space: {
        '5px': '5px',
      }
    }
  }
}

主題自訂 文件中,深入了解如何自訂預設主題。

任意值

如果您需要使用一次性的 space-{x|y} 值,而這在您的主題中沒有意義,請使用方括號使用任何任意值即時產生屬性。

<div class="space-y-[5px]">
  <!-- ... -->
</div>

任意值 文件中,深入了解任意值支援。