クイックリファレンス

クラス
プロパティ
space-x-0 > * + *左マージン: 0px;
space-y-0 > * + *上マージン: 0px;
space-x-0.5 > * + *左マージン: 0.125rem; /* 2px */
space-y-0.5 > * + *上マージン: 0.125rem; /* 2px */
space-x-1 > * + *左マージン: 0.25rem; /* 4px */
space-y-1 > * + *上マージン: 0.25rem; /* 4px */
space-x-1.5 > * + *左マージン: 0.375rem; /* 6px */
space-y-1.5 > * + *上マージン: 0.375rem; /* 6px */
space-x-2 > * + *左マージン: 0.5rem; /* 8px */
space-y-2 > * + *上マージン: 0.5rem; /* 8px */
space-x-2.5 > * + *左マージン: 0.625rem; /* 10px */
space-y-2.5 > * + *上マージン: 0.625rem; /* 10px */
space-x-3 > * + *左マージン: 0.75rem; /* 12px */
space-y-3 > * + *上マージン: 0.75rem; /* 12px */
space-x-3.5 > * + *左マージン: 0.875rem; /* 14px */
space-y-3.5 > * + *上マージン: 0.875rem; /* 14px */
space-x-4 > * + *左マージン: 1rem; /* 16px */
space-y-4 > * + *上マージン: 1rem; /* 16px */
space-x-5 > * + *左マージン: 1.25rem; /* 20px */
space-y-5 > * + *上マージン: 1.25rem; /* 20px */
space-x-6 > * + *左マージン: 1.5rem; /* 24px */
space-y-6 > * + *上マージン: 1.5rem; /* 24px */
space-x-7 > * + *左マージン: 1.75rem; /* 28px */
space-y-7 > * + *上マージン: 1.75rem; /* 28px */
space-x-8 > * + *左マージン: 2rem; /* 32px */
space-y-8 > * + *上マージン: 2rem; /* 32px */
space-x-9 > * + *左マージン: 2.25rem; /* 36px */
space-y-9 > * + *上マージン: 2.25rem; /* 36px */
space-x-10 > * + *左マージン: 2.5rem; /* 40px */
space-y-10 > * + *上マージン: 2.5rem; /* 40px */
space-x-11 > * + *左マージン: 2.75rem; /* 44px */
space-y-11 > * + *上マージン: 2.75rem; /* 44px */
space-x-12 > * + *左マージン: 3rem; /* 48px */
space-y-12 > * + *上マージン: 3rem; /* 48px */
space-x-14 > * + *左マージン: 3.5rem; /* 56px */
space-y-14 > * + *上マージン: 3.5rem; /* 56px */
space-x-16 > * + *左マージン: 4rem; /* 64px */
space-y-16 > * + *上マージン: 4rem; /* 64px */
space-x-20 > * + *左マージン: 5rem; /* 80px */
space-y-20 > * + *上マージン: 5rem; /* 80px */
space-x-24 > * + *左マージン: 6rem; /* 96px */
space-y-24 > * + *上マージン: 6rem; /* 96px */
space-x-28 > * + *左マージン: 7rem; /* 112px */
space-y-28 > * + *上マージン: 7rem; /* 112px */
space-x-32 > * + *左マージン: 8rem; /* 128px */
space-y-32 > * + *上マージン: 8rem; /* 128px */
space-x-36 > * + *左マージン: 9rem; /* 144px */
space-y-36 > * + *上マージン: 9rem; /* 144px */
space-x-40 > * + *左マージン: 10rem; /* 160px */
space-y-40 > * + *上マージン: 10rem; /* 160px */
space-x-44 > * + *左マージン: 11rem; /* 176px */
space-y-44 > * + *上マージン: 11rem; /* 176px */
space-x-48 > * + *左マージン: 12rem; /* 192px */
space-y-48 > * + *上マージン: 12rem; /* 192px */
space-x-52 > * + *左マージン: 13rem; /* 208px */
space-y-52 > * + *上マージン: 13rem; /* 208px */
space-x-56 > * + *左マージン: 14rem; /* 224px */
space-y-56 > * + *上マージン: 14rem; /* 224px */
space-x-60 > * + *左マージン: 15rem; /* 240px */
space-y-60 > * + *上マージン: 15rem; /* 240px */
space-x-64 > * + *左マージン: 16rem; /* 256px */
space-y-64 > * + *上マージン: 16rem; /* 256px */
space-x-72 > * + *左マージン: 18rem; /* 288px */
space-y-72 > * + *上マージン: 18rem; /* 288px */
space-x-80 > * + *左マージン: 20rem; /* 320px */
space-y-80 > * + *上マージン: 20rem; /* 320px */
space-x-96 > * + *左マージン: 24rem; /* 384px */
space-y-96 > * + *上マージン: 24rem; /* 384px */
space-x-px > * + *左マージン: 1px;
space-y-px > * + *上マージン: 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-reverse または flex-col-reverse を使用している場合)、space-x-reverse または space-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順序ではなく複雑なカスタム順序でレンダリングされる状況など、複雑なケースを処理するように設計されていません。

そのような状況では、可能な限りgapユーティリティを使用するか、親要素に一致する負のマージンを持つすべての要素にマージンを追加することをお勧めします。

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

divideユーティリティとの併用不可

space-*ユーティリティは、divideユーティリティと連携して動作するように設計されていません。そのような状況では、代わりに子要素にマージン/パディングユーティリティを追加することを検討してください。


条件付き適用

ホバー、フォーカス、その他の状態

Tailwindを使用すると、バリアント修飾子を使用して、さまざまな状態でのユーティリティクラスを条件付きで適用できます。例えば、使用します hover:space-x-8 で、space-x-8ユーティリティを適用します。hover.

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

使用可能なすべての状態修飾子の完全なリストについては、 ホバー、フォーカス、その他の状態ドキュメントを参照してください。

ブレークポイントとメディアクエリ

レスポンシブなブレークポイント、ダークモード、prefers-reduced-motionなど、メディアクエリをターゲットとするバリアント修飾子も使用できます。たとえば、md:space-x-8を使用すると、中程度の画面サイズ以上でspace-x-8ユーティリティが適用されます。

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

詳細については、 レスポンシブデザイン, ダークモード その他のメディアクエリ修飾子に関するドキュメントを参照してください。.


カスタム値の使用

テーマのカスタマイズ

デフォルトでは、Tailwindのスペーススケールはデフォルトのスペーススケールを使用します。tailwind.config.jsファイルのtheme.spacingまたはtheme.extend.spacingを編集して、スペーススケールをカスタマイズできます。

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

あるいは、tailwind.config.jsファイルのtheme.spaceまたはtheme.extend.spaceを編集して、スペーススケールだけをカスタマイズすることもできます。

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

デフォルトテーマのカスタマイズの詳細については、テーマのカスタマイズドキュメントを参照してください。

任意の値

テーマに含めるのが適切ではない、一度限りのspace-{x|y}値を使用する必要がある場合は、角括弧を使用して、任意の値を使用してプロパティを動的に生成します。

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

任意の値のサポートの詳細については、 任意の値 ドキュメントを参照してください。