Tailwind CSS 4 in React: Utility Classes, Responsive Prefixes, Hover and Dark Mode
Style a React task card with Tailwind CSS 4: install the Vite plugin, read utility classes, go from one column to two with md:, and add hover, focus and dark mode with one prefix each.
What you will learn
- Set up Tailwind CSS 4 in a Vite React app in three steps
- Read utility classes like mt-4, px-4 and bg-indigo-600 and know what they do
- Make a layout responsive with md: and other breakpoint prefixes
- Add hover, focus and dark mode styles with prefixes
Before you start
- React components and props
- Basic HTML and CSS
You have a task card on a page: a title, a short text and a button. It needs to look good. In this article you style it with Tailwind CSS 4 in a React app, with spacing, colour, hover, a responsive grid and dark mode.
Carve every piece, or snap bricks
With plain CSS, styling is like carving. For every piece you invent a name, like plain-card. Then you jump to another file. Then you carve the rules, one new shape every time.
Tailwind is more like LEGO. You get small ready-made bricks, and each brick does one job:
bg-white: a white background brick.p-6: a padding brick, the space inside the card.rounded-2xl: a rounded-corner brick.shadow-lg: a soft shadow brick.
Small classes, snapped together right in your markup. That is what utility first means.
Setup: three steps, no config file
Step one, install Tailwind and its Vite plugin:
npm install tailwindcss @tailwindcss/vite
Step two, add the plugin to vite.config.ts, next to React:
import tailwindcss from '@tailwindcss/vite'
import react from '@vitejs/plugin-react'
import { defineConfig } from 'vite'
export default defineConfig({
plugins: [react(), tailwindcss()],
})
Step three, one line in src/index.css:
@import "tailwindcss";
That is all. There is no tailwind.config.js. Tailwind 4 reads your files and builds only the classes you use, so the final CSS stays small.
Plain CSS or utility classes: the same button
Here is the button in plain CSS, from PlainCard.css:
.plain-card button {
margin-top: 16px;
padding: 8px 16px;
border-radius: 8px;
background: #4f46e5;
color: #fff;
font-weight: 600;
}
.plain-card button:hover {
background: #4338ca;
}
Margin, padding, radius, background, colour, font weight, then a second rule for hover. That is a lot of lines for one button. Here is the Tailwind version, written on the button itself:
<button className="mt-4 w-full rounded-lg bg-indigo-600
px-4 py-2 font-semibold text-white hover:bg-indigo-700
focus:outline-none focus:ring-4 focus:ring-indigo-300
sm:w-auto">
Read it piece by piece:
| Class | Meaning |
|---|---|
mt-4 | Margin on top. The number times 4px: 4 x 4 = 16px |
px-4 py-2 | Padding: 16px across, 8px down |
rounded-lg | Rounded corners |
bg-indigo-600 | Indigo background |
hover:bg-indigo-700 | Darker indigo while the mouse is on it |
No new name and no second file. The style sits right on the element.
The card: classes on each tag
Here is the full card component, Card.tsx:
type CardProps = { title: string; text: string }
export default function Card({ title, text }: CardProps) {
return (
<div className="rounded-2xl bg-white p-6 shadow-lg
dark:bg-slate-800">
<h2 className="text-2xl font-bold text-slate-900
dark:text-white">{title}</h2>
<p className="mt-2 text-slate-600
dark:text-slate-300">{text}</p>
<button className="mt-4 w-full rounded-lg bg-indigo-600
px-4 py-2 font-semibold text-white hover:bg-indigo-700
focus:outline-none focus:ring-4 focus:ring-indigo-300
sm:w-auto">
Mark done
</button>
</div>
)
}
The outer div has rounded-2xl shadow-lg bg-white p-6, which makes the white card with soft corners. The heading has text-2xl font-bold, so big, bold text. The button has the margin, padding and indigo classes you just read. Run it with npm run dev.
Do not worry about memorising class names. AI can write them for you. What you need is to be able to read them.
Responsive: a prefix means "from this width up"
The page lays out the cards in App.tsx:
import Card from './Card'
export default function App() {
return (
<main className="min-h-screen bg-slate-100 p-6
dark:bg-slate-900">
<h1 className="mb-6 text-3xl font-bold text-slate-900
dark:text-white">Task cards</h1>
<div className="grid gap-6 md:grid-cols-2">
<Card title="Write report" text="Summarise the quarter." />
<Card title="Email Jack" text="Send the budget." />
</div>
</main>
)
}
Look at grid gap-6 md:grid-cols-2. On a phone, 375px wide, the md: rule is ignored, so you get one column. On an 800px screen, the md: rule kicks in and you get two columns.


