Setup Tailwind CSS in React
In this post we will see how to install tailwind.css in react
Installation
- Install tailwind with the below command
yarn add tailwindcss postcss-cli autoprefixer -D
- Create a PostCSS configuration file in your root directory manually or you can use the command:
touch postcss.config.js
- paste the below code in the config file
const tailwindcss = require('tailwindcss');
module.exports = {
plugins: [
tailwindcss('./tailwind.js'),
require('autoprefixer')
],
};
- Create 2 .css files in
src/assets/
name them astailwind.css
andmain.css
- Add below code to
tailwind.css
file
@import "tailwindcss/base";
@import "tailwindcss/components";
@import "tailwindcss/utilities";
- Go to
package.json
and change thescripts
as below
"scripts": {
"start": "npm run watch:css && react-scripts start",
"build": "npm run build:css && react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"build:css": "postcss src/assets/tailwind.css -o src/assets/main.css",
"watch:css": "postcss src/assets/tailwind.css -o src/assets/main.css"
},
- Go to
index.js
and import themain.css
and deleteindex.css
import
import './assets/main.css'
That's it. whenever you run the build or start comand the tailwind css will be compiled and added to main.css
file. Now you can use tailwind.css.