Learn how to efficiently set up and use Tailwind CSS in your React applications for rapid, utility-first styling with best practices and practical examples.
Table of content
Styling modern React applications can be a challenge, but Tailwind CSS offers a utility-first framework that makes it fast, flexible, and responsive. In this article, you’ll learn step-by-step how to set up Tailwind CSS in a React project, why it’s gaining popularity among developers, and how to get the most out of its features.
If you haven’t already, create a new React project using Create React App:
npx create-react-app my-app
cd my-app
Install Tailwind CSS and its dependencies via npm:
npm install -D tailwindcss postcss autoprefixer
npx tailwindcss init -p
tailwind.config.js
and postcss.config.js
.Open tailwind.config.js
and set the content
property to include your React files:
module.exports = {
content: [
"./src/**/*.{js,jsx,ts,tsx}",
],
theme: {
extend: {},
},
plugins: [],
}
Replace the contents of src/index.css
with:
@tailwind base;
@tailwind components;
@tailwind utilities;
Tip: Import index.css
in src/index.js
if not already done.
You can now use Tailwind classes directly in your JSX. Here’s an example:
function App() {
return (
<div className="bg-gray-100 py-4 px-6 rounded-lg shadow-md">
<h1 className="text-2xl font-bold text-gray-800 mb-4">Hello Tailwind CSS!</h1>
<p className="text-gray-700 leading-relaxed">Welcome to the world of utility-first CSS!</p>
</div>
);
}
export default App;
Tailwind CSS is a game-changer for React developers who want speed, flexibility, and maintainability in styling. Setting it up is easy, and once you start using its utilities, you’ll never look back. For more tips on web development, stay tuned to fulldev.pl!