Home > Web Front-end > JS Tutorial > Set Up Tailwind CSS in a React JS Project

Set Up Tailwind CSS in a React JS Project

Barbara Streisand
Release: 2024-12-16 13:27:18
Original
326 people have browsed it

Set Up Tailwind CSS in a React JS Project

If you don’t have a React app already, create one:

npx create-react-app my-app
cd my-app
Copy after login
  1. Install Tailwind CSS Run the following command to install Tailwind CSS and its dependencies:
npm install -D tailwindcss postcss autoprefixer
Copy after login

Then initialize Tailwind CSS:

npx tailwindcss init

Copy after login

This will create a tailwind.config.js file in your project.

  1. Configure Tailwind Edit the tailwind.config.js file to configure the content paths. Replace the content key with the following:
/** @type {import('tailwindcss').Config} */
module.exports = {
  content: [
    "./src/**/*.{js,jsx,ts,tsx}", // Scan these files for Tailwind classes
  ],
  theme: {
    extend: {},
  },
  plugins: [],
};

Copy after login
  1. Add Tailwind Directives to CSS In the src folder, locate or create a file called index.css. Add the following Tailwind directives:
@tailwind base;
@tailwind components;
@tailwind utilities;

Copy after login
  1. Import CSS in React Ensure index.css is imported into your project. In the src/index.js file, you should have:
import './index.css';

Copy after login
  1. Start the Development Server Run your React app to see Tailwind CSS in action:
npm start

Copy after login

The above is the detailed content of Set Up Tailwind CSS in a React JS Project. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template