はじめに
ReactでのTailwindCSSの適用方法で少しつまずいたので、セットアップ方法をまとめます。
一般的な方法、Viteを用いた方法をそれぞれ記載しています。
セットアップ方法
-
プロジェクト作成
npx create-react-app my-project cd my-project
Viteの場合、以下のコマンドで作成します。
Vitenpm create vite@latest my-project -- --template react cd my-project
-
TailwindCSSをインストール
npm install -D tailwindcss npx tailwindcss init
Viteの場合、以下のコマンドでインストールします。
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を用いた方法では若干異なるところに注意しましょう!
参考