Tailwind CSS with React 🎯

beginner
18 min

Tailwind CSS with React 🎯

Welcome to this comprehensive guide on integrating Tailwind CSS with React! By the end of this tutorial, you'll be able to style your React projects effectively using the popular utility-first CSS framework, Tailwind CSS.

What is Tailwind CSS? 📝

Tailwind CSS is a utility-first CSS framework for rapidly building custom user interfaces. It provides low-level utility classes that allow you to style your components quickly and efficiently.

Why Tailwind CSS with React? 💡

Combining Tailwind CSS with React is a powerful way to create visually appealing and responsive user interfaces. It allows you to focus on building components rather than writing extensive CSS styles.

Setting Up Tailwind CSS with Create React App

Let's start by setting up a new React project using Create React App and installing Tailwind CSS:

bash
npx create-react-app tailwind-react-app cd tailwind-react-app npm install tailwindcss

After installation, generate the initial base styles for Tailwind CSS:

bash
npx tailwindcss init -p

Next, update the jest and babel configuration files to include Tailwind's PurgeCSS plugin to remove unused CSS:

bash
npm install @tailwindcss/postcss-purgecss autoprefixer postcss

Edit the postcss.config.js file and add the necessary plugins:

javascript
module.exports = { plugins: [ require('tailwindcss'), require('autoprefixer'), require('@tailwindcss/postcss-purgecss')({ content: ['src/**/*.js', 'src/**/*.jsx'], }), ], };

Now, create a tailwind.config.js file to configure Tailwind CSS settings:

javascript
module.exports = { purge: ['./src/**/*.{js,jsx}', './public/index.html'], darkMode: false, theme: { extend: {}, }, variants: {}, plugins: [], };

Finally, import the Tailwind CSS styles in your project's entry file:

javascript
// src/index.js import 'tailwindcss/tailwind.css';

Styling Components with Tailwind CSS

Now that Tailwind CSS is set up, let's create a simple component and style it using utility classes:

jsx
// src/components/Button.js import React from 'react'; const Button = ({ children, onClick }) => ( <button className="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded" onClick={onClick}> {children} </button> ); export default Button;

In this example, we've used several utility classes to style the button:

  • bg-blue-500: background color
  • hover:bg-blue-700: hover background color
  • text-white: text color
  • font-bold: font weight
  • py-2: padding-y
  • px-4: padding-x
  • rounded: border-radius

Using Tailwind CSS with React Hooks

Let's create a simple custom hook to handle the button's click event:

jsx
// src/hooks/useClickOutside.js import React, { useState } from 'react'; const useClickOutside = (ref, handler) => { useEffect( () => { const listener = event => { if (ref.current && !ref.current.contains(event.target)) { handler(event); } }; document.addEventListener('mousedown', listener); return () => { document.removeEventListener('mousedown', listener); }; }, [ref, handler] ); }; export default useClickOutside;

Now, modify the Button component to use the custom hook for handling click events outside the button:

jsx
// src/components/Button.js import React, { useRef, useEffect } from 'react'; import { useClickOutside } from './hooks/useClickOutside'; const Button = ({ children, onClick }) => { const ref = useRef(null); useClickOutside(ref, onClick); return ( <button className="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded" ref={ref} onClick={onClick}> {children} </button> ); }; export default Button;

Quiz 💡

Quick Quiz
Question 1 of 1

What is Tailwind CSS?

That's it for the introductory part of this guide! In the next lessons, we'll dive deeper into using Tailwind CSS with React, covering more advanced concepts and examples. Stay tuned! 🚀