How to Use Tailwind CSS in React.js?

Tailwind CSS is a utility-first CSS framework that makes styling modern web applications faster and easier. It is widely used in React.js applications due to its flexibility, performance, and ease of use.

In this blog post, we will walk through the steps to install and use Tailwind CSS in a React.js project.

1. Setting Up a React.js Project

Before installing Tailwind CSS, make sure you have a React.js project set up. If you haven’t created one yet, you can do so using Vite or Create React App.

Using Vite (Recommended for Speed):

npx create-vite@latest my-tailwind-app --template react
cd my-tailwind-app
npm install

Using Create React App (CRA):

npx create-react-app my-tailwind-app
cd my-tailwind-app
npm install

2. Installing Tailwind CSS

To install Tailwind CSS, run the following command:

npm install -D tailwindcss postcss autoprefixer

Then, generate the Tailwind configuration files:

npx tailwindcss init -p

This command creates two files:

  • tailwind.config.js (Tailwind configuration file)

  • postcss.config.js (PostCSS configuration file)

3. Configuring Tailwind CSS

Open tailwind.config.js and update the content array to include your React files:

/** @type {import('tailwindcss').Config} */
export default {
  content: [
    "./index.html",
    "./src/**/*.{js,jsx,ts,tsx}"
  ],
  theme: {
    extend: {},
  },
  plugins: [],
};

This ensures Tailwind scans your files for class usage and optimizes the CSS.

4. Importing Tailwind CSS in Your Project

Next, import Tailwind’s base styles into your project by adding the following lines to src/index.css (or src/App.css in CRA):

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

This enables Tailwind's utility classes throughout your project.

5. Using Tailwind CSS in React Components

Now, you can start using Tailwind classes in your React components. Here’s an example of a simple button component:

const Button = () => {
  return (
    <button className="bg-blue-500 text-white px-4 py-2 rounded-md hover:bg-blue-700">
      Click Me
    </button>
  );
};

export default Button;

Explanation:

  • bg-blue-500: Sets the background color to blue.

  • text-white: Sets text color to white.

  • px-4 py-2: Adds padding on the x-axis (left & right) and y-axis (top & bottom).

  • rounded-md: Rounds the button corners.

  • hover:bg-blue-700: Changes background color on hover.

6. Responsive Design with Tailwind CSS

Tailwind makes it easy to create responsive designs. You can use responsive prefixes like sm:, md:, lg:, etc.

Example:

const Card = () => {
  return (
    <div className="p-6 max-w-sm bg-white rounded-lg shadow-md md:max-w-md lg:max-w-lg">
      <h2 className="text-xl font-semibold">Tailwind in React</h2>
      <p className="text-gray-600">This is a responsive card component.</p>
    </div>
  );
};

export default Card;

Breakpoints Used:

  • md:max-w-md: Sets max width for medium screens.

  • lg:max-w-lg: Sets max width for large screens.

7. Customizing Tailwind CSS (Optional)

You can extend Tailwind’s default theme in tailwind.config.js to add custom colors, spacing, or fonts.

Example:

export default {
  theme: {
    extend: {
      colors: {
        primary: "#4F46E5",
        secondary: "#F59E0B",
      },
    },
  },
};

Then, use them in your components:

<button className="bg-primary text-white px-4 py-2 rounded-md">Custom Button</button>

8. Optimizing Tailwind CSS for Production

Tailwind removes unused styles in production builds using PurgeCSS (automatically handled via the content config in tailwind.config.js). To generate a production build, run:

npm run build

Conclusion

Tailwind CSS makes styling React applications effortless with its utility-first approach. Here’s a quick recap: ✅ Install Tailwind in a React project. ✅ Configure Tailwind in tailwind.config.js. ✅ Import Tailwind styles in index.css. ✅ Use utility classes in React components. ✅ Customize Tailwind and optimize for production.

With Tailwind CSS, you can create beautiful and responsive React apps efficiently. 🚀