TailWind CSS カスタムプロパティ一覧 備忘録

TailWind CSS カスタムプロパティ一覧 備忘録

  • 06 Nov, 2024

TailWind CSS勉強中にダークモード実装のためにカスタムプロパティというものを学びました。
カスタムプロパティは以下のような形で使用できます。

global.css

@layer base {
  :root {
    /* ライトモードの色定義 */
    --background: 255 255 255; /* 白 */
    --text: 0 0 0;             /* 黒 */
  }

  .dark {
    /* ダークモードの色定義 */
    --background: 0 0 0;       /* 黒 */
    --text: 255 255 255;       /* 白 */
  }
}

tailwind.config.js

// tailwind.config.js
module.exports = {
  darkMode: 'class',
  theme: {
    extend: {
      backgroundColor: {
        hoge: 'rgb(var(--background))', 
      },
      textColor: {
        fuga: 'rgb(var(--text))', 
      },
    },
  },
};

html

<html class="dark">
  <div class="bg-hoge">
    <p class="text-fuga">Text</p>
  </div>
</html>

ここで、backgroundColorのなかのhogebg-hogetextColorのなかのfugatext-fugaになるといったように、決められているようです。

以下によく使うであろうリストを記します。

主なカスタマイズ可能なプロパティと接頭辞

  • backgroundColor: bg-

    • 背景色のカスタマイズ。
    • 例: bg-primarybg-accent
  • textColor: text-

    • テキスト色のカスタマイズ。
    • 例: text-primarytext-muted
  • borderColor: border-

    • ボーダー(境界線)色のカスタマイズ。
    • 例: border-primaryborder-accent
  • spacing: p-(padding)、m-(margin)

    • マージンとパディングのサイズカスタマイズ。
    • 例: p-7(パディング7ユニット)、m-8(マージン8ユニット)
  • borderRadius: rounded-

    • 角の丸み(ボーダー半径)をカスタマイズ。
    • 例: rounded-lgrounded-custom
  • width / height: w- / h-

    • 幅と高さのカスタマイズ。
    • 例: w-72h-80
  • fontSize: text-

    • フォントサイズのカスタマイズ。
    • 例: text-xltext-2xltext-custom
  • boxShadow: shadow-

    • ボックスシャドウのカスタマイズ。
    • 例: shadow-mdshadow-custom
  • opacity: opacity-

    • 透明度のカスタマイズ。
    • 例: opacity-75opacity-90
  • zIndex: z-

    • Z軸のスタッキング順序(重なり順)のカスタマイズ。
    • 例: z-10z-50

随時追加予定です。