Facebook Pixel

How to Set Up Tailwind CSS in a React Project

Setting up Tailwind in a React project is quick. Here is the step-by-step setup and the common mistakes.

How to Set Up Tailwind CSS in a React Project

Setting up Tailwind in a React project is quick. Here is the step-by-step setup and the common mistakes.

Step 1: Install Tailwind

Install Tailwind and its peer dependencies with npm. Use the official Tailwind packages, not a random CDN, for a real project.

Step 2: Generate the Config

Run the Tailwind init command to generate a configuration file. This file lets you customize the theme, colors, and which files Tailwind scans.

Step 3: Configure the Content Paths

In the config, list the files Tailwind should scan for class names, like your src folder and HTML files. This is critical: Tailwind only generates CSS for classes it finds in these files.

Step 4: Add the Tailwind Directives

In your main CSS file, add the three Tailwind directives: base, components, and utilities. This injects Tailwind's styles into your project.

Step 5: Import the CSS

Import the CSS file into your React entry point so Tailwind's styles are bundled and applied.

Step 6: Use Utility Classes

Start using utility classes in your components, like className="flex p-4 text-center". You should see the styling apply immediately.

The Common Mistakes

Forgetting to configure the content paths, so Tailwind generates no CSS. Or using a CDN in production, which ships the entire framework instead of only your used classes.

The Takeaway

Install Tailwind, generate the config, configure the content paths, add the directives to your CSS, import it, and use utility classes. The biggest mistake is forgetting the content paths, which produces no CSS.

Install Tailwind with npm, generate the config, configure the content paths to your source files, add the three Tailwind directives to your CSS, import that CSS in your entry point, and start using utility classes in components.

Usually because you forgot to configure the content paths in the Tailwind config. Tailwind only generates CSS for classes it finds in the files you list, so if the paths are wrong or missing, no CSS is produced.

Three directives you add to your main CSS file: @tailwind base, @tailwind components, and @tailwind utilities. They inject Tailwind's base styles, component classes, and utility classes into your project.

No. The CDN ships the entire framework, including classes you do not use, which defeats one of Tailwind's main benefits. Install Tailwind properly so it scans your markup and ships only the classes you use.

In the generated config file, extend the theme with your own colors, fonts, spacing, and more. Customization lives in one place, which keeps your design tokens consistent across the app.

Ready to master React completely?

Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.

Please Login.
Please Login.
Please Login.
Please Login.