DEV Community

Cover image for How to install Tailwind CSS with your react project
AlkemyHossain
AlkemyHossain

Posted on

How to install Tailwind CSS with your react project

Introduction: Tailwind CSS is a class-based UI CSS framework to design your websites, the way you want within shortage amount of time. I found that it is pretty tricky to embedded with react project.
Now we will see how we can install Tailwind CSS in your project by following some steps. So let’s get started…

I will add a link to the GitHub repository of the ReactJs & tailwind starter. You can clone and start without the hassle of setup.
Just make sure you have installed Node.Js 12+ and React CLI.

1. Create React App

We will start by creating React project By create-react-app and cd into the newly-created directory. If you have already done this you can skip it.

$ npx create-react-app my-project
$ cd my-project
Enter fullscreen mode Exit fullscreen mode

2. Install Tailwind

Install Tailwind via npm (node package manager)
You will need Node.js 12 or higher.

Install tailwind with other dependencies using

$ npm install tailwindcss@npm:@tailwindcss/postcss7-compat @tailwindcss/postcss7-compat postcss@^7 autoprefixer@^9
Enter fullscreen mode Exit fullscreen mode

As react doesn’t support postcss8 so we will install postcss7.

Install CRACO
CRACO is Create React App Configuration Override, an easy and comprehensible configuration layer for create-react-app. Craco is used for override PostCSS

$ npm install @craco/craco
Enter fullscreen mode Exit fullscreen mode

Once it’s installed, update your ‘scripts’ as below in package.json e

"scripts": {
   "start": "craco start",
   "build": "craco build",
   "test": "craco test",
   "eject": "react-scripts eject"
 },
Enter fullscreen mode Exit fullscreen mode

Next, create a ‘craco.config.js’ at the root of our project and add those line:

module.exports = {
   style: {
     postcss: {
       plugins: [
         require('tailwindcss'),
         require('autoprefixer'),
       ],
     },
   },
 }
Enter fullscreen mode Exit fullscreen mode

Generate Tailwind config file:
Now, Let’s generate your tailwind.config.js file:

npx tailwindcss init
Enter fullscreen mode Exit fullscreen mode

This command will create the tailwind config file in your root directory.
And replace this line to remove unused style in production:

module.exports = {
 purge: ['./src/**/*.{js,jsx,ts,tsx}', './public/index.html'],
 darkMode: false, // or 'media' or 'class'
 theme: {
   extend: {},
 },
 variants: {
   extend: {},
 },
 plugins: [],
}
Enter fullscreen mode Exit fullscreen mode

Installing is done! Let’s find out, how we can use tailwind classes in your project.

Use of Tailwind CSS

Include Tailwind in your CSS
Now open your index.css and replace it with importing tailwind base, components, utilities

@tailwind base;
@tailwind components;
@tailwind utilities;
Enter fullscreen mode Exit fullscreen mode

Include index.css.
Make sure you include or import index.css in your parent index.js file.

import './index.css';
Enter fullscreen mode Exit fullscreen mode

We are finished now. Create a component using Tailwind CSS and add it to your index.js for test.
Run your project using

`npm run start`
Enter fullscreen mode Exit fullscreen mode

I hope you find this article helpful. This is my very first writing experience in dev.to.

Happy Coding!

Top comments (2)

Collapse
 
braydentw profile image
Brayden W ⚡️

Thanks!

Collapse
 
csalkemy profile image
AlkemyHossain

Welcome! Buddy