DEV Community

Cover image for TailwindCSS with ReactJS
Hasib Al Rashid
Hasib Al Rashid

Posted on

TailwindCSS with ReactJS

Setting up Tailwind with React

We all know setting up tailwind with React is somewhat a pain for the beginners. I personally got into this problem too. Today here we will be setting up tailwindcss from scratch.

Note: This tutorial can also be used in a project made before
Enter fullscreen mode Exit fullscreen mode

Without talking anymore lets jump right into the tutorial

First we will start our react project in the normal way

npx create-react-app project-name
Enter fullscreen mode Exit fullscreen mode

Right when you see the Happy Hacking Text in your screen, then you know that its done
image

Cd into the Folder and lets start editing and stuffs!

Open your code editor. (Mine is VSCode)

Now open your terminal and Install the following things.

With NPM:

npm i -D tailwindcss postcss autoprefixer postcss-cli
Enter fullscreen mode Exit fullscreen mode

With Yarn:

yarn add -D tailwindcss postcss autoprefixer postcss-cli
Enter fullscreen mode Exit fullscreen mode

Now Create output.css and tailwind.css Files in the src folder like in the structure given below.

src/
├── styles/
         ├── output.css
         └── tailwind.css
├── app.js
└── index.js
Enter fullscreen mode Exit fullscreen mode

Your folder structure should look like this
image

In the tailwind.css file, Paste this code.

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

Leave the Output.css file empty because it will be taken care of Postcss.

Now enter the following two commands in the Command Line:
(Works with both yarn and npm)

npx tailwindcss init --full
Enter fullscreen mode Exit fullscreen mode

and

npx tailwindcss init tailwindcss-config.js -p
Enter fullscreen mode Exit fullscreen mode

Now we have to edit the Following Lines in package.json

"scripts": {
    "start": "npm run watch:css && react-scripts start",
    "build": "npm run watch:css && react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject",
    "watch:css": "postcss src/styles/tailwind.css -o src/styles/output.css"
},
Enter fullscreen mode Exit fullscreen mode

Now in order to wrap up and actually code in tailwind we have to import the following line in out App.js file.

import './styles/output.css'
Enter fullscreen mode Exit fullscreen mode

Now start the project and play with Tailwind yourself!

npm run start
Enter fullscreen mode Exit fullscreen mode

or

yarn start
Enter fullscreen mode Exit fullscreen mode

We are all good. We can now use tailwind in our react project!

HOLD UP! But there is no Intellisense :(

First of all it is all bland and there is no suggestions on what will we do.

For that we need to add a Extension in VSCode for Tailwind

Simply search Tailwind in the extensions tab and install the first one :)
image

Restart your code editor and now we have awesome Tailwind Intellisense!
image

👑 Happy Hacking!

Follow me on Github at https://github.com/hasib-rashid

Discussion (0)