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

React / 57

Tailwind trade-offs and readable class lists

TL;DR

The honest pros and cons of Tailwind, plus the habits that keep long class lists tidy, like components, clsx and cn() for conditional classes.

The honest trade-offs

GoodNot so good
No class names to invent, no second file to jump toLong class lists make JSX noisy
A shared scale keeps spacing and colour consistentYou have to learn the names (justify-between, tracking-tight…)
The CSS file stays small, because classes are reusedNeeds a build step
Styles live on the element, so deleting a component deletes its stylesClass names can't be built from pieces at runtime
Responsive, hover and dark mode right in the markupRepeated class lists if you don't make components

Most of the bad column is about readability. Here's how to fix it.

1 · Make a component, not a CSS class

If you copy the same class list onto five buttons, the fix in React is a component:

Button.jsx
export function Button({ children, ...props }) {
  return (
    <button className="rounded-lg bg-orange-600 px-4 py-2 font-semibold text-white hover:bg-orange-700" {...props}>
      {children}
    </button>
  );
}

Now the classes exist once, and every screen just writes <Button>. Tailwind does have @apply to bundle utilities into a CSS class, but leaning on it takes you back to naming things in a stylesheet. Save it for styling HTML you don't control, like a third-party widget.

2 · Conditional classes with clsx

Building class strings with template literals gets messy fast. clsx (npm install clsx) takes strings, conditions and objects, and drops anything falsy:

<li className={`rounded-lg p-3 ${isActive ? "bg-orange-100 font-semibold" : ""} ${isSoldOut ? "opacity-50" : ""}`}>
  Masala Dosa
</li>
import { clsx } from "clsx";
 
<li className={clsx("rounded-lg p-3", isActive && "bg-orange-100 font-semibold", isSoldOut && "opacity-50")}>
  Masala Dosa
</li>

3 · Let overrides win with cn()

Reusable components often accept a className so the parent can adjust them. But if Button has px-4 and the parent passes px-8, both classes end up on the element, and the winner is whichever rule comes later in Tailwind's CSS, not in your string. tailwind-merge fixes that by removing the earlier conflicting class. Most projects (and shadcn/ui) combine the two into a tiny helper called cn:

lib/cn.js
import { clsx } from "clsx";
import { twMerge } from "tailwind-merge";
 
export function cn(...inputs) {
  return twMerge(clsx(inputs));
}

clsx joins, cn merges

Try it

clsx(…) gives

rounded-lg font-semibold px-4 py-2 px-8

Conflict: two classes set the same property. Which wins depends on CSS order, not class order.

cn(…) gives

rounded-lg font-semibold py-2 px-8

Add to cart
clsx only joins the truthy strings, so px-4 and px-8 can both end up on the button. cn (clsx + tailwind-merge) keeps only the last one, so the parent's override really wins.
Button.jsx
import { cn } from "./lib/cn";
 
const variants = {
  primary: "bg-orange-600 text-white hover:bg-orange-700",
  ghost: "text-orange-700 hover:bg-orange-50",
};
 
export function Button({ variant = "primary", size = "md", className, ...props }) {
  return (
    <button
      className={cn(
        "rounded-lg font-semibold disabled:opacity-50",
        variants[variant],
        size === "sm" ? "px-3 py-1 text-sm" : "px-4 py-2",
        className
      )}
      {...props}
    />
  );
}
 
// <Button variant="ghost" className="px-8">View menu</Button>  →  px-8 wins

The variants object also keeps every class name complete, which is exactly what Tailwind's scanner needs. Libraries like class-variance-authority (cva) turn this pattern into an API, if you have many variants.

4 · Let the tools sort it

Install prettier-plugin-tailwindcss and every class list gets sorted into the same order (layout first, then spacing, colour, and so on) whenever you format. Diffs get smaller and lists become scannable. The Tailwind CSS IntelliSense editor extension shows the real CSS when you hover a class.

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.