はじめに
ReactでのTailwindCSSの適用方法で少しつまずいたので、セットアップ方法をまとめます。
一般的な方法、Viteを用いた方法をそれぞれ記載しています。
セットアップ方法
-
プロジェクト作成
npx create-react-app my-project cd my-projectViteの場合、以下のコマンドで作成します。
Vitenpm create vite@latest my-project -- --template react cd my-project -
TailwindCSSをインストール
npm install -D tailwindcss npx tailwindcss initViteの場合、以下のコマンドでインストールします。
Vitenpm create vite@latest my-project -- --template react cd my-project -
作成された
tailwind.config.jsファイルを開く -
contentプロパティのパスに、srcフォルダ内のファイルをすべてを参照するパスを指定する
tailwind.config.js/** @type {import('tailwindcss').Config} */ module.exports = { content: [ "./src/**/*.{js,jsx,ts,tsx}", ], theme: { extend: {}, }, plugins: [], }"./src/**/*.{js,jsx,ts,tsx}" -
index.cssに、tailwindの情報を追加するindex.css@tailwind base; @tailwind components; @tailwind utilities;
さいごに
通常のReactのcreate-react-appでの作成方法と、Viteを用いた方法では若干異なるところに注意しましょう!
参考