Setup Tailwind CSS with React
Easy setup using create-react-app

I'm a full stack developer based in Australia. I write articles about Node.js, React, JavaScript & Web Development to make your learning journey easier ๐
Search for a command to run...
Easy setup using create-react-app

I'm a full stack developer based in Australia. I write articles about Node.js, React, JavaScript & Web Development to make your learning journey easier ๐
No comments yet. Be the first to comment.
Introduction Website performance is an important aspect of web development. It consists of mainly two things: The speed of your website How usable is your website for visitors? Importance of website performance Website performance is very importa...

There are plenty of native web APIs that have a variety of uses. Here are 5 useful APIs that I use quite often in my web development projects. Clipboard API The clipboard API is a simple method to copy text to a user's clipboard. This can be used in ...

Hey All ๐ In this article, we'll be covering the Fetch API in JavaScript how you can use it to make HTTP requests. What is the Fetch API? The fetch() method provides a simple way to make HTTP requests in JavaScript. It is natively in the browser, me...

Hey All ๐ In this article, we'll be covering 4 mistakes commonly made by people while learning to code that will seriously impact their coding journey. #1 - Copying and Pasting The first mistake I'll talk about is copying and pasting. We've all don...

Introduction There are plenty of units in CSS. From px to vh, there is an abundance of units all with their distinctive use cases. Considering the amount of options, its perfectly reasonably to be confused as to which unit you should use and where. T...

In this article, I'll be showing you how to setup Tailwind CSS with React so you can build highly customizable UIs in React with a utility based approach.
Firstly, we will use create-react-app to setup a react app. To do this, run the following command in your terminal substituting in the name of your application:
npx create-react-app appName
Obviously, you will need npm installed. You can install it by installing node.js
Next, we will install tailwind and its peer-dependencies in order to work with our react app.
npm i -D tailwindcss@npm:@tailwindcss/postcss7-compat postcss@^7 autoprefixer@^9
Remember the
-Dflag since these are dev dependencies
To be able to configure Tailwind in with create-react-app, we will need another package called craco.
npm install @craco/craco
After installing craco, we will update our scripts in our package.json from the default react-scripts to craco. The default scripts should go from this:
// package.json
{
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
}
to this:
// package.json
{
"scripts": {
"start": "craco start",
"build": "craco build",
"test": "craco test",
"eject": "react-scripts eject"
},
}
Note: don't change the eject script to craco
Next, we will create a craco.config.js file at the root of our project so we can add previously installed PostCSS plugins:
// craco.config.js
module.exports = {
style: {
postcss: {
plugins: [
require('tailwindcss'),
require('autoprefixer'),
],
},
},
}
Next, we will need to generate a tailwind.config.js file using tailwindcss-cli. To do this we can run the following command in the terminal:
npx tailwindcss-cli@latest init
This will create a base tailwind.config.js file that looks like this:
// tailwind.config.js
module.exports = {
purge: [],
darkMode: false, // or 'media' or 'class'
theme: {
extend: {},
},
variants: {
extend: {},
},
plugins: [],
}
We can then update the purge property so that unused styles are removed in production:
purge: ["./src/**/*.{js,jsx,ts,tsx}", "./public/index.html"],
This file is used to customize the theme, which allows you to change sizes, colors and more about your styling. You can learn more about customizing the theme and variants in Tailwind's documentation.
In our index.css in the ./src directory that is automatically generated by Create React App, we can use the @tailwind directive to include the base, components and utilities styles.
Remember to remove the original contents in this file.
The ./src/index.css file should now look like this:
/* src/index.css */
@tailwind base;
@tailwind components;
@tailwind utilities;
Ensure that the index.css file is imported in the index.js file and you are ready to begin using Tailwind CSS in your application. You can now run npm start and you're ready to go!
Remember to check out the official documentation of Tailwind CSS if you're learning to see all styles you can use in your application as well as how to customize the theme and variants, e.t.c
๐ Thanks for reading this article!
If you like what I do and would love to see more related content, follow me on my other social platforms:
GitHub: Blake-K-Yeboah
LinkedIn: Blake-K-Yeboah
You can also show your support by buying me a coffee ๐