Same code, no media query. The prefixes start at fixed widths:
sm:from 640pxmd:from 768pxlg:from 1024px
So think mobile first. Write the small screen, then add md: and lg: when there is more room. The button does the same thing: w-full on a phone, sm:w-auto from 640px up.
Hover and focus are just prefixes
hover:bg-indigo-700 turns the button darker while the mouse is on it. Focus works the same way: focus:ring-4 focus:ring-indigo-300 draws a ring around the button when the keyboard reaches it, which matters for accessibility.

You write the state before the class, and Tailwind applies it only at that moment.
Dark mode: one extra class
For dark mode, each tag gets one more class: dark:bg-slate-800 on the card, dark:text-white on the heading, dark:text-slate-300 on the text.

There is no toggle code and no extra file. By default, Tailwind's dark: follows the system setting. If you want a button that switches the theme, and colours that come from your design instead of the Tailwind palette, read design tokens to a Tailwind theme.
Common mistakes
- Looking for
tailwind.config.js. Tailwind 4 with the Vite plugin does not need one. The@import "tailwindcss";line is the setup. - Thinking
md:means "only on medium screens". It means from 768px up. Write the phone style without a prefix. - Removing the focus ring because it looks odd. Keyboard users need it to know where they are.
- Forgetting the dark text classes.
dark:bg-slate-800alone gives you dark text on a dark card.
Recap
| Need | Tailwind |
|---|---|
| Spacing | p-6, mt-4, px-4 py-2 (number x 4px) |
| Colour | bg-indigo-600, text-slate-600 |
| Screen size | sm: md: lg:, from that width up |
| States | hover: focus: |
| Dark mode | dark:, follows the system by default |
These are the foundations: utilities, prefixes and how to read them. With AI you can generate the long class lists, and you will know what they mean.
StudyEasy AI Community
Learn AI engineering, share what you build, and connect with other learners.
Free resources: lesson files, notebooks and code
Ask questions and join engineering discussions
Meet real people learning and building with AI
Related course on Udemy
The Python Architect Masterclass: Full Stack to AI Engineer
Python, Django, FastAPI, React, Docker, AWS, LLMs, RAG, agents and MCP, from first steps to AI engineer.View on Udemy ↗: The Python Architect Masterclass: Full Stack to AI Engineer (paid course, opens in a new tab)Related posts
One Request, Two Answers: the Same Post as an HTML Page and as JSON
Ask a Django blog for the same post twice: once as a 12,731-byte HTML page built by a template, once as 2,976 bytes of JSON built by a serializer. Compare the headers, see who builds the page, and why the server still owns every write.
A Blog Design from a Prompt: Downloading Figma Make Code and Reading App.tsx and BlogCard.tsx
Make a blog design from a prompt in Figma Make, download src.zip, unzip it with Python's zipfile module, and read the two files that matter: App.tsx with its selectedPost state and BlogCard.tsx with its props.
How a Web Page Works: HTML Tags, Elements and Attributes
A web page is a text file full of labelled pieces. Learn tags, attributes and nesting with three tiny pages, and see what breaks when an attribute or a closing tag is wrong.
Semantic HTML: header, nav, main, aside and footer as Page Landmarks
The same page written as div soup and with semantic tags looks identical, but only one gives screen readers and search engines landmarks and an outline. Plus tables, div, span and the one-main rule.
