Skip to content
ClapelloNotes by Barath
ReactCh 10 · 2/6
Beginner4 min readUpdated

React / 53

Tailwind CSS: the utility-first idea

TL;DR

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"
  • flexdisplay: flex;
  • gap-3gap: 0.75rem;3 × 0.25rem
  • p-4padding: 1rem;4 × 0.25rem = 16px
  • rounded-xlborder-radius: 0.75rem;
  • font-boldfont-weight: 700;
Each utility class does one small job. You build a design by combining them in className, not by inventing a new class name in a stylesheet.

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

DishCard.jsx
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>
  );
}
DishCard.jsx
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:

ClassMeans
p-4padding: 1rem on all sides
px-4 / py-2padding left+right / top+bottom
mt-6margin-top: 1.5rem
text-lgfont size large
text-orange-700text colour, orange, shade 700
bg-orange-50background colour
hover:bg-orange-100that background, only on hover
md:flexdisplay: 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 styleTailwind classes
Hover, focus, media queriesNoYes (hover:, md:)
ValuesAnythingA shared scale, so designs stay consistent
CSS sizen/aEach class is written once and reused everywhere
Dark modeNodark:

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"
read as text

2 · tailwind

Finds class names

keeps p-4 and font-bold, ignores the rest
generates

3 · output css

Only what you used

.p-4 { padding: 1rem }.font-bold { … }
Tailwind reads your files as plain text, collects anything that looks like a class name it knows, and writes CSS for those only. Unused utilities never reach the browser.

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.