fervor [>]CODING & CURIOSITY
FERVOR LEARNING SYSTEMTUTORIALS
← React

React / 5 MIN READ

Tailwind Install

Installing Tailwind CSS in React projects

From the original Fervor library. Examples may use older package versions.

Installing Tailwind CSS into a React app is a breeze. Let’s walk through it step-by-step. Ready? Let’s get started!

Step 1: Create a React App

If you haven’t already created a React app, you can do so with create-react-app. Open your terminal and run:

npx create-react-app my-app
cd my-app

Replace my-app with whatever you want to name your project.

Step 2: Install Tailwind CSS

Now, we need to install Tailwind CSS and its dependencies. Run the following commands in your terminal:

npm install -D tailwindcss postcss autoprefixer

After this, initialize Tailwind CSS:

npx tailwindcss init -p

This will create two new files in your project:

  • tailwind.config.js – Tailwind’s configuration file.
  • postcss.config.js – Configuration for PostCSS, which is a tool for transforming CSS.

Step 3: Configure Tailwind

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

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

This tells Tailwind to scan all your JavaScript, TypeScript, and JSX files inside the src directory for class names.

Step 4: Import Tailwind in CSS

Create a new CSS file in the src directory, e.g., src/tailwind.css, and add the following lines to it:

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

Then, import this CSS file into your src/index.js file:

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import './tailwind.css'; // Add this line

ReactDOM.render(
  <React.StrictMode>
    <App />
  </React.StrictMode>,
  document.getElementById('root')
);

Step 5: Use Tailwind CSS Classes

Now, you can start using Tailwind CSS classes in your React components. For example, open src/App.js and modify it:

import React from 'react';

function App() {
  return (
    <div className="text-center">
      <header className="bg-blue-500 min-h-screen flex flex-col items-center justify-center text-white">
        <h1 className="text-4xl font-bold">
          Welcome to Tailwind CSS with React!
        </h1>
        <p className="mt-4 text-lg">
          This is an example of a React app using Tailwind CSS.
        </p>
      </header>
    </div>
  );
}

export default App;

Step 6: Start Your Development Server

Run your development server to see Tailwind CSS in action:

npm start

Open http://localhost:3000 to view it in the browser. You should see your React app styled with Tailwind CSS.

Recap

  1. Create a React app: npx create-react-app my-app.
  2. Install Tailwind CSS: npm install -D tailwindcss postcss autoprefixer.
  3. Initialize Tailwind: npx tailwindcss init -p.
  4. Configure Tailwind: Update tailwind.config.js.
  5. Create and import Tailwind CSS file: src/tailwind.css.
  6. Use Tailwind classes in your components.
  7. Start your development server: npm start.

And there you have it! You’ve successfully set up Tailwind CSS in your React app. Happy coding! If you have any questions or run into issues, feel free to ask.

Bonus Cool Stuff

Tailwind CSS is packed with a ton of cool features that make styling your web applications a breeze. Here are some of the highlights:

1. Responsive Design

Tailwind makes it super easy to create responsive designs with its mobile-first approach. You can apply styles for different screen sizes using simple prefixes:

<div className="bg-blue-500 p-4 md:bg-red-500 lg:bg-green-500">
  Resize the browser to see the background color change!
</div>

2. Hover, Focus, and Other States

Tailwind allows you to style elements based on their state, like hover, focus, active, etc., without writing any custom CSS:

<button className="bg-blue-500 text-white p-2 hover:bg-blue-700 focus:outline-none">
  Hover and Focus on Me!
</button>

3. Dark Mode Support

Tailwind has built-in support for dark mode. You can toggle dark mode and style your components accordingly:

<div className="bg-white dark:bg-gray-800 text-black dark:text-white p-4">
  This text changes color based on dark mode!
</div>

4. Grid Layouts

Tailwind simplifies the creation of complex grid layouts with its utility classes:

<div className="grid grid-cols-3 gap-4">
  <div className="bg-blue-500 p-4">1</div>
  <div className="bg-green-500 p-4">2</div>
  <div className="bg-red-500 p-4">3</div>
</div>

5. Flexbox Utilities

Flexbox is a powerful layout tool, and Tailwind provides numerous utilities to make it even easier to use:

<div className="flex justify-center items-center h-screen bg-gray-100">
  <div className="bg-white p-6 rounded-lg shadow-lg">
    Centered Content!
  </div>
</div>

6. Typography

Tailwind includes a comprehensive set of typography utilities to style your text:

<h1 className="text-4xl font-bold text-center">
  Big Bold Text
</h1>
<p className="mt-4 text-lg leading-relaxed">
  This is some descriptive text with a larger font size and relaxed line height.
</p>

7. Custom Animations

Creating custom animations is straightforward with Tailwind. You can define animations in your tailwind.config.js file and use them in your components:

// tailwind.config.js
module.exports = {
  theme: {
    extend: {
      animation: {
        'spin-slow': 'spin 3s linear infinite',
      },
    },
  },
}
<div className="w-16 h-16 bg-blue-500 animate-spin-slow">
  Spinning Box!
</div>

8. Forms

Tailwind provides utilities for styling forms, making it easy to create good-looking form elements:

<form className="space-y-4">
  <input type="text" className="border p-2 rounded w-full" placeholder="Your Name" />
  <input type="email" className="border p-2 rounded w-full" placeholder="Your Email" />
  <button type="submit" className="bg-blue-500 text-white p-2 rounded hover:bg-blue-700">
    Submit
  </button>
</form>

9. Shadows and Borders

Adding shadows and borders is a breeze with Tailwind’s utility classes:

<div className="p-4 bg-white rounded-lg shadow-lg border border-gray-200">
  This box has a shadow and a border!
</div>

10. Customizing Tailwind

You can customize Tailwind to fit your needs by extending the default theme in tailwind.config.js:

module.exports = {
  theme: {
    extend: {
      colors: {
        brand: '#1c64f2',
      },
      spacing: {
        '128': '32rem',
      },
    },
  },
}

And then use your custom classes in your components:

<div className="bg-brand p-128">
  Custom Tailwind Classes!
</div>

Recap

  1. Responsive Design: Easily create designs that adapt to different screen sizes.
  2. Hover, Focus States: Style elements based on user interactions.
  3. Dark Mode: Built-in support for dark mode styling.
  4. Grid Layouts: Simplify complex grid layouts.
  5. Flexbox Utilities: Powerful and easy-to-use flexbox classes.
  6. Typography: Comprehensive set of text styling utilities.
  7. Custom Animations: Define and use custom animations.
  8. Forms: Effortlessly style form elements.
  9. Shadows and Borders: Add depth and borders with ease.
  10. Customization: Extend and customize Tailwind to fit your project’s needs.

Tailwind CSS is incredibly versatile and can make your development process faster and more enjoyable. Have fun experimenting with these features!

Keep your curiosity going.Explore more React →
287 TUTORIALS · 22 TOPICSREADY