Unicorn Platform

World's best and simplest website builder powered by AI

Build your website
Friday, October 27, 2023

Quickly Install Tailwind CSS in React Projects


Tailwind CSS has rapidly become the most popular utility-first CSS framework for building fully customizable user interfaces without writing any custom CSS. With Tailwind's vast library of atomic CSS classes, you can rapidly develop consistent, beautiful designs for React projects.

This step-by-step guide will walk through installing Tailwind CSS into a React app made with Create React App or Vite. We'll cover configuring Tailwind, importing it properly, and using the utility classes to style React components. Whether you're creating websites, web apps, or mobile apps with React, Tailwind saves countless hours by eliminating handwritten CSS. Let's dive in and get Tailwind added to your next project!

What is Tailwind CSS?

Tailwind CSS provides developers with hundreds of reusable, low-level utility classes for styling elements directly in HTML without writing custom CSS. Instead of opinionated frameworks that encourage predefined designs, Tailwind equips you with building blocks like colors, spacing, typography, and layout utilities to craft completely custom UIs.

For example, Tailwind has classes like text-center, py-4, text-2xl, and bg-blue-500 that you simply apply directly in your markup. Everything is customizable - even the class names themselves. This utility-first methodology paired with React's composable components enables rapid iteration and consistent styling without fighting custom CSS.

Tailwind helps you style UIs intuitively while eliminating the need to context switch between HTML, CSS, and JavaScript. If you've struggled with slow CSS development, give Tailwind CSS a try!

Benefits of Using Tailwind CSS with React

There are many advantages to using Tailwind with React:

  • Speed up development by avoiding context switching between files. Style your components directly in JSX with utility classes.

  • Style React elements intuitively by composing classes to build any UI without leaving your component

  • Rapid iteration is enabled by applying classes and seeing changes instantly

  • Easily make sweeping changes by modifying the Tailwind theme configuration

  • Reusable style definitions with custom CSS utilities for your components

  • Styled Components interoperability integrates nicely with other libraries

  • Excellent documentation and community support to learn best practices

  • Accessibility features like dark mode work right out of the box

  • Highly customizable to match your brand and design system

Together, React and Tailwind provide a robust toolset for crafting beautiful UIs without fighting CSS - giving you back precious development time.

Installing Tailwind in a React Project

Let's go through the quick steps for setting up Tailwind CSS in a React app created with Create React App or Vite.

The key aspects are:

  • Installing Tailwind and its peer dependencies via NPM

  • Generating a tailwind.config.js file

  • Importing Tailwind in your CSS

We'll see examples for both CRA and Vite.

Installing Tailwind CSS with Create React App

Here is how to install Tailwind in a React project made with Create React App:

  1. Install Tailwind and its peer dependencies:
npm install -D tailwindcss postcss autoprefixer
  1. Generate your Tailwind config file:
npx tailwindcss init
  1. Import Tailwind in src/index.css:
@tailwind base;
@tailwind components;
@tailwind utilities;
  1. Configure PurgeCSS to remove unused CSS for production.

And that covers setting up Tailwind CSS in a React app made with CRA!

Installing Tailwind CSS with Vite

To install Tailwind in a React project created using Vite:

  1. Install the Tailwind NPM package:
npm install -D tailwindcss
  1. Point to your tailwind.config.js file in vite.config.js:
export default {
  plugins: [
  1. Import Tailwind in src/index.css:
@tailwind base; 
@tailwind components;
@tailwind utilities;

Vite will handle purging unused CSS for you automatically.

As you can see, getting started with Tailwind CSS is fast and easy with both Create React App and Vite. Now let's look at using Tailwind classes to style components.

Using Tailwind Classes in React Components

Once Tailwind is installed, you can start styling React elements by applying utility classes directly in JSX. Let's go over some examples.

Composing Utility Classes in JSX

With Tailwind's atomic CSS methodology, you construct UIs by stacking classes:

// Example Primary Button
function PrimaryButton() {
  return (
      className="bg-blue-500 text-white font-bold py-2 px-4 rounded hover:bg-blue-700">
      Click Me

Some best practices:

  • Style elements directly in JSX following the methodology
  • Use responsive prefixes like md: for breakpoints
  • Stack hover/focus variants like hover:

You can also extract common utilities into reusable components:

/* Extract button styles */
@layer components {
  .btn {
    @apply font-bold py-2 px-4 rounded;
  .btn-primary {
    @apply bg-blue-500 text-white;

Then reference them in JSX:

<button className="btn btn-primary hover:bg-blue-700">

This allows creating reusable Tailwind class names.

Styling Common Components

Tailwind provides all the low-level building blocks needed to style any component:

Buttons - Use bg- and text- for colors, font-bold for typography, rounded for shapes, py- and px- for padding, and hover: to add interactivity.

Navigation Menus - Build layouts with flex, style items with text- and spacing classes like px- and py-, and add hover:underline for links.

Forms - Set widths with w-, use my- and mt- for spacing, style inputs with border- and focus:, and align labels with text- utilities.

You can even create entirely custom color palettes and themes by modifying Tailwind's default configuration.

Overall, Tailwind provides all the tools needed to style any component without leaving your JSX.

Customizing the Tailwind CSS Configuration

One of Tailwind's strongest features is extensive customization. You can modify default values, add new utilities, create variants, customize breakpoints, and more.

Let's look at some of the most common ways to customize Tailwind for your project.

Changing Default Theme Values

To brand Tailwind with your design system's colors and fonts, modify the default values in tailwind.config.js:

// tailwind.config.js
module.exports = {

  theme: {
    extend: {
      colors: {
        brand: '#5b21b6',
      fontFamily: {
        sans: ['Graphik', 'sans-serif'],


This allows changing color palettes, font stacks, border radius values, breakpoints, and much more.

Adding New Custom Utilities

Register completely new class names by adding CSS under @layer directives:

// Custom background class 
@layer utilities {
  .bg-grid {
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg'%3E%3C/svg%3E");

You can also add custom variants like active and focus for utility classes. This allows introducing completely new class names tailored to your components.

Extending the default spacing and sizing scales is another great way to customize Tailwind CSS.


And that wraps up this guide on integrating Tailwind CSS into React apps! Here are some key takeaways:

  • Speed up development by eliminating handwritten CSS with utility classes
  • Faster iteration enabled by composing classes directly in JSX
  • Style React components intuitively without context switching
  • Highly customizable to match your brand design system
  • Works seamlessly with CRA and Vite starters
  • Extract reusable component classes from utilities
  • Add custom styles by extending Tailwind's defaults

Hopefully this provides a smooth experience setting up Tailwind CSS in your next React project. Check out the official docs for more examples and best practices. Tailwind can truly transform how you style UIs with React - give it a try today!