Tailwind CSS is a utility first CSS framework that allows developers to design custom web components without switching to a CSS file. In this tutorial, you will learn how to install Tailwind CSS in React and how you can use it to build a simple React page.

Why Use Tailwind CSS?

There are already a lot of CSS frameworks that simplify how developers design web pages. So why should you use Tailwind CSS?

CSS frameworks such as Bootstrap and Foundation are opinionated frameworks, meaning they provide developers with pre-defined components that have default styles. This limits both customization and creativity, and you end up with websites that look rather generic.

Tailwind CSS, however, is a utility-first framework that gives you the creative control to create dynamic components. And unlike Bootstrap, you can easily customize designs as you please.

Another advantage of using Tailwind CSS is that you end up with a small CSS bundle size since it removes all of the unused CSS during the build process (which is different from Bootstrap, since it includes all CSS files in the build).

Learn more about the differences between Tailwind CSS and Bootstrap from our article on the topic.

Disadvantages of Using Tailwind CSS

Tailwind CSS has a steep learning curve even for experienced developers. It takes some time to learn how to fully use the utility classes, and you might need to refer to the documentation often. However, after getting familiar with the classes, you'll find it easier and speedier compared to plain CSS.

Most developers like to follow the separation of concerns principle such that the CSS and HTML files are written in different files. With Tailwind CSS, you write the CSS directly in the HTML markup—a drawback for some.

Even with these disadvantages, Tailwind CSS is a framework that you should seriously consider if you're already comfortable with CSS and want to build designs faster.

Getting Started: Create a React Project

Run the following command in the terminal to scaffold a React application using create-react-app.

        npx create-react-app react-tailwind
    

create-react-app provides an easy way to create a React app without configuring build tools like webpack, babel, or linters. This means you end up with a working React environment within minutes.

The above command creates a new folder named react-tailwind. Navigate to the folder and open it using your preferred text editor.

        cd react-tailwind
    

Next, install Tailwind CSS and configure it to work with the React application.

Use Tailwind CSS in React

Install Tailwind CSS and its dependencies with this command:

        npm install tailwindcss postcss autoprefixer
    

PostCSS uses JavaScript plugins to make CSS compatible with most browsers. It checks the browser the application is running in and determines the polyfills needed to make your CSS work seamlessly. Autoprefixer is a PostCSS plugin that uses values from caniuse.com to automatically add vendor prefixes to CSS rules.

Initialize Tailwind CSS

Run the tailwind init command to generate Tailwind CSS default configuration files.

        npx tailwindcss init
    

This creates tailwind.config.js in the root folder which stores all of Tailwind’s configuration files and contains the following:

        module.exports = {
 content: [],
 theme: {
   extend: {},
 },
 plugins: [],
}

Configure Template Paths

You need to tell Tailwind CSS the files it should check to see what CSS classes are being used. This allows Tailwind to identify and remove the unused classes and therefore reduces the size of the CSS generated.

In tailwind.config.js, add the template paths under the content key.

        module.exports = {
 content: [
   "./src/**/*.{js,jsx,ts,tsx}",
 ],
 theme: {
   extend: {},
 },
 plugins: [],
}

Inject Tailwind CSS Into React

The next step is to include Tailwind CSS in the application using @tailwind directives.

Delete everything in index.css and add the following to import the base styles, components, and utilities.

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

Finally, make sure index.css is imported in index.js and Tailwind CSS will be ready for use.

Using Tailwind CSS to Style a React Component

You will create the simple web page below and style it using Tailwind’s utility classes.

Screenshot of web page showing navigation menu and hero section

This page contains two main sections: a navigation bar, and the hero section (which has a heading and a button).

To illustrate how Tailwind CSS makes writing CSS easier, try styling the web page using plain CSS and Tailwind CSS.

Get started by modifying the App.js in the src folder to remove the unnecessary code.

        import './App.css'
function App() {
 return (
   <div className='app'>
   </div>
 );
}
export default App;

Next, add the web page content to App.js.

        import "./App.css";
function App() {
 return (
   <div className="wrapper">
     <nav>
       <div className="logo">Tailwind CSS</div>
       <ul>
         <li>Install</li>
         <li>Docs</li>
       </ul>
     </nav>
     <div className="hero">
       <h1 className="header">
         Tailwind CSS makes styling React components easier!
       </h1>
       <button className="btn">Get Started</button>
     </div>
   </div>
 );
}

To use plain CSS, add the CSS to App.css.

        nav {
 display: flex;
 justify-content: space-between;
 padding: 16px 36px;
 color: #000;
 box-shadow: 0px 2px 5px 0px rgba(168, 168, 168, 0.75);
}
.logo {
 font-size: 18px;
 font-weight: bold;
}
ul {
 list-style: none;
 display: inline-flex;
}
ul li {
 margin-left: 16px;
 cursor: pointer;
}
.hero {
 display: flex;
 flex-direction: column;
 align-items: center;
 margin-top: 64px;
}
h1 {
 font-size: 36px;
 text-align: center;
}
.btn {
 background-color: #000000;
 color: #fff;
 padding: 10px;
 width: fit-content;
 margin-top: 36px;
}

With Tailwind CSS, you don’t need to write the CSS rules for each class. Instead, you use utility classes. These are classes scoped to a single CSS property. For instance, if you want to create a button with a black background and white text color, you need to use the bg-black and text-white utility classes.

App.js should look like this.

        function App() {
 return (
   <div>
     <Navbar />
     <div className="flex flex-col items-center mt-16">
       <h1 className="text-4xl text-center">
         Tailwind CSS makes styling React components easier!
       </h1>
       <button className="bg-black text-white p-2.5 w-fit mt-9 ">
         Get Started
       </button>
     </div>
   </div>
 );
}

You don’t need to import App.css since the styles generated by Tailwind CSS are stored in index.css which you imported in index.js earlier.

Compared to plain CSS, this approach results in less code that is easy to understand.

Code in Style With Tailwind CSS

In this article, you learned about Tailwind CSS, its strengths, disadvantages, and how you can use its utility classes in React applications. Apart from classes, Tailwind CSS also offers other additional features including the ability to create responsive layouts and reusable components.

But, as we mentioned earlier, Tailwind is far from the only CSS framework on the market. Which will you use for your next project?