Setting Up Tailwind CSS In A React Project

11 min read

To set up Tailwind CSS in a React project, you will need to follow the following steps:

Step 1: Create a new React project

Use the following command in your terminal to create a new React project:

npx create-react-app my-app

Step 2: Install Tailwind CSS

Install Tailwind CSS and its dependencies using npm:

npm install tailwindcss postcss-cli autoprefixer

Step 3: Add Tailwind CSS configuration file

Create a new Tailwind configuration file by running the following command in your terminal:

npx tailwindcss init

This will create a new tailwind.config.js file in the root directory of your project. You can customize this file to suit your project’s needs.

Step 4: Create a PostCSS configuration file

Create a new postcss.config.js file in the root directory of your project and add the following code:

module.exports = {
  plugins: [
    require('tailwindcss'),
    require('autoprefixer'),
  ],
};

This file tells PostCSS to use Tailwind CSS and Autoprefixer.

Step 5: Import Tailwind CSS in your app

Open the index.css file in the src directory of your project and add the following code to import the Tailwind CSS styles:

@tailwind base;
@tailwind components;
@tailwind utilities;

Step 6: Import the index.css style sheet in your app

Open the index.js file in the src directory of your project and add the following code to import the index.css style sheet:

import './index.css';

Step 7: Start the server

Finally, start the development server using the following command:

npm start

Your React project should now be up and running with Tailwind CSS. You can start using the utility classes provided by Tailwind CSS in your JSX code.