![]() |
VOOZH | about |
We will see how to set up Tailwind CSS in a React project using the Create React App. Tailwind CSS is a utility-first CSS framework that allows for rapid development of custom user interfaces. When combined with Create React App, it offers a flexible approach to build modern React applications.
Before proceeding, ensure you have the following installed:
Follow these steps to integrate Tailwind CSS into your React project:
If you haven't already installed Create React App, you can do so by running the following command:
npx create-react-app my-tailwind-appNote: Replace my-tailwind-app with the name of your project.Navigate into your project directory:
cd my-tailwind-appNow, install Tailwind CSS and its dependencies:
npm install -D tailwindcss@3 postcss autoprefixerCreate a tailwind.config.js file in the root directory of your project:
npx tailwindcss init -pThis command generates a tailwind.config.js file where you can customize Tailwind CSS according to your needs.
Create a postcss.config.js file in the root directory and add the following content:
module.exports = {
plugins: [
require('tailwindcss'),
require('autoprefixer'),
],
}
This configures PostCSS to use Tailwind CSS and autoprefixer.
Open the src/index.css file and import Tailwind CSS:
/* ./src/index.css */
@import "tailwindcss/base";
@import "tailwindcss/components";
@import "tailwindcss/utilities";
Add the paths to all of your template files in your tailwind.config.js file
/** @type {import('tailwindcss').Config} */
module.exports = {
content: [
"./src/**/*.{js,jsx,ts,tsx}",
],
theme: {
extend: {},
},
plugins: [],
}
Now, start your React app:
npm startYour app should now be up and running with Tailwind CSS integrated. You can start using Tailwind CSS utility classes in your React components.
Let's create a simple React component to demonstrate how to use Tailwind CSS classes:
Output: