How To Use Tailwind CSS With React
Tailwind CSS is basically a utility-first CSS framework for rapidly building custom user interfaces. It is a highly customizable, low-level CSS framework that gives you all of the building blocks you need to build bespoke designs without any annoying opinionated styles you have to fight to override.
It's our go-to CSS framework of choice at FLOE.
1. Create your project
Start by creating a new Vite project if you don’t have one set up already. The most common approach is to use Create Vite.
npm create vite@latest tailwind-project -- --template react
cd tailwind-project
2. Install Tailwind CSS
Install tailwindcss and its peer dependencies via npm, and then run the init command to generate both tailwind.config.cjs
and postcss.config.cjs
.
npm install -D tailwindcss postcss autoprefixer
npx tailwindcss init -p
3. Configure your template paths
Add the paths to all of your template files in your tailwind.config.cjs
file.
/** @type {import('tailwindcss').Config} */
module.exports = {
content: ["./index.html", "./src/**/*.{js,ts,jsx,tsx}"],
theme: {
extend: {},
},
plugins: [],
};
4. Add the Tailwind directives to your CSS
Add the @tailwind directives for each of Tailwind’s layers to your ./src/index.css
file.
@tailwind base;
@tailwind components;
@tailwind utilities;
5. Start your build process
Run your build process with npm run dev
.
npm run dev
6. Start using Tailwind in your project
Start using Tailwind’s utility classes to style your content.
export default function App() {
return <h1 className="text-3xl font-bold underline">Hello world!</h1>;
}
VSC users should checkout the Tailwind CSS IntelliSense Plugin
Happy coding!