React / 53
Tailwind CSS: the utility-first idea
Style elements with small single-purpose classes like p-4 and font-bold, right in your JSX. Tailwind ships CSS only for the classes you actually use.
One class, one job
Each Tailwind class is a utility: a class that sets one thing.
in your jsx
className="flex gap-3 p-4 rounded-xl font-bold"- flex
display: flex; - gap-3
gap: 0.75rem;3 × 0.25rem - p-4
padding: 1rem;4 × 0.25rem = 16px - rounded-xl
border-radius: 0.75rem; - font-bold
font-weight: 700;
You don't write the CSS; Tailwind already has a class for almost every property, on a fixed scale. Spacing goes up in steps of 0.25rem, so p-1 is 4px, p-4 is 16px and p-8 is 32px. Colours come in shades from 50 (lightest) to 950 (darkest): bg-orange-100, text-orange-700.
The same card, two ways
import "./DishCard.css";
// .dish-card { display: flex; gap: 0.75rem; padding: 1rem;
// border-radius: 0.75rem; background: #fff7ed; }
// .dish-card__name { font-weight: 700; }
function DishCard({ name, price }) {
return (
<div className="dish-card">
<p className="dish-card__name">{name}</p>
<p>₹{price}</p>
</div>
);
}function DishCard({ name, price }) {
return (
<div className="flex gap-3 rounded-xl bg-orange-50 p-4">
<p className="font-bold">{name}</p>
<p>₹{price}</p>
</div>
);
}Same result. The Tailwind version has no second file and no names to invent, and you can see the styling where you use it.
Reading a class name
Most classes follow the pattern what-how much, with an optional when in front:
| Class | Means |
|---|---|
p-4 | padding: 1rem on all sides |
px-4 / py-2 | padding left+right / top+bottom |
mt-6 | margin-top: 1.5rem |
text-lg | font size large |
text-orange-700 | text colour, orange, shade 700 |
bg-orange-50 | background colour |
hover:bg-orange-100 | that background, only on hover |
md:flex | display: flex, only from the md screen size up |
The hover: and md: parts are called variants. They get their own topic.
Isn't this just inline styles?
It looks similar, but it isn't:
Inline style | Tailwind classes | |
|---|---|---|
| Hover, focus, media queries | No | Yes (hover:, md:) |
| Values | Anything | A shared scale, so designs stay consistent |
| CSS size | n/a | Each class is written once and reused everywhere |
| Dark mode | No | dark: |
Only the CSS you use
Tailwind knows thousands of classes, but your final CSS file contains only the ones you wrote:
1 · your files
Card.jsx, App.jsx…
className="p-4 font-bold"2 · tailwind
Finds class names
3 · output css
Only what you used
.p-4 { padding: 1rem }.font-bold { … }In v4 this is automatic. Tailwind scans every file in your project except files in .gitignore, node_modules, images and other binary files, CSS files and lock files. It reads them as plain text: it doesn't run your code, it just looks for words that match class names.
That last detail matters a lot:
Don't
Building class names from pieces
function Badge({ color }) {
return <span className={`bg-${color}-100 text-${color}-700`}>New</span>;
}The text bg-green-100 never appears in any file, so Tailwind never generates it. The badge silently has no colour.
Do
Write every class in full
const colors = {
green: "bg-green-100 text-green-700",
red: "bg-red-100 text-red-700",
};
function Badge({ color }) {
return <span className={colors[color]}>New</span>;
}Every class appears complete somewhere, so Tailwind finds it.
Gotchas
Recall
Can you answer these?
0/4
Answer out loud (or in your head) before you tap. That's what makes it stick.
Saved on this device only. No login.
