はじめに
CDN によるインストール
CDN スクリプトは、Tailwind でプロトタイプを作る最速の方法です。マークアップを走査することで、ユーテ ィリティを実行時に生成します。本番では、PostCSS か CLI のビルドを優先し、未使用のクラスをパージし、小さな CSS バンドルを出荷しましょう。
<!-- Quick start: drop this in <head> -->
<script src="https://cdn.tailwindcss.com"></script>
<!-- Tailwind scans class names in your HTML and generates styles on the fly -->
<h1 class="text-3xl font-bold text-blue-600">Hello Tailwind</h1>プロジェクトのセットアップ(CLI)
content 配列が重要です — Tailwind は、そのファイル内で見つけたクラスだけを生成します(ツリーシェイキング)。パスの忘れが、「クラスが効かない」バグの第 1 位の原因です。開発では --watch を使いましょう。
# Install Tailwind as a project dependency
npm install -D tailwindcss
npx tailwindcss init
# tailwind.config.js — tell Tailwind where your templates live
/** @type {import('tailwindcss').Config} */
module.exports = {
content: ["./src/**/*.{html,js,jsx,ts,tsx}"],
theme: { extend: {} },
plugins: [],
};
# Build CSS
npx tailwindcss -i ./src/input.css -o ./dist/output.css --watch入力 CSS のディレクティブ
@tailwind ディレクティブは、Tailwind の 3 つのレイヤーを順に注入します:base(リセット)、components、utilities です。@layer は、自分の CSS を正しいカスケードレイヤーへ追加させ、@apply は、ユーティリティのスタイルをカスタムルールへインライン化します。
/* src/input.css — the three layers Tailwind uses */
@tailwind base; /* normalize/reset + base element styles */
@tailwind components; /* component classes you register */
@tailwind utilities; /* the utility classes themselves */
/* Add custom CSS after the directives */
@layer base {
h1 { @apply text-2xl; }
}ユーティリティファーストの考え方
Tailwind の哲学:セマンティックなクラス名を発明するのでなく、小さい単一目的のユーティリティクラスを組み合わせて UI を構築します。これにより、CSS がフラットに保たれ、デッドコードが避けられ、共有のスペーシング/カラーのスケールを通じて、デザインの一貫性が保たれます。
<!-- Instead of naming custom classes, compose utilities -->
<button class="bg-blue-600 hover:bg-blue-700 text-white
font-medium py-2 px-4 rounded-lg shadow
transition-colors duration-200">
Save
</button>
<!-- Equivalent to writing CSS: .btn { background, padding, radius... } -->
<!-- But each utility maps to a single, predictable declaration. -->設定とテーマの拡張
theme.extend を使い、Tailwind のデフォルトを置き換えずに追加しましょう。theme の(extend でない)トップレベルにキーを定義すると、そのスケール全体が置き換わります。brand-500 のようなカスタムカラーは、bg-brand-500、text-brand-500、border-brand-500 などを生成します。
// tailwind.config.js
module.exports = {
content: ["./src/**/*.{html,tsx}"],
theme: {
extend: {
colors: {
brand: { 500: "#06B6D4", 600: "#0891B2" },
},
fontFamily: {
sans: ["Inter", "system-ui", "sans-serif"],
},
spacing: { 128: "32rem" },
},
},
plugins: [],
};カラー
テキスト、背景、ボーダーの色
カラーのユーティリティは、{property}-{color}-{shade} のパターンに従います。シェードは、50(最も明るい)から 950(最も暗い)まで走ります。よくあるプロパティ:text-、bg-、border-、ring-、fill-、stroke-、from-/to-/via-(グラデーション)です。
<p class="text-red-500">Red text</p>
<div class="bg-blue-100">Light blue background</div>
<span class="border border-gray-300">Gray border</span>
<button class="bg-emerald-500 hover:bg-emerald-600">Emerald</button>不透明度の修飾子
任意のカラーユーティリティに /{opacity} を追加すると、alpha チャネルが設定されます。内部では color-mix か rgba を使うため、text、bg、border、ring などに機能します。新しいパレットのエントリを定義せずに色合いを付ける最もきれいな方法です。
<div class="bg-black/50">50% black overlay</div>
<p class="text-blue-600/75">75% opacity blue text</p>
<div class="border-gray-900/10">10% gray border</div>
<button class="bg-indigo-500 hover:bg-indigo-500/90">Slight fade</button>グラデーション
bg-gradient-to-{direction} を、from-、via-、to- のカラーストップと共に使いましょう。方向には、t(上)、b、l、r、tr、tl、br、bl が含まれます。ラジアル/コニカルのグラデーション(v3.3+)のためには、bg-gradient-radial を使います(プ ラグインか任意値が必要です)。
<div class="bg-gradient-to-r from-purple-500 to-pink-500">
Left to right gradient
</div>
<div class="bg-gradient-to-tr from-yellow-400 via-red-500 to-pink-600">
Three-stop diagonal gradient
</div>
<div class="bg-gradient-to-b from-sky-400 to-sky-900">Vertical</div>カスタムと任意のカラー
角括弧の任意値により、設定を編集せずに任意の CSS の色が使えます — 1 回だけの利用に最適です。繰り返し使う色には、theme.extend.colors で定義しましょう。そうすれば、不透明度の修飾子の対応と一貫した命名が得られます。
<!-- Arbitrary hex/rgb value -->
<div class="bg-[#1da1f2]">Twitter blue</div>
<p class="text-[rgb(255,0,0)]">Red</p>
<!-- Custom color from your theme -->
<button class="bg-brand-600">My brand color</button>
<!-- Current color / transparent -->
<div class="bg-transparent border border-current">Transparent</div>カラーパレットの概観
Tailwind のパレットは、汎用の「gray」を 4 つのニュートラルの系に置き換えます:slate(冷たい青み)、gray、zinc、neutral、stone(暖かい)です。プロジェクト全体で 1 つを選び、ニュートラルに一貫性を持ちましょう。各色には、(50、100、...、950 の)11 のシェードがあります。
<!-- Tailwind ships 22 color families, each 50-950 -->
<!-- slate gray zinc neutral stone red orange amber yellow -->
<!-- lime green emerald teal cyan sky blue indigo violet -->
<!-- purple fuchsia pink rose -->
<p class="text-slate-900">Primary text</p>
<p class="text-slate-500">Secondary text</p>
<p class="text-slate-400">Muted text</p>
<!-- Use slate/zinc/neutral/stone instead of plain gray -->
<!-- They have subtler blue/warm tones than old gray-*. -->タイポグラフィ
フォントサイズと太さ
text-{size} は、rem ベースのスケールにマッピングします(text-base = 1rem)。font-{weight} は、100〜900 をカバーします。text-[42px] のような任意値も機能します。行の高さは、leading-* で上書きしない限り、テキストサイズと共に自動拡縮します。
<p class="text-xs">12px</p>
<p class="text-sm">14px</p>
<p class="text-base">16px (default)</p>
<p class="text-lg">18px</p>
<p class="text-2xl">24px</p>
<p class="text-7xl">72px</p>
<p class="font-thin">100</p>
<p class="font-normal">400</p>
<p class="font-medium">500</p>
<p class="font-semibold">600</p>
<p class="font-bold">700</p>
<p class="font-black">900</p>行の高さと字間
leading-* は行の高さを設定し(数値 = rem、名前 = 相対)、tracking-* は字間(レタースペーシング)を設定します。スモールキャップ風のラベルとアイキャッチには、uppercase + tracking-wider + text-xs のペアを使いましょう — 非常に一般的な Tailwind のイディオムです。
<p class="leading-none">Line height 1</p>
<p class="leading-tight">1.25</p>
<p class="leading-normal">1.5 (default)</p>
<p class="leading-loose">2</p>
<p class="leading-6">1.5rem fixed</p>
<p class="tracking-tight">-0.025em</p>
<p class="tracking-normal">0</p>
<p class="tracking-wide">0.025em</p>
<p class="tracking-widest">0.1em</p>
<!-- Uppercase + tracking for labels -->
<span class="uppercase tracking-wider text-xs font-bold">Label</span>テキストの整列と装飾
text-{left,center,right,justify} は、インラインのコンテンツを整列させます。underline/line-through/no-underline は text-decoration を切り替え、decoration-{1-4} は太さを、underline-offset-{n} は位置を設定します。ナビのリンクの標準が、hover:underline のパターンです。
<p class="text-left">Left</p>
<p class="text-center">Center</p>
<p class="text-right">Right</p>
<p class="text-justify">Justified long paragraph...</p>
<a class="underline">Underlined</a>
<a class="line-through">Strikethrough</a>
<a class="no-underline hover:underline">Hover underline</a>
<a class="decoration-2 underline-offset-4">Thicker underline, offset</a>フォントファミリーと任意のフォント
デフォルトで 3 つのフォントスタックが出荷されます:sans(システム UI)、serif、mono です。theme.extend.fontFamily で(display、body などの)追加をしましょう。1 回だけの任意のフォントには、font-['Name'] を使います。実際の Web フォントを CSS/HTML で @import か <link> することを忘れないように。
<p class="font-sans">Default sans-serif stack</p>
<p class="font-serif">Serif stack</p>
<p class="font-mono">Monospace</p>
<!-- Arbitrary font family -->
<p class="font-['Georgia']">Georgia</p>
<!-- After configuring theme.extend.fontFamily -->
<p class="font-display">Custom display font</p>テキストの溢れと切り詰め
truncate = overflow-hidden text-ellipsis whitespace-nowrap です(1 行 + 省略記号)。line-clamp-{1-6} は、N 行へ切り詰めます(-webkit-line-clamp を使います)。自由な折り返しのためには、break-words か break-all で長い単語の分割を制御しましょう。
<!-- Single-line ellipsis -->
<p class="truncate w-64">
Very long text that will be cut off with an ellipsis at the end...
</p>
<!-- Clip without ellipsis -->
<p class="overflow-clip w-32">Long text clipped</p>
<!-- Multi-line clamp (2 lines) -->
<p class="line-clamp-2">
Multi-line paragraph that gets clamped after two lines
with an ellipsis, useful for card previews and previews.
</p>スペーシング
パディングとマージン
スペーシングのユーティリティは、単一の 4px ベースのスケールを使います:1 = 0.25rem = 4px です。全辺には p* / m*、軸のペアには px/py、各辺には pt/pr/pb/pl か mt/mr/mb/ml です。mx-auto は、ブロックレベルの要素を水平に中央寄せします。
<!-- p-* = padding, m-* = margin -->
<div class="p-4">16px all sides</div>
<div class="px-4 py-2">16px horizontal, 8px vertical</div>
<div class="pt-2 pr-4 pb-2 pl-4">Per side (top/right/bottom/left)</div>
<div class="m-8">32px margin all sides</div>
<div class="mx-auto">Center a block (auto L/R margin)</div>
<div class="mt-0 mb-4">No top margin, 16px bottom</div>スペーシングのスケール
1 つのスケールが、パディング、マージン、gap、width、height などを統べます — この一貫性が、Tailwind の強みです。重なりとオフセットの引き寄せには、負の値(-mt-2)を使いましょう。必要なときにスケールから脱するのが、任意値の p-[13px] ですが、稀であるべきです。
<!-- The default scale (in px) -->
0 = 0px 1 = 4px 2 = 8px 3 = 12px 4 = 16px
5 = 20px 6 = 24px 8 = 32px 10 = 40px 12 = 48px
16 = 64px 20 = 80px 24 = 96px 32 = 128px 48 = 192px
<!-- Negative margins -->
<div class="-mt-4">Pulls up by 16px</div>
<div class="-ml-2">Pulls left by 8px</div>
<!-- Arbitrary values -->
<div class="p-[13px] mt-[7px]">Custom sizes</div>子の間のスペース
space-x-* / space-y-* は、(周りでなく)兄弟の間にマージンを加えます — 縦のスタックとバッジの列に最適です。注意:space-x は margin-left を使うため、flex の justify とうまく組み合わせられません。flex には、gap-* のユーティリティを優先しましょう(Flexbox を参照)。
<div class="space-y-4">
<div>Item 1</div>
<div>Item 2</div>
<div>Item 3</div>
</div>
<!-- Adds 16px margin-top to every child except the first -->
<div class="space-x-2 flex">
<span>A</span><span>B</span><span>C</span>
</div>
<!-- Adds 8px margin-left to every child except the first -->幅と高さ
w-* / h-* は、スペーシングのスケールと、分数(1/2、1/3、2/3、1/4...)、screen/full、名前付きのサイズ(max-width の xs..7xl)を使います。max-w-{size} は幅を制限します — 読みやすい行の長さのために、ページのコンテンツを max-w-md mx-auto で包みましょう。内因的なサイジングを使うのが、w-fit / w-max です。
<div class="w-16 h-16">64x64 box</div>
<div class="w-1/2 h-full">50% width, full height</div>
<div class="w-screen h-screen">Full viewport</div>
<div class="w-96">24rem fixed</div>
<div class="max-w-md mx-auto">Capped width, centered</div>
<div class="min-h-screen">At least full viewport tall</div>
<!-- Modern: fit-content, intrinsic sizing -->
<div class="w-fit">Shrinks to content</div>Flex と Grid の gap
flex/grid の子の間のスペースのモダンな方法であるのが、gap-* です。古い space-x/space-y のマージンのアプローチを置き換えます。両軸に gap を設定するか、gap-x/gap-y を個別に設定しましょう。パディング/マージンと同じスペーシングのスケールを使います。
<div class="flex gap-4">
<div>A</div><div>B</div><div>C</div>
</div>
<!-- 16px between flex children, no margin hacks needed -->
<div class="grid grid-cols-3 gap-2 gap-y-4">
<div>1</div><div>2</div><div>3</div>
<div>4</div><div>5</div><div>6</div>
</div>
<!-- gap = both axes; gap-x / gap-y for per-axis -->Flexbox
基本の flex コンテナ
flex は display:flex を、inline-flex は display:inline-flex を設定します。flex-col は主軸を垂直に切り替え — gap と組み合わせると、縦のスタックを構築する最もきれいな方法です。デフォルトの方向は、row(水平)です。
<div class="flex">
<div>A</div><div>B</div><div>C</div>
</div>
<div class="inline-flex gap-2">
<span>Badge</span><span>Badge</span>
</div>
<!-- Vertical stack = flex-col (often cleaner than space-y) -->
<div class="flex flex-col gap-4">
<div>Row 1</div>
<div>Row 2</div>
</div>Justify と Align
justify-* は主軸を(flex-row では水平)、items-* は交差軸を(flex-row では垂直)制御します。「flex justify-center items-center」の組み合わせは、両方向にコンテンツを中央寄せします — 中央寄せで最も使われる Tailwind のスニペットです。
<div class="flex justify-start"> <!-- left --></div>
<div class="flex justify-center"> <!-- center --></div>
<div class="flex justify-between"> <!-- spread --></div>
<div class="flex justify-around"> <!-- equal space around --></div>
<div class="flex justify-end"> <!-- right --></div>
<!-- Cross axis (perpendicular to main) -->
<div class="flex items-center"> <!-- vertically center --></div>
<div class="flex items-start"> <!-- top align --></div>
<div class="flex items-end"> <!-- bottom align --></div>
<div class="flex items-stretch"> <!-- fill height --></div>Flex の grow、shrink、basis
flex-1 = flex:1 1 0% です(満たすために成長し、縮小でき、basis 0 です)。flex-none = 成長も縮小もしない(固定)です。flex-grow-0 / flex-shrink-0 は、成長/縮小を個別に無効化します。basis-{n} は、成長前の初期サイズを設定します。
<div class="flex">
<div class="flex-1">Grows to fill</div>
<div class="flex-none">Never shrinks (fixed)</div>
<div class="flex-initial">Default size, can shrink</div>
</div>
<div class="flex">
<div class="flex-grow-0 w-20">Fixed 80px</div>
<div class="flex-grow basis-0">Takes remaining</div>
</div>
<!-- Responsive: sidebar collapses on mobile -->
<div class="flex flex-col md:flex-row">
<aside class="md:w-64 md:flex-shrink-0">Sidebar</aside>
<main class="flex-1">Main</main>
</div>Flex の折り返しと順序
flex-wrap は、子が新しい行へ流れるのを可能にします(デフォルトの nowrap は溢れます)。order-{n} は、DOM を変えずに子の視覚的な順序を変え、order-first/last はショートカットです。ブレークポイントごとに異なる視覚的な順序が欲しいレスポンシブのレイアウトに有用です。
<div class="flex flex-wrap gap-4">
<div class="w-40">Card</div>
<div class="w-40">Card</div>
<div class="w-40">Card</div>
<!-- Cards wrap to next line instead of overflowing -->
</div>
<div class="flex flex-wrap-reverse">Wrap backwards</div>
<div class="flex flex-nowrap">No wrapping (default)</div>
<!-- Reorder children -->
<div class="flex">
<div class="order-last">First in DOM, last visually</div>
<div class="order-first">Last in DOM, first visually</div>
<div class="order-2">Custom order</div>
</div>中央寄せのパターン
flex items-center justify-center は、標準の中央寄せのスニペットです — 任意のコンテンツサイズに機能します。スティッキーフッターのパターン(min-h-screen + flex-col + flex-1 の main)は、コンテンツが少なくてもフッターを底部へ押し付けます。
<!-- Center anything both axes (classic) -->
<div class="flex items-center justify-center h-screen">
<div>Dead center</div>
</div>
<!-- Sticky footer pattern -->
<div class="flex flex-col min-h-screen">
<header>Header</header>
<main class="flex-1">Content grows</main>
<footer>Footer</footer>
</div>
<!-- Space between with centered item -->
<div class="flex items-center justify-between">
<button>Left</button>
<h1 class="absolute left-1/2 -translate-x-1/2">Centered title</h1>
<button>Right</button>
</div>Grid
基本の grid
grid は display:grid を設定します。grid-cols-{n} は、固定の列数を定義し(1〜12)、auto-rows-fr は全行を等しい高さにします(それぞれ 1fr)。gap-* は、flex と同様に、トラックのスペーシングに機能します。
<div class="grid grid-cols-3 gap-4">
<div>1</div><div>2</div><div>3</div>
<div>4</div><div>5</div><div>6</div>
</div>
<!-- Auto rows set the row height -->
<div class="grid grid-cols-2 auto-rows-fr gap-2">
<div>Equal-height rows</div>
<div>Equal-height rows</div>
</div>
<!-- Inline grid -->
<span class="inline-grid grid-cols-2 gap-1">...</span>レスポンシブな列
auto-fit + minmax のパターンは、強力なレスポンシブのトリックです:列は少なくとも 200px で、行を満たすよう伸び、自動的に折り返します — メディアクエリが不要です。より多くの制御のためには、明示的なレスポンシブの grid-cols-* と組み合わせましょう。
<div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 gap-4">
<!-- 1 column on mobile, 2 on small, 3 on large, 4 on xl -->
</div>
<!-- Auto-fit / minmax for card grids that wrap naturally -->
<div class="grid grid-cols-[repeat(auto-fit,minmax(200px,1fr))] gap-4">
<div>Card</div><div>Card</div><div>Card</div>
</div>列と行のスパン
col-span-{n} / row-span-{n} は、アイテムを複数のトラックにまたがらせます。col-start-{n} / col-end-{n} は、明示的なグリッドラインにアイテムを配置します。これらを組 み合わせると、メディアクエリなしで、雑誌スタイルの複雑なレイアウトが可能になります。
<div class="grid grid-cols-3 grid-rows-3 gap-2">
<div class="col-span-2">Spans 2 cols</div>
<div>1 col</div>
<div class="row-span-2">Spans 2 rows</div>
<div class="col-span-2 row-span-2">Big tile</div>
<div>1 col</div>
<div>1 col</div>
</div>
<!-- Explicit placement -->
<div class="col-start-2 col-end-4 row-start-1">
Placed at column 2-4, row 1
</div>任意値による grid-template-areas
角括弧の任意値により、任意の grid-template の構文が指定できます:grid-cols-[200px_1fr_100px] は、3 つのカスタム幅のトラックを作ります(アンダースコアがスペースになります)。名前付きの grid-template-areas は組み込みのユーティリティでないため、カスタムクラスで定義するか、col/row のスパン経由でレイアウトを使いましょう。
<div class="grid grid-cols-[200px_1fr] grid-rows-[auto_1fr_auto] gap-4
min-h-screen">
<aside class="row-span-3">Sidebar</aside>
<header>Header</header>
<main>Main</main>
<footer>Footer</footer>
</div>
<!-- Named template areas need custom CSS or plugin: -->
<style>
.app { grid-template-areas: "h h" "s m" "f f"; }
</style>gap と整列
justify-items-* / content-* は、アイテムがセル内にどう座るか、トラックがコンテナ内にどう座るかを制御します。justify-items + items-content(両軸)の省略が、place-items-center であり、グリッドブロック全体を中央寄せするのが、place-content-center です。
<div class="grid grid-cols-3 gap-4 gap-x-8 gap-y-2">
<!-- 32px col gap, 8px row gap -->
</div>
<!-- Align whole grid tracks -->
<div class="grid grid-cols-2 justify-items-center">
<div>Each item centered in its cell</div>
</div>
<div class="grid grid-cols-2 content-center h-64">
<div>Vertically centers the rows</div>
</div>
<div class="grid grid-cols-2 place-items-center">
<!-- Both axes at once -->
</div>ボーダー
ボーダーの幅と色
border は全辺に 1px を設定し、border-{2,4,8} は太さを設定します。特定の辺を狙うのが、border-{t,r,b,l,x,y} です。スタイルを設定するのが、border-dashed/dotted/double です。必ず border-{color} とペアにしましょう — Tailwind のデフォルトのボーダー色は、gray-200 です(v3 で gray-300 から変わりました)。
<div class="border">1px all sides</div>
<div class="border-2">2px</div>
<div class="border-4 border-red-500">4px red</div>
<div class="border-t border-b">Only top & bottom</div>
<div class="border-l-4 border-emerald-500">Left accent bar</div>
<div class="border-x border-gray-300">Left & right</div>
<div class="border border-dashed border-gray-400">Dashed</div>
<div class="border border-dotted">Dotted</div>
<div class="border border-double">Double</div>ボーダーの角丸
rounded-{none,sm,md,lg,xl,2xl,3xl,full} は半径を拡大し、rounded-full はピルか完全な円を作ります(幅 = 高さのとき)。角ごとには、rounded-{tl,tr,bl,br}-{size}、辺ごとには、rounded-{t,b,l,r}-{size} です。アバターの円は、rounded-full + 等しい w/h を使います。
<div class="rounded">0.25rem</div>
<div class="rounded-md">0.375rem</div>
<div class="rounded-lg">0.5rem</div>
<div class="rounded-xl">0.75rem</div>
<div class="rounded-2xl">1rem</div>
<div class="rounded-full">Fully rounded (pill/circle)</div>
<div class="rounded-t-lg rounded-b-none">Top only</div>
<div class="rounded-l-full">Left side pill</div>
<div class="rounded-tr-2xl rounded-bl-2xl">Per-corner</div>子の間の区切り線
divide-{x,y} は、兄弟の間にボーダーを加えます — 各子に border-t でスタイルするよりずっときれいです。divide-{color} と divide-{width} でカスタマイズしましょう。リストとテーブルで快適なスペーシングのために、子の py/px とペアにしましょう。
<ul class="divide-y divide-gray-200">
<li class="py-2">Item 1</li>
<li class="py-2">Item 2</li>
<li class="py-2">Item 3</li>
</ul>
<!-- Adds a 1px top border to each child except the first -->
<div class="flex divide-x divide-gray-300">
<div class="px-4">Col 1</div>
<div class="px-4">Col 2</div>
<div class="px-4">Col 3</div>
</div>Ring(アウトライン風)
リングは、box-shadow ベースのアウトラインであり、(ボーダーと違い)レイアウトに影響せず、うまく重なります。ring-{n} が幅を、ring-{color} が色を設定し、要素とリングの間に余白を加えるのが、ring-offset-{n} です。アクセシブルなフォーカススタイルの標準が、focus:ring のパターンです。
<button class="ring-2 ring-blue-500 ring-offset-2">
Ring with offset
</button>
<input class="ring-2 ring-red-500 focus:ring-2 focus:ring-blue-500" />
<button class="ring-1 ring-inset ring-gray-300">Inset ring</button>
<!-- Focus ring pattern (accessibility) -->
<button class="focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-offset-2">
Accessible focus
</button>Outline
outline-* は、本物の CSS の outline プロパティを使います(ボーダーボックスの外で、レイアウトへの影響なし)。Tailwind ではリングが好まれます — レイヤー化された box-shadow であり、複数のリングが積み重ねられ、ring-offset がきれいな隙間を与えるからです。CSS の outline の挙動が明示的に必要なときに有用であるのが、outline です。
<input class="outline-none" /> <!-- Remove default -->
<input class="outline outline-2 outline-blue-500" />
<input class="outline-dashed outline-1 outline-gray-400" />
<input class="focus:outline-none focus:ring-2 focus:ring-blue-500"
type="text" />
<!-- Outline vs ring: outline is drawn outside border-box,
ring is a box-shadow (can stack, has offset) -->背景
背景色とアタッチ
bg-{color} は background-color を設定し、alpha のために /{opacity} があります。bg-fixed はパララックス効果を作ります(コンテンツがスクロールする間、背景が留まります)。ボーダー/コンテンツの下に背景がどこまで伸びるかを制御するのが、bg-clip-{padding,border,content} です。
<div class="bg-blue-500">Solid color</div>
<div class="bg-blue-500/50">50% opacity</div>
<div class="bg-fixed">Parallax (fixed on scroll)</div>
<div class="bg-local">Scrolls with content</div>
<div class="bg-scroll">Default</div>
<!-- Clip: padding-box, border-box, content-box -->
<div class="bg-clip-padding border-4 border-dashed">Padding clip</div>
<div class="bg-clip-border">Border clip</div>背景画像とグラデーション
任意の URL で background-image を使うのが、bg-[url('/path/to/img.jpg')] です(URL を引用しましょう)。bg-cover はアスペクト比を保ちつつ要素を満たし、bg-contain は画像全体を収めます。位置を制御するのが、bg-{center,top,bottom,left,right} と bg-{repeat,no-repeat,repeat-x,repeat-y} です。
<div class="bg-gradient-to-r from-cyan-500 to-blue-500">
Horizontal gradient
</div>
<!-- Image with arbitrary value -->
<div class="bg-[url('/hero.jpg')] bg-cover bg-center h-64">
Hero with cover image
</div>
<!-- Repeat & position -->
<div class="bg-repeat-x bg-[url('/pattern.png')]">Repeat horizontally</div>
<div class="bg-no-repeat bg-top">No repeat, top aligned</div>グラデーションの方向とストップ
from-/via-/to- はグラデーションのカラーストップを設定し、bg-gradient-to-{direction} は角度を設定します。to-br のような対角の方向は、温かみのあるヒーローのグラデーションでよくあります。ネイティブのラジアル/コニカルのグラデーションには、プラグインか任意値が必要です:bg-[radial-gradient(...)] です。
<!-- Directions: to-t, to-b, to-l, to-r, to-tr, to-tl, to-br, to-bl -->
<div class="bg-gradient-to-br from-amber-400 to-orange-600">
Bottom-right diagonal
</div>
<!-- Three-stop gradient -->
<div class="bg-gradient-to-r from-green-400 via-teal-500 to-blue-600">
Multi-stop
</div>
<!-- Radial (Tailwind v3.3+) -->
<div class="bg-gradient-radial from-pink-400 to-purple-700">
Radial (may need plugin)
</div>背景のサイズと位置
ヒーロー画像とロゴそれぞれの鍵となる選択が、bg-cover 対 bg-contain です。box を満たすのが(溢れを切り取る)、cover で、画像全体を収めるのが(空きが見えるかも)、contain です。標準のレスポンシブなヒーロー画像のためには、bg-cover + bg-center を組み合わせましょう。
<div class="bg-cover bg-center h-48 bg-[url('/img.jpg')]">
Cover: fills container, may crop
</div>
<div class="bg-contain bg-center h-48 bg-no-repeat bg-[url('/logo.png')]">
Contain: fits entire image, may letterbox
</div>
<div class="bg-top-left bg-[url('/img.jpg')] bg-cover">Top-left anchor</div>
<div class="bg-[center_top]">Custom position</div>背景のブレンドモード
複数の背景レイヤー(画像 + 色)がどう合成されるかを制御するのが、bg-blend-{mode} です。multiply、screen、overlay、darken、lighten がよくあります。テキストの読みやすさのために画像を暗くするには、よりきれいなアプローチが(右の例の)上部の半透明のグラデーションのオーバーレイです。
<div class="bg-blend-multiply bg-[url('/photo.jpg')] bg-blue-500">
Multiplies image with blue overlay
</div>
<!-- Common overlay pattern: dark gradient over image -->
<div class="relative">
<img src="/hero.jpg" class="w-full" />
<div class="absolute inset-0 bg-gradient-to-t from-black/70 to-transparent">
Overlay
</div>
</div>シャドウ
Box Shadow のスケール
シャドウのスケール(sm、デフォルト、md、lg、xl、2xl)は、アプリ全体で一貫したエレベーションを与えます。ウェルと押された状態に有用な内側の影を作るのが、shadow-inner です。カードは典型的に shadow-md を使い、リフト効果のために hover:shadow-lg とします。
<div class="shadow-sm">Subtle</div>
<div class="shadow">Default</div>
<div class="shadow-md">Medium</div>
<div class="shadow-lg">Large</div>
<div class="shadow-xl">Extra large</div>
<div class="shadow-2xl">Huge</div>
<div class="shadow-none">No shadow</div>
<!-- Inner shadow -->
<div class="shadow-inner">Pressed-in look</div>カラーのシャドウ
box shadow を色で染めるのが、shadow-{color} です(同じカラーパレットを使います)。/{opacity} と組み合わせると、グローの効果が作れます。カラーのシャドウは、ボタンとアクセントをより鮮やかでブランドらしいものに感じさせる手軽な方法です。
<div class="shadow-lg shadow-blue-500/50">
Blue tinted shadow
</div>
<div class="shadow-xl shadow-cyan-500/30">
Cyan glow effect
</div>
<div class="shadow-md shadow-black/30">Darker default</div>
<button class="bg-emerald-500 shadow-lg shadow-emerald-500/50">
Colored button shadow
</button>ホバーリフトのパターン
transition-shadow と組み合わせた hover:shadow-{larger} が、標準のカードホバーのパターンです。hover:-translate-y-0.5 と active:translate-y-0 とペアにすると、ボタンに物理的な押下のフィードバックが与えられます — 小さいが満足感のあるインタラクションの磨きです。
<div class="bg-white rounded-xl shadow-md hover:shadow-xl
transition-shadow duration-300 p-6 cursor-pointer">
Card with hover lift
</div>
<!-- Combined with slight translate for tactile feel -->
<button class="shadow-md hover:shadow-sm active:shadow-sm
hover:-translate-y-0.5 active:translate-y-0
transition-all">
Tactile button
</button>Drop Shadow(SVG/テキスト用)
drop-shadow は CSS の filter:drop-shadow() を使うため、(box-shadow が box だけを影とするのに対し)要素の本物の形状に従います(透明度を持つ SVG や PNG に最適)。テキスト上の drop-shadow-md は、text-shadow のより柔らかい代替です。
<h1 class="drop-shadow-md">Text with shadow</h1>
<svg class="drop-shadow-lg">
<!-- SVG shape gets shadowed as a whole -->
</svg>
<!-- Combine with rotate for dramatic effects -->
<div class="rotate-3 drop-shadow-2xl">Tilted card</div>
<!-- Filter utilities affect the element as a whole image -->
<div class="blur-sm brightness-110">Image filters</div>任意とレイヤー化されたシャドウ
デフォルトのスケール外の影のためには、任意値の構文の shadow-[...] を使いましょう。本当に高級感のある(マルチレイヤーの深さを持つ)複雑なレイヤー化された影のためには、カスタム CSS のクラスで一度定義するほうがきれいです — Tailwind の単一レイヤーのシャドウでは、マルチレイヤーの深さに届きません。
<div class="shadow-[0_8px_30px_rgb(0,0,0,0.12)]">
Custom shadow via arbitrary value
</div>
<!-- Multiple layered shadows via custom CSS -->
<style>
.card-shadow {
box-shadow:
0 1px 2px rgba(0,0,0,0.04),
0 4px 8px rgba(0,0,0,0.06),
0 12px 24px rgba(0,0,0,0.08);
}
</style>
<div class="card-shadow">Layered elevation</div>レスポンシブデザイン
ブレークポイント
Tailwind はモバイルファーストです:ベースのスタイルがどこでも適用され、ブレークポイントの接頭辞が min-width で適用されます。順序が重要です — クラスを小さいものから大きいものへ書きましょう(text-sm md:text-base lg:text-lg)。ブレークポイントは一般的なデバイスの幅に一致し、設定可能です。
<!-- Default = mobile first, then min-width overrides -->
<!-- sm: 640px, md: 768px, lg: 1024px, xl: 1280px, 2xl: 1536px -->
<div class="text-sm md:text-base lg:text-lg">
Grows with viewport
</div>
<div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4">
Responsive grid
</div>
<!-- Hide on small screens, show on large -->
<div class="hidden md:block">Desktop only</div>
<div class="block md:hidden">Mobile only</div>レスポンシブな Flex/Grid
レスポンシブなレイアウトの背骨であるのが、flex-col md:flex-row のパターンです:携帯では縦に積み、デスクトップでは横並びにします 。カードのグリッドのためには、レスポンシブの grid-cols-* とペアにしましょう。必ずモバイルのレイアウトを最初に設計し、上へ強化しましょう。
<!-- Stack on mobile, row on desktop -->
<div class="flex flex-col md:flex-row gap-4">
<aside class="md:w-64">Sidebar</aside>
<main class="flex-1">Main</main>
</div>
<!-- 1 col mobile -> 2 col tablet -> 4 col desktop -->
<div class="grid grid-cols-1 sm:grid-cols-2 xl:grid-cols-4 gap-4">
<div>Card</div>
</div>
<!-- Sidebar that hides on mobile -->
<aside class="hidden lg:block lg:w-1/4">Sidebar</aside>コンテナと最大幅
container は、各ブレークポイントでレスポンシブな最大幅と共にコンテンツを中央寄せします(centering とパディングを手動で加える必要があります)。最もよくあるページラッパーが、「max-w-7xl mx-auto px-4 sm:px-6 lg:px-8」の手動パターンです。記事のためには、読みやすい行の長さで max-w-2xl/3xl に制限しましょう。
<div class="container mx-auto px-4">
<!-- Centers content and caps width at each breakpoint -->
</div>
<!-- Or manually cap width -->
<div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
Standard page wrapper
</div>
<article class="prose max-w-none lg:max-w-2xl mx-auto">
Article with readable line length
</article>ブレークポイントでの表示/非表示
モバイルとデスクトップの間で要素を切り替える標準のパターンであるのが、hidden md:block / block md:hidden です(例:ハンバーガーメニュー対フルナビ)。印刷のときだけ表示するのが、print:block です。min-[1200px]: のような任意のブレークポイントで、カスタム幅を狙えます。
<!-- Mobile-only nav -->
<nav class="block md:hidden">Mobile menu</nav>
<!-- Desktop-only nav -->
<nav class="hidden md:flex">Desktop menu</nav>
<!-- Hide print-only content -->
<div class="hidden print:block">Only shows when printing</div>
<!-- Custom breakpoint -->
<div class="min-[1200px]:flex">Custom MQ</div>レスポンシブなタイポグラフィとスペーシング
視覚的な階層のために、各ブレークポイントでタイポグラフィとスペーシングを拡大しましょう。本物の流動 的なタイプのためには、任意値で clamp() を使いましょう:text-[clamp(2rem,5vw,4rem)] — 見出しが、min と max の間でビューポートと共に滑らかに成長します。
<h1 class="text-3xl sm:text-4xl md:text-5xl lg:text-6xl
font-bold leading-tight">
Fluid-ish heading
</h1>
<section class="px-4 sm:px-6 lg:px-12 py-8 md:py-12 lg:py-20">
Scales padding with viewport
</section>
<!-- True fluid type with clamp via arbitrary value -->
<h1 class="text-[clamp(2rem,5vw,4rem)]">
Smoothly scales with viewport
</h1>ダークモード
ダークモードの有効化(class 戦略)
デフォルトで Tailwind は prefers-color-scheme(media)を使います。darkMode:'class' を設定すると、親(典型的には <html>)に「dark」クラスを加えることでダークモードを切り替えられ — テーマスイッチャーに必要です。darkMode:'selector'(v3.4+)は似ていますが、カスタムセレクタをサポートします。
// tailwind.config.js
module.exports = {
darkMode: "class", // default is "media" (prefers-color-scheme)
// ...
};
<!-- Toggle by adding/removing "dark" on <html> -->
<html class="dark">
<body class="bg-white dark:bg-gray-900 text-gray-900 dark:text-gray-100">
Auto-styled in dark
</body>
</html>ダークのバリアント
任意のユーティリティに dark: を接頭すると、ダークモードでのみ適用されます。パターンは、明るいスタイルを最初に書き、その後、dark: で上書きすることです。体系的に:すべての bg-*、text-*、border-* に dark:* の相当物をペアにし、ダークモードで何も壊れて見えないようにしましょう。
<div class="bg-white dark:bg-gray-800
text-gray-900 dark:text-gray-100
border-gray-200 dark:border-gray-700">
Adapts to theme
</div>
<button class="bg-blue-600 dark:bg-blue-500
hover:bg-blue-700 dark:hover:bg-blue-600">
Themed button
</button>テーマのトグルスクリプト
間違ったテーマのフラッシュ(FOUC)を避けるため、ページがレンダリングされる前に、<head> にこのインラインスクリプトを置きましょう。最初に localStorage をチェックし、その後、OS の設定へフォールバックします。トグルは単にクラスを反転し、選択を保存します。
// Check stored preference or system preference on load
if (localStorage.theme === 'dark' ||
(!('theme' in localStorage) &&
window.matchMedia('(prefers-color-scheme: dark)').matches)) {
document.documentElement.classList.add('dark');
} else {
document.documentElement.classList.remove('dark');
}
// Toggle button handler
function toggleTheme() {
document.documentElement.classList.toggle('dark');
localStorage.theme = document.documentElement.classList.contains('dark')
? 'dark' : 'light';
}システム設定のみ
手動のトグルが不要なら、darkMode:'media'(デフォルト)が、ゼロの JavaScript でユーザーの OS の設定に従います。より単純ですが、設定を上書きする能力が失われます。OS の選択を尊重するだけで十分なコンテンツサイトに使いましょう。
// tailwind.config.js
module.exports = {
darkMode: "media", // follow prefers-color-scheme automatically
// ...
};
<!-- dark: utilities apply when OS is in dark mode -->
<div class="bg-white dark:bg-gray-900">
No toggle, just follows the OS
</div>
<!-- Force a specific theme by not using dark: at all
(the dark styles simply never apply) -->ダークモードのベストプラクティス
良いダークモードは、単なる色の反転ではありません:背景には、純粋な黒でなく暗いグレー(gray-900)を、テキストには、純粋な白でなくオフホワイト(gray-100)を使い、画像を少し暗くし、シャドウを減らすか取り除きましょう。彩度の高いブランドカラーは、ダークモードでは明るくする必要がしばしばあります(blue-600 → blue-500)。
<!-- Don't just invert — lower contrast and saturation -->
<div class="bg-white dark:bg-gray-900 <!-- not black -->
text-gray-900 dark:text-gray-100 <!-- not pure white -->
shadow-lg dark:shadow-none"> <!-- shadows look off in dark -->
<!-- Dim images slightly in dark mode -->
<img class="dark:opacity-90 dark:brightness-90" src="/photo.jpg" />
<!-- Reduce border intensity -->
<div class="border-gray-200 dark:border-gray-800">...</div>アニメーションとトランジション
トランジション
どのプロパティがアニメーション化するかを定義するのが、transition-{property} で、時間を設定するのが、duration-{ms}、タイミング関数を設定するのが、ease-{in,out,in-out} です。最もよくあるのが、transition-colors です(transition-all より安価)。hover:* の変化の前に必ず transition を加えましょう。さもなければ、パチッと変わります。
<button class="bg-blue-500 hover:bg-blue-600
transition-colors duration-200">
Color transition
</button>
<div class="transition-all duration-300 ease-in-out
hover:scale-105 hover:shadow-lg">
Multi-property transition
</div>
<div class="transition-opacity duration-500 opacity-50 hover:opacity-100">
Fade in on hover
</div>トランジションのプロパティ
パフォーマンスのために、transition-all より具体的なトランジション(transition-colors、transition-transform)を優先しましょう — all はすべてのプロパティを監視し、ガタつきを引き起こし得ます。duration-* は、75/100/150/200/300/500/700/1000ms を使います。UI 要素が現れるのに自然に感じるのが、ease-out です。
<div class="transition-none">No animation</div>
<div class="transition-colors">bg/text/border-color</div>
<div class="transition-opacity">opacity only</div>
<div class="transition-shadow">box-shadow only</div>
<div class="transition-transform">scale/rotate/translate</div>
<div class="transition-all">Everything (heaviest)</div>
<!-- Duration & easing -->
<div class="duration-150">150ms</div>
<div class="duration-700">700ms</div>
<div class="ease-linear">Linear</div>
<div class="ease-in">Accelerate</div>
<div class="ease-out">Decelerate</div>組み込みのアニメーション
Tailwind は、4 つのキーフレームのアニメーションを出荷します:spin(ローダー)、ping(広がる通知のドット)、pulse(不透明度のフェード — スケルトンローダーに最適)、bounce(遊び心)です。カスタムアニメーションのためには、config でキーフレームを定義し、animate-[name] で参照しましょう。
<div class="animate-spin">Spinning loader</div>
<div class="animate-ping">Pulsing ping (notifications)</div>
<div class="animate-pulse">Fading pulse (skeleton loaders)</div>
<div class="animate-bounce">Bouncing arrow</div>
<!-- Skeleton loader pattern -->
<div class="animate-pulse bg-gray-200 rounded h-4 w-3/4"></div>
<div class="animate-pulse bg-gray-200 rounded h-4 w-1/2 mt-2"></div>Transform
scale-、rotate-、translate-、skew- は、すべて組み合わせられ、transition-transform と共に滑らかにアニメーション化します。transform の原点を変えるのが、origin-{center,top,bottom-left,...} です。控えめに保たれるなら、カードのホバーの演出(scale + わずかな rotate + リフト)は、高級感を感じさせます。
<div class="hover:scale-110 transition-transform">Grow on hover</div>
<div class="hover:rotate-3">Tilt on hover</div>
<div class="hover:-translate-y-1">Lift on hover</div>
<div class="hover:skew-y-3">Skew on hover</div>
<!-- Combined -->
<div class="hover:scale-105 hover:-translate-y-1 hover:rotate-1
transition-transform duration-300">
Card flourish
</div>
<!-- Origin -->
<div class="origin-top-left rotate-12">Rotated from corner</div>設定経由のカスタムアニメーション
theme.extend で、カスタムのキーフレームとアニメーション名を定義しましょう。アニメーショ ンの値は省略記法です:'{name} {duration} {timing} {iteration}' です。一度定義されると、animate-{name} が通常のユーティリティになります。キーフレームは小さく保ちましょう — CSS バンドルに出荷されるのです。
// tailwind.config.js
module.exports = {
theme: {
extend: {
keyframes: {
wiggle: {
"0%, 100%": { transform: "rotate(-3deg)" },
"50%": { transform: "rotate(3deg)" },
},
},
animation: {
wiggle: "wiggle 1s ease-in-out infinite",
},
},
},
};
// Usage
<div class="animate-wiggle">Wiggling</div>フォーム
入力フィールド
標準の input のパターン:全幅の w-full、パディング、ボーダー、rounded、そしてデフォルトの outline を取り除くフォーカスリングです。ネイティブのフォーカスボーダーを隠すのが、focus:border-transparent で、明確なアクセシブルなフォーカスインジケータを描くのが、focus:ring-2 です。明確さのために、disabled 状態を必ずスタイルしましょう。
<input type="text"
class="w-full px-3 py-2 border border-gray-300 rounded-md
focus:outline-none focus:ring-2 focus:ring-blue-500
focus:border-transparent
placeholder-gray-400 text-gray-900"
placeholder="Enter your name" />
<input type="email" disabled
class="opacity-50 cursor-not-allowed" />
<textarea class="w-full rounded-md border-gray-300" rows="4"></textarea>チェックボックスとラジオ
チェックボックス/ラジオ上の text-{color} は、チェックされた色を設定します(内部では accent-color を使います)。キーボードのフォーカスインジケータを加えるのが、focus:ring-* です。カスタムのトグルのためには、隠れたチェックボックスでスタイルを駆動する peer-* のパターンを使うか、@tailwindcss/forms のようなプラグインを使いましょう。
<label class="flex items-center gap-2">
<input type="checkbox" class="rounded border-gray-300
text-blue-600 focus:ring-blue-500" />
Accept terms
</label>
<label class="flex items-center gap-2">
<input type="radio" name="plan"
class="border-gray-300 text-blue-600 focus:ring-blue-500" />
Free plan
</label>
<!-- Custom toggle switch -->
<button role="switch" aria-checked="false"
class="relative w-11 h-6 rounded-full bg-gray-300
peer-checked:bg-blue-600 transition-colors">
<span class="absolute left-0.5 top-0.5 w-5 h-5 bg-white
rounded-full transition-transform peer-checked:translate-x-5">
</span>
</button>Select と Options
ネイティブの <select> のスタイリングは、ブラウザをまたいで限定的です。完全な制御のためには、カスタムのドロップダウンコンポーネントか、Headless UI のようなライブラリを使いましょう。@tailwindcss/forms のプラグインは、すべてのフォーム要素に一貫した、容易にテーマ化可能なベースラインを与えます — フォーム重視のアプリには導入の価値があります。
<select class="w-full px-3 py-2 border border-gray-300 rounded-md
bg-white text-gray-900 focus:ring-2 focus:ring-blue-500">
<option value="">Choose...</option>
<option value="us">United States</option>
<option value="cn">China</option>
</select>
<!-- Multiple select -->
<select multiple class="h-32">
<option>Tag 1</option>
<option>Tag 2</option>
</select>フォームのレイアウト
フォームの行のスペースの最もきれいな方法であるのが、space-y-4 です — フィールドごとのマージンが不要です。一貫したラベルのスタイリングのために、各入力を <label class='block text-sm font-medium mb-1'> とペアにしましょう。デスクトップでフォームの幅を読みやすく保つため、max-w-md で包みましょう。
<form class="space-y-4 max-w-md">
<div>
<label class="block text-sm font-medium text-gray-700 mb-1">
Email
</label>
<input type="email" class="w-full ..." />
</div>
<div>
<label class="block text-sm font-medium text-gray-700 mb-1">
Password
</label>
<input type="password" class="w-full ..." />
</div>
<button type="submit" class="w-full bg-blue-600 text-white py-2 rounded">
Sign in
</button>
</form>ボタン
ボタ ンには、hover だけでなく、active:(押された)、disabled:(状態)、focus:(キーボード)のバリアントが必要です。物理的なフィードバックを与えるのが、active:bg-{darker} です。標準の disabled の扱いが、disabled:opacity-50 disabled:cursor-not-allowed です。ボタンのバリアント間で、一貫した高さ(例:py-2)を保ちましょう。
<button class="bg-blue-600 hover:bg-blue-700 active:bg-blue-800
text-white font-medium py-2 px-4 rounded-md
transition-colors focus:outline-none
focus:ring-2 focus:ring-blue-500 focus:ring-offset-2">
Primary
</button>
<button class="bg-white border border-gray-300 hover:bg-gray-50
text-gray-700 py-2 px-4 rounded-md transition-colors">
Secondary
</button>
<button class="text-blue-600 hover:text-blue-800 hover:underline">
Ghost link button
</button>
<button class="bg-red-600 hover:bg-red-700 disabled:opacity-50
disabled:cursor-not-allowed" disabled>
Disabled
</button>カード
基本のカード
標準のカード:rounded + shadow + overflow-hidden です(画像の角がきれいにクリップされます)。画像の歪みを防ぐのが、object-cover です。transition-shadow 付きの hover:shadow-lg が、リフト効果を与えます。rounded の画像の上部のための鍵が、overflow-hidden です。
<div class="max-w-sm rounded-xl shadow-md overflow-hidden
bg-white hover:shadow-lg transition-shadow">
<img class="w-full h-48 object-cover" src="/photo.jpg" alt="" />
<div class="p-6">
<h3 class="text-xl font-semibold mb-2">Card title</h3>
<p class="text-gray-600">Card description goes here.</p>
<button class="mt-4 text-blue-600 font-medium hover:underline">
Learn more
</button>
</div>
</div>カードのグリッド
カードのグリッドには、auto-fill + minmax のパターンを使いましょう:カードは少なくとも 280px で、満たすよう伸び、自動的に折り返します — メディアクエリなしです。既知のブレークポイントでの固定の列数のためには、レスポンシブの grid-cols-* のアプローチのほうが推論しやすいです。
<div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3
gap-6">
<div class="rounded-xl shadow p-6 bg-white">Card 1</div>
<div class="rounded-xl shadow p-6 bg-white">Card 2</div>
<div class="rounded-xl shadow p-6 bg-white">Card 3</div>
<div class="rounded-xl shadow p-6 bg-white">Card 4</div>
</div>
<!-- Auto-fit grid for variable width -->
<div class="grid gap-6
grid-cols-[repeat(auto-fill,minmax(280px,1fr))]">
<!-- Cards naturally wrap based on viewport -->
</div>インタラクティブなカード(全体がクリック可能)
カード全体を <a> で包むと、完全にクリック可能になります(別の「Read more」ボタンより良い UX です)。group + group-hover のパターンは、親がホバーされたときに子の要素(タイトルの色、矢印の位置)をアニメーション化させてくれます — インタラクティブなカードに強力です。
<a href="/post" class="group block max-w-sm rounded-xl
shadow-md hover:shadow-xl transition-shadow bg-white">
<div class="p-6">
<h3 class="font-semibold group-hover:text-blue-600 transition-colors">
Card title
</h3>
<p class="text-gray-600 mt-2">Description...</p>
<span class="mt-4 inline-flex items-center text-blue-600">
Read more
<svg class="w-4 h-4 ml-1 group-hover:translate-x-1 transition-transform">
<!-- arrow icon path -->
</svg>
</span>
</div>
</a>フッター付きのカード
カードに flex flex-col を、ボディに flex-1 を使いましょう。カードのボディの長さが異なってもフッターが底部に留まり、カードの行が整列し続けます。フッターは典型的に、控えめな bg と上のボーダー付きの、メタデータとアクションを持ちます。
<div class="max-w-sm rounded-xl shadow bg-white flex flex-col">
<div class="p-6 flex-1">
<h3 class="text-lg font-semibold">Title</h3>
<p class="text-gray-600 mt-2">Body content...</p>
</div>
<div class="px-6 py-4 bg-gray-50 border-t border-gray-100
flex justify-between items-center">
<span class="text-sm text-gray-500">Aug 12, 2025</span>
<button class="text-sm text-blue-600 hover:underline">Share</button>
</div>
</div>プロフィール/アバターのカード
アバター + 名前 + アクションが、標準の行カードのパターンです。整列させるのが、flex items-center gap-4 です。ステータ スのドットのためには、カードの bg に一致するリングで絶対配置の右下に置き、ドットを「切り抜き」ましょう。アバターは、rounded-full + 等しい w/h + object-cover を使います。
<div class="flex items-center gap-4 p-4 rounded-xl shadow bg-white">
<img src="/avatar.jpg"
class="w-12 h-12 rounded-full object-cover" alt="" />
<div class="flex-1">
<p class="font-semibold">Alice Zhang</p>
<p class="text-sm text-gray-500">Software Engineer</p>
</div>
<button class="text-blue-600 text-sm font-medium">Follow</button>
</div>
<!-- Status indicator -->
<div class="relative">
<img class="w-10 h-10 rounded-full" src="/avatar.jpg" />
<span class="absolute bottom-0 right-0 w-3 h-3
bg-green-500 rounded-full ring-2 ring-white"></span>
</div>レイアウトのパターン
Position
主力であるのが、relative + absolute です:親に position:relative を、子に position:absolute を設定すれば、正確に配置できます。inset-0 = top/right/bottom/left:0(親を満たします)です。スティッキーヘッダーに不可欠なのが、sticky top-0 です — スクロールできるよう親が十分高くなければなりません。
<div class="static">Default flow</div>
<div class="relative">
<div class="absolute top-0 right-0">Top-right of parent</div>
</div>
<div class="fixed bottom-4 right-4">Stuck to viewport</div>
<div class="sticky top-0">Sticks when scrolling</div>
<!-- Inset shorthand for all sides -->
<div class="absolute inset-0">Fills parent</div>
<div class="absolute inset-x-0 bottom-0">Full width, at bottom</div>Z-Index とスタッキング
Tailwind は、z-auto と共に z-0/10/20/30/40/50 を提供します。一貫した規約を使いましょう:0 = ベース、10 = ドロップダウン、20 = sticky、30 = モーダル、40 = オーバーレイ、50 = トースト/ツールチップです。モーダルには、高い z-index と、ビューポートを覆う fixed inset-0 の両方が必要です。
<div class="z-0">Base layer</div>
<div class="z-10">Dropdowns</div>
<div class="z-20">Sticky headers</div>
<div class="z-30">Modals</div>
<div class="z-40">Overlays</div>
<div class="z-50">Tooltips, toasts</div>
<!-- Combine with relative/absolute/fixed -->
<div class="fixed inset-0 z-50 bg-black/50">Modal overlay</div>Display
基本であるのが、block/inline-block/inline/hidden です。幅/高さを必要とするが、インラインで座るべき要素に有用なのが、inline-block です。hidden はレイアウトから完全に取り除きます(スペースを保つ opacity-0 と対照的です)。レスポンシブな表示/非表示のために、ブレークポイントの接頭辞で切り替えましょう。
<div class="block">Full-width block</div>
<div class="inline-block">Inline with set width/height</div>
<div class="inline">Flows inline</div>
<div class="hidden">Removed from layout</div>
<div class="flex">Flex container</div>
<div class="grid">Grid container</div>
<!-- Toggle on breakpoint -->
<div class="hidden md:block">Hidden on mobile</div>
<div class="block md:hidden">Hidden on desktop</div>
<!-- Tables, contents, list-item also available -->Overflow
コンテンツをクリップするのが、overflow-hidden です(rounded の角が子をクリップさせるためにも使われます)。必要なときだけスクロールバーを加えるのが、overflow-auto で、nav の行とコードブロックの標準の水平スクロールのパターンが、whitespace-nowrap 付きの overflow-x-auto です。
<div class="overflow-hidden">Clips overflow</div>
<div class="overflow-auto">Scrollbars if needed</div>
<div class="overflow-scroll">Always scrollable</div>
<div class="overflow-visible">Default, overflows</div>
<!-- Per-axis -->
<div class="overflow-x-auto whitespace-nowrap">Horizontal scroll</div>
<div class="overflow-y-auto h-64">Vertical scroll area</div>
<!-- Hide scrollbars while keeping scroll (Webkit) -->
<div class="overflow-x-auto scrollbar-hide">...</div>Float と Clear
レイアウトのための float は、flexbox/grid にほぼ置き換えられましたが、画像の周りのテキストの折り返しには、float-left + mr-4 が今でもうまく機能します。ほとんどの画像のサイジングのニーズのためには、寸法を設定した img 上の object-cover(満たす、切 り取るかも)か object-contain(収める、レターボックスかも)が、モダンなアプローチです。
<img class="float-left mr-4 rounded" src="/photo.jpg" />
<p>Text wraps around the floated image on the right...</p>
<p class="clear-both">After the float</p>
<!-- Floats are mostly legacy; prefer flex/grid for layout.
Still useful for text wrapping around images. -->
<!-- Object fit for responsive images -->
<img class="w-full h-48 object-cover" src="/hero.jpg" />
<img class="w-24 h-24 object-contain" src="/logo.png" />エフェクトとフィルタ
Backdrop Blur(グラスモーフィズム)
要素の背後のものを(要素自身でなく)ぼかすのが、backdrop-blur-* です。半透明の bg(bg-white/30)と組み合わせると、グラスモーフィズムの効果が作れます。モーダル、コンテンツの上のスティッキーヘッダー、iOS 風のすりガラスのパネルに最適です。
<div class="backdrop-blur-md bg-white/30 border border-white/20
rounded-xl p-6 shadow-lg">
Frosted glass card
</div>
<!-- Strengths -->
<div class="backdrop-blur-sm">Light blur</div>
<div class="backdrop-blur-md">Medium</div>
<div class="backdrop-blur-xl">Heavy</div>
<div class="backdrop-blur-2xl">Maximum</div>
<!-- Modal overlay with blur -->
<div class="fixed inset-0 bg-black/50 backdrop-blur-sm z-50">
Overlay with frosted bg
</div>画像のフィルタ
フィルタのユーティリティは、CSS の filter 関数にマッピングします:blur、brightness、contrast、grayscale、invert、sepia、saturate、hue-rotate です。チームの写真や商品の画像に人気の効果であるのが、grayscale hover:grayscale-0 のパターンです — ホバーで色が現れます。
<img class="blur-sm" src="/photo.jpg" />
<img class="blur-md" src="/photo.jpg" />
<img class="brightness-110" src="/photo.jpg" />
<img class="contrast-125" src="/photo.jpg" />
<img class="grayscale" src="/photo.jpg" />
<img class="invert" src="/photo.jpg" />
<img class="sepia" src="/photo.jpg" />
<img class="saturate-150" src="/photo.jpg" />
<!-- Combined + hover -->
<img class="grayscale hover:grayscale-0 transition-all duration-500"
src="/photo.jpg" />Mix Blend Mode
要素が背後のものとどう混ざるかを制御するのが、mix-blend-mode です。画像の上の白いテキストの mix-blend-difference は、下の画像にかかわらず読める高コントラストの効果を作ります — ヒーローセクションで人気です。控えめに使いましょう。やや安っぽく見え得ます。
<div class="mix-blend-multiply">Multiplies with bg</div>
<div class="mix-blend-screen">Lightens</div>
<div class="mix-blend-overlay">Contrast blend</div>
<div class="mix-blend-difference">Inverts colors</div>
<!-- Text over image with blend -->
<div class="relative">
<img src="/photo.jpg" />
<h1 class="absolute inset-0 flex items-center justify-center
text-white mix-blend-difference text-6xl font-bold">
Overlay text
</h1>
</div>不透明度
要素の不透明度を設定するのが、opacity-{0,25,50,75,100}(5/10/20/.../95 も)です — 要素とその子の両方に影響します。半透明の背景だけなら、代わりに bg-{color}/{opacity} を使いましょう。テキストが完全に不透明のまま保たれます。隠すがレイアウトを保つのが、opacity-0 です(取り除く hidden と対照的です)。
<div class="opacity-100">Fully visible</div>
<div class="opacity-75">75%</div>
<div class="opacity-50">Half</div>
<div class="opacity-25">Mostly transparent</div>
<div class="opacity-0">Invisible (still takes space)</div>
<!-- Hover fade -->
<button class="opacity-100 hover:opacity-80 transition-opacity">
Fades on hover
</button>
<!-- vs bg-black/50: opacity affects the whole element
(including children), bg color/opacity only the background -->カーソルとユーザー選択
カーソルを必ず挙動に合わせましょう:クリック可能には cursor-pointer、無効には cursor-not-allowed です。テキストの選択を無効化するのが、select-none です(UI のクロームとダブルクリックされやすいボタンに有用)。一貫したレイアウトが欲しいとき、textarea のデフォルトのリサイズハンドルを防ぐのが、resize-none です。
<button class="cursor-pointer">Pointer on hover</button>
<div class="cursor-not-allowed opacity-50">Disabled</div>
<div class="cursor-move">Drag handle</div>
<div class="cursor-text">Text input area</div>
<!-- Selection -->
<p class="select-none">Cannot be selected</p>
<p class="select-text">Selectable text</p>
<p class="select-all">Selects all on click</p>
<!-- Resize handle -->
<textarea class="resize-none"></textarea>
<textarea class="resize-y"></textarea>トランジションと transform
Transform の原点とスケール
transform の原点を設定するのが、origin-* です。軸ごとに独立に拡縮するのが、scale-x-* / scale-y-* です。より滑らかなアニメーションのために GPU の加速(translate3d)を強制するのが、transform-gpu で — カルーセルのような transform 重視の要素でガタつきを見たら使いましょう。
<div class="origin-center scale-110">Center origin (default)</div>
<div class="origin-top-left scale-90">Top-left origin</div>
<div class="origin-bottom rotate-45">Bottom origin, rotated</div>
<!-- Combined transforms -->
<div class="transform-gpu hover:scale-105 hover:rotate-3
transition-transform duration-300">
GPU-accelerated flourish
</div>
<!-- Independent scale axes -->
<div class="hover:scale-x-110">Stretches horizontally</div>
<div class="hover:scale-y-90">Squishes vertically</div>Translate と Rotate
要素をスペーシングのスケールを使って 動かすのが、translate-{x,y}-{n} です(-translate-x-1/2 で 50% になる /2 も)。left-1/2 + -translate-x-1/2 のパターンは、幅にかかわらず絶対配置の要素を完璧に中央寄せします — ツールチップとモーダルに有用です。
<div class="translate-x-4">16px right</div>
<div class="-translate-x-4">16px left</div>
<div class="translate-y-2">8px down</div>
<div class="rotate-45">45deg clockwise</div>
<div class="-rotate-12">12deg counter-clockwise</div>
<div class="rotate-180">Flipped</div>
<!-- Centering trick: absolute + translate -->
<div class="absolute left-1/2 top-1/2
-translate-x-1/2 -translate-y-1/2">
Perfectly centered
</div>Skew と 3D の transform
軸に沿って要素を傾けるのが、skew です — 控えめな平行四辺形の効果に有用です。3D の transform(perspective、rotate-y、rotate-x、transform-style-3d)は、デフォルトの Tailwind ではテーマ拡張を必要としますが、カードのフリップとパララックスの効果を可能にします。ほとんどのプロジェクトは、2D の transform だけを使います。
<div class="skew-x-12">Skewed horizontally</div>
<div class="skew-y-6">Skewed vertically</div>
<div class="-skew-x-6">Counter skew</div>
<!-- 3D (needs perspective on parent) -->
<div class="perspective-1000">
<div class="rotate-y-12 transform-style-3d">Tilted in 3D</div>
</div>
<!-- Card flip pattern -->
<div class="preserve-3d group hover:rotate-y-180
transition-transform duration-700">
Flips on hover (needs config)
</div>トランジションの遅延とタイミング
トランジションの開始を遅らせるのが、delay-{ms} です — 子ごとの遅延と組み合わせると、ずらした登場が作れます(各アイテムがわずかに後から始まります)。現れるものには(場所へ減速し)、ease-out が自然に感じられ、消えるものには(加速して去る)、ease-in です。
<div class="transition-colors duration-300 delay-150
hover:bg-blue-500">
Starts after 150ms
</div>
<!-- Stagger children via inline delay style -->
<div class="flex gap-2">
<div class="transition-all delay-0 duration-500">A</div>
<div class="transition-all delay-100 duration-500">B</div>
<div class="transition-all delay-200 duration-500">C</div>
</div>
<!-- Easing -->
<div class="ease-in-out">Smooth</div>
<div class="ease-out">Decelerate (good for entrances)</div>
<div class="ease-in">Accelerate (good for exits)</div>Hover 対 Focus 対 Active の状態
hover に加え、focus(キーボードのユーザー)と active(マウスダウン)を必ず考えましょう。フォーカスリングには、focus: でなく focus-visible: を使います — キーボードのフォーカス(必要な人)にだけリングを表示し、マウスクリック(自身の視覚的フィードバックを持つ)では隠します。デフォルトのブラウザのリングを抑えるのが、focus:outline-none です。親のホバーで子のスタイルを駆動するのが、group + group-hover: です — カードと複雑なホバーに不可欠です。
<button class="bg-blue-500
hover:bg-blue-600 <!-- mouse over -->
focus:bg-blue-700 <!-- keyboard focus -->
active:bg-blue-800 <!-- mouse down -->
focus:outline-none focus:ring-2 focus:ring-blue-300
transition-colors">
All states styled
</button>
<!-- Group hover: style children when parent hovered -->
<div class="group">
<button class="group-hover:scale-110 transition-transform">
Scales when parent hovered
</button>
</div>サイジング
幅のスケール
w-* は、スペーシングのスケールと、パーセンテージのための分数(1/2、1/3、2/3、1/4、3/4、1/5、...、1/12)を使います。w-full = 100%、w-screen = 100vw、w-fit = 内因的な幅、w-max = max-content です。読みやすい幅で制限されるレスポンシブなコンテンツのためには、w-full + max-w-{size} を使いましょう。
<div class="w-0">0</div>
<div class="w-px">1px</div>
<div class="w-1">0.25rem</div>
<div class="w-4">1rem (16px)</div>
<div class="w-16">4rem (64px)</div>
<div class="w-64">16rem</div>
<div class="w-96">24rem</div>
<!-- Fractions -->
<div class="w-1/2">50%</div>
<div class="w-1/3">33.33%</div>
<div class="w-2/3">66.67%</div>
<div class="w-full">100%</div>
<div class="w-screen">100vw</div>高さとビューポート
h-screen = 100vh ですが、モバイルのブラウザのアドレスバーの問題があります。モバイルフレンドリーな全高のレイアウトには、h-dvh(動的なビューポートの高さ)を優先しましょう — ブラウザの chrome が表示/非表示になると調整します。スティッキーフッターのレイアウトの標準が、min-h-screen です。
<div class="h-8">2rem</div>
<div class="h-64">16rem</div>
<div class="h-full">100% of parent</div>
<div class="h-screen">100vh (full viewport)</div>
<!-- Modern viewport units (v3.4+) -->
<div class="h-dvh">Dynamic viewport height (mobile-friendly)</div>
<div class="h-svh">Small viewport height</div>
<div class="h-lvh">Large viewport height</div>
<!-- min/max -->
<div class="min-h-screen">At least viewport tall</div>
<div class="max-h-96 overflow-y-auto">Capped scroll area</div>Min/Max 幅
max-w-{xs,sm,md,lg,xl,2xl,...,7xl} は、コンテンツの幅に理想的な rem ベースのサイズにマッピングします。標準であるのが、ページのコンテンツを max-w-7xl mx-auto で包むことです。重要な flexbox のトリックであるのが、min-w-0 です — flex の子が内因的なサイズ以下に縮小でき、溢れを防ぎます。
<!-- Max-width is the key to readable content -->
<article class="max-w-2xl mx-auto">
Caps article at ~42rem (readable line length)
</article>
<!-- Common max-widths -->
<div class="max-w-sm">384px</div>
<div class="max-w-md">28rem</div>
<div class="max-w-lg">32rem</div>
<div class="max-w-xl">36rem</div>
<div class="max-w-7xl">80rem</div>
<!-- min-width for inputs etc -->
<input class="min-w-0" /> <!-- prevent flex overflow -->
<div class="min-w-full">Always at least full width</div>アスペクト比
幅にかかわらず固定のアスペクト比を保つのが、aspect-{ratio} です — 古い padding-bottom のハックよりずっときれいです。ビデオの埋め込みの標準が、aspect-video です。歪みなしに box を満たすには、object-cover と組み合わせましょう。aspect-[21/9] のような任意の比率も機能します。
<div class="aspect-square">1:1 square</div>
<div class="aspect-video">16:9 widescreen</div>
<div class="aspect-[4/3]">4:3</div>
<div class="aspect-[21/9]">Ultrawide</div>
<!-- Responsive video embed -->
<div class="aspect-video w-full">
<iframe class="w-full h-full" src="..."></iframe>
</div>
<!-- Avatar / product image -->
<img class="aspect-square object-cover w-24" />Object Fit と Position
<img>(と <video>)上の object-fit は、ソースが box をどう満たすかを制御します:cover(満たす、切り取る)、contain(収める、レターボックス)、fill(引き伸ばす)、none(元のサイズ)です。切り取られるときどの部分が保たれるかを制御するのが、object-{position} です。ヒーロー画像のデフォルトが、object-cover + object-center です。
<img class="w-full h-48 object-cover" src="/hero.jpg" />
<!-- Fills the box, may crop. Best for hero images. -->
<img class="w-full h-48 object-contain" src="/logo.png" />
<!-- Fits entire image, may letterbox. Best for logos. -->
<img class="w-full h-48 object-fill" src="/img.jpg" />
<!-- Stretches to fill (distorts). Rarely what you want. -->
<img class="w-full h-48 object-none" src="/img.jpg" />
<!-- Keeps original size, may overflow. -->
<!-- Position -->
<img class="object-cover object-top" src="/portrait.jpg" />
<img class="object-cover object-center" src="/photo.jpg" />インタラクティビティと状態
Hover、Focus、Group
要素を状態のソースに変えるのが、group で、親がホバーされたときに子へスタイルを適用するのが、group-hover:* です。カードのホバーが、画像、タイトル、隠れたアクションに一度に影響するカードにとって重要です。名前付きのグループ(group/card)は、ネストを可能にします。
<button class="hover:bg-blue-600">Hover state</button>
<input class="focus:ring-2 focus:ring-blue-500" />
<div class="active:scale-95">Press feedback</div>
<!-- group / group-hover: style children based on parent state -->
<div class="group">
<img class="group-hover:scale-110 transition-transform" />
<h3 class="group-hover:text-blue-600">Title</h3>
<p class="opacity-0 group-hover:opacity-100">Revealed on hover</p>
</div>Peer(兄弟の状態)
要素をその兄弟のための状態のソースとして印付けるのが、peer です(デフォルトでは、後の兄弟のみ)。チェックボックス/ラジオの状態に基づき兄弟をスタイルさせるのが、peer-checked:* で — JavaScript なしの CSS だけのトグル、パスワード表示のボタン、アコーディオンを駆動します。
<!-- Style a sibling based on an input's state -->
<input type="checkbox" id="c" class="peer" />
<label for="c" class="peer-checked:text-blue-600">
Styled when checkbox checked
</label>
<!-- Show/hide password -->
<input type="checkbox" id="show" class="peer hidden" />
<label for="show" class="peer-checked:hidden">Show</label>
<label for="show" class="hidden peer-checked:inline">Hide</label>
<!-- Toggle a panel -->
<input type="checkbox" class="peer hidden" />
<div class="hidden peer-checked:block">Toggleable panel</div>Focus-Visible(キーボードのみ)
フォーカスリングには、focus: でなく focus-visible: を使いましょう — キーボードのユーザー(リングを必要とする人)にだけリングを表示し、マウスクリック(自身の視覚的フィードバックを持つ)では隠します。デフォルトのブラウザのリングを抑えるのが、focus:outline-none とのペアです。
<button class="focus:outline-none
focus-visible:ring-2 focus-visible:ring-blue-500">
No ring on mouse click, ring on keyboard focus
</button>
<!-- Why focus-visible: -->
<!-- - focus: applies on BOTH mouse click and keyboard
(annoying for mouse users)
- focus-visible: only on keyboard navigation
(the accessible choice for focus rings) -->Placeholder と File 入力
プレースホルダーのテキストをスタイルするのが、placeholder-* です。<input type='file'> の内側の擬似ボタンをスタイルするのが、file:* で — スタイリングが厄介に難しい要素ですが、Tailwind の file: バリアントが扱いやすくしてくれます。上のパターンは、きれいでブランドらしいファイルアップロードのボタンを与えます。
<input class="placeholder-gray-400 placeholder-italic"
placeholder="Type here..." />
<!-- Style the file input button -->
<input type="file"
class="block w-full text-sm text-gray-500
file:mr-4 file:py-2 file:px-4
file:rounded-md file:border-0
file:text-sm file:bg-blue-50
file:text-blue-700
hover:file:bg-blue-100" />Marker と Selection
リストの箇条書き/番号をスタイルするのが、marker:* です。ユーザーがテキストを選択したときのハイライトをスタイルするのが、selection:* です。ドロップキャップとスモールキャップの最初の行を可能にするのが、first-letter:* / first-line:* です — 編集的なデザインに有用です。これらの擬似要素すべてに、専用の Tailwind のバリアントがあります。
<ul class="marker:text-blue-500 list-disc pl-5">
<li>Blue bullet</li>
<li>Blue bullet</li>
</ul>
<!-- Style text selection -->
<p class="selection:bg-blue-200 selection:text-blue-900">
Select this text to see a blue highlight.
</p>
<!-- Global selection (in CSS) -->
::selection { background: #bfdbfe; color: #1e3a8a; }
<!-- First-line / first-letter -->
<p class="first-letter:text-5xl first-letter:font-bold
first-letter:float-left first-letter:mr-2
first-line:uppercase first-line:tracking-wide">
Drop cap paragraph.
</p>任意値とカスタマイズ
任意値
角括弧により、テーマを拡張せずに任意の CSS の値が使えます — 1 回だけの利用に最適です。値内のアンダースコアはスペースになります(grid-cols-[200px_1fr] が「200px 1fr」になります)。使いすぎは一貫しないデザインに至るため、繰り返される値にはテーマ拡張を優先しましょう。
<!-- Any CSS value in square brackets -->
<div class="w-[300px] h-[200px] bg-[#1da1f2]">
Custom sizes and colors
</div>
<p class="text-[13px] leading-[1.6]">
Custom typography
</p>
<div class="grid-cols-[200px_1fr_100px]">
Custom grid template (underscores = spaces)
</div>
<div class="top-[117px]">
Custom positioning
</div>任意のプロパティ
任意の CSS プロパティを定義するのが、[property:value] です — Tailwind がユーティリティを持たないプロパティ(mask-type、hyphens など)に有用です。CSS 変数を設定するのが、[--var:value] で、その後、p-[var(--tw)] のような任意値で参照できます。テーマ化に強力です。
<!-- Define an arbitrary CSS property -->
<div class="[mask-type:luminance]">
Sets mask-type: luminance
</div>
<!-- Arbitrary property with Tailwind variable -->
<div class="[--scroll-offset:56px] top-[var(--scroll-offset)]">
Custom CSS variable
</div>
<!-- CSS variables + arbitrary values are powerful together -->
<div class="[--tw:1rem] p-[var(--tw)]">
Theme-driven custom value
</div>任意のバリアント
Tailwind のバリアントシステムを拡張するのが、任意のバリアントです:カスタムブレークポイントには min-[1200px]:、ARIA の状態には aria-*:、縞模様には odd:/even:、関係的なスタイリングには has-[:checked]: です。子の状態に基づき親をスタイルできる(最近の CSS の機能である)has-* バリアントが、特に強力です。
<!-- Arbitrary media query -->
<div class="min-[1200px]:flex">Custom breakpoint</div>
<!-- ARIA attribute variants -->
<button aria-pressed="true"
class="aria-pressed:bg-blue-600">
Toggle styled by ARIA state
</button>
<!-- nth-child -->
<li class="odd:bg-gray-50">Striped table row</li>
<li class="even:bg-white">Striped table row</li>
<!-- has: variant (Tailwind v3.4+) -->
<div class="has-[:checked]:bg-blue-50">
Highlights when it contains a checked input
</div>テーマ拡張
デフォルトに追加するのが、theme.extend で、置き換えるのが、theme.{key} です。DEFAULT により、ベース名(bg-brand)とシェード(bg-brand-dark)が使えます。カスタムのスペーシング、ブレークポイント、角丸などを加えましょう。ほとんど常に欲しいのが拡張です — カラーパレットの置き換えは、Tailwind のデフォルトを取り除きます。
// tailwind.config.js
module.exports = {
theme: {
extend: {
colors: {
brand: { light: "#5eead4", DEFAULT: "#14b8a6", dark: "#0f766e" },
},
spacing: { 18: "4.5rem" },
borderRadius: { xl: "1rem" },
screens: { tall: { raw: "(min-height: 800px)" } },
},
},
};
<!-- Now you can use: -->
<div class="bg-brand text-brand-light p-18 rounded-xl">
Custom themed utilities
</div>プラグインと @apply
新しいユーティリティかコンポーネントを加えるのが、プラグインです(forms、typography、aspect-ratio が公式です)。Tailwind のユーティリティをカスタム CSS へインライン化させるのが、@apply で — 同一に使う クラスが数十箇所あるときに有用です。@apply を使いすぎず、ユーティリティファーストのアプローチのほうが通常きれいです。
// tailwind.config.js — register a plugin
const forms = require("@tailwindcss/forms");
module.exports = { plugins: [forms] };
/* Use @apply to compose utilities into custom CSS */
@layer components {
.btn-primary {
@apply bg-blue-600 text-white font-medium py-2 px-4
rounded-md hover:bg-blue-700 transition-colors;
}
}
<!-- Then use the custom class -->
<button class="btn-primary">Save</button>実世界のパターン
モーダル/ダイアログ
モーダルの解剖:fixed inset-0 のオーバーレイ(ビューポートを覆う)、z-50(すべての上)、パネルのための flex の中央寄せ、そしてオーバーレイの上に座るよう相対配置されるパネル自体です。磨きを加えるのが、backdrop-blur です。アクセシビリティのために、可視の閉じるボタンと ESC で閉じるを必ず提供しましょう。
<div class="fixed inset-0 z-50 flex items-center justify-center">
<!-- Overlay -->
<div class="absolute inset-0 bg-black/50 backdrop-blur-sm"></div>
<!-- Panel -->
<div class="relative bg-white rounded-xl shadow-2xl
w-full max-w-md mx-4 p-6">
<h2 class="text-lg font-semibold mb-4">Modal title</h2>
<p class="text-gray-600">Modal body content.</p>
<div class="flex justify-end gap-2 mt-6">
<button class="px-4 py-2 text-gray-600">Cancel</button>
<button class="px-4 py-2 bg-blue-600 text-white rounded-md">OK</button>
</div>
</div>
</div>トースト/通知
トーストは角に固定されます(右下が慣例で)、幅が制限され、タイプを示す色付きの左ボーダーが付きます(緑 = 成功、赤 = エラー、黄 = 警告、青 = 情報)。JS 駆動のアニメーション(translate-x によるスライドイン)と数秒後の自動消去とペアにしましょう。
<div class="fixed bottom-4 right-4 z-50 max-w-sm
bg-white rounded-lg shadow-xl border-l-4 border-green-500
flex items-center gap-3 p-4">
<div class="text-green-500">
<svg class="w-5 h-5"><!-- check icon --></svg>
</div>
<div class="flex-1">
<p class="font-medium text-gray-900">Saved successfully</p>
<p class="text-sm text-gray-500">Your changes are live.</p>
</div>
<button class="text-gray-400 hover:text-gray-600">×</button>
</div>バッジとピル
バッジは、inline-flex のピル(rounded-full)かラベル(rounded)です。ソフトなパステルのスタイルには、bg-{color}-100 + text-{color}-800 を使いましょう。ステータスインジケータのためには、ドット(w-1.5 h-1.5 rounded-full)を加えます。バッジのテキストのスケールには、text-xs font-medium を保ちましょう。
<span class="inline-flex items-center px-2.5 py-0.5
rounded-full text-xs font-medium
bg-green-100 text-green-800">
Active
</span>
<span class="inline-flex items-center gap-1 px-2 py-0.5
rounded-full text-xs font-medium
bg-blue-100 text-blue-800">
<span class="w-1.5 h-1.5 rounded-full bg-blue-500"></span>
Online
</span>
<span class="inline-flex items-center px-2 py-0.5
rounded text-xs font-medium
bg-red-100 text-red-800">
Deprecated
</span>空の状態
空の状態に必要なもの:ソフトな円の中のアイコン、短い見出し、補足のテキスト、主要な CTA です。すべてを中央寄せし(text-center mx-auto)、寛大な py-12 のパディングを使い、色を抑え(gray-400/500 のテキスト)、CTA ボタンが目を引くようにしましょう。「No data」をただ表示してはいけません。
<div class="text-center py-12 px-4">
<div class="mx-auto w-16 h-16 rounded-full bg-gray-100
flex items-center justify-center mb-4">
<svg class="w-8 h-8 text-gray-400"><!-- icon --></svg>
</div>
<h3 class="text-lg font-medium text-gray-900">No items yet</h3>
<p class="mt-1 text-sm text-gray-500">
Get started by creating your first item.
</p>
<button class="mt-4 bg-blue-600 text-white px-4 py-2 rounded-md">
New item
</button>
</div>ローディングスケルトン
スケルトンローダーは、本物のコンテンツの形をしたグレーのブロックのコンテナに、animate-pulse を使います。データが届いたときの急なシフトがないよう、ロードされた状態のレイアウトと比率に合わせましょう。アバターを模すのが、rounded-full + w-10 h-10 で、テキストの行を模すのが、様々な幅(w-3/4、w-1/2)です。
<div class="animate-pulse space-y-3">
<div class="h-4 bg-gray-200 rounded w-3/4"></div>
<div class="h-4 bg-gray-200 rounded w-1/2"></div>
<div class="space-y-2">
<div class="h-3 bg-gray-200 rounded"></div>
<div class="h-3 bg-gray-200 rounded"></div>
</div>
<div class="flex gap-4">
<div class="w-10 h-10 bg-gray-200 rounded-full"></div>
<div class="flex-1 space-y-2 py-1">
<div class="h-3 bg-gray-200 rounded"></div>
<div class="h-3 bg-gray-200 rounded w-5/6"></div>
</div>
</div>
</div>関連する Tailwind CSS スニペット
Copy-paste ready code for common tasks.
Utility Classes
Compose a button and card from atomic utilities.
Responsive Design
Apply per-breakpoint utilities with sm:, md:, and lg: prefixes.
Hover & Focus States
Style interactive states and group-hover with variant prefixes.
Flexbox & Grid
Center, distribute, and grid layouts with utilities.
Colors
Use the default palette, opacity modifiers, and gradients.
Spacing
Apply padding, margin, gap, and width/height from one scale.
Custom Configuration
Extend the theme with custom colors, fonts, and animations.
Dark Mode
Toggle themes with the dark: variant and class strategy.
Was this helpful?