Sub topic 3
Styling with Tailwind utilities
This chapter assumes you can read React and TypeScript and have written a few Tailwind classes before. It covers the parts of Tailwind that shadcn/ui relies on. Those are how class names become CSS, how conflicts between classes are decided, and how variants react to state. Later chapters build on this in cn, cva, and theming.
Why shadcn/ui components are full of class names
A shadcn/ui component has no stylesheet next to it. The styling is a string of Tailwind classes on the element. Radix primitives (covered in the previous chapters) provide behavior and ARIA attributes but ship no CSS. Tailwind supplies the visual layer.
Because you own the copied source, you will read and edit these class strings often. You need to understand how Tailwind processes them.
How a class name becomes CSS
Tailwind does not ship a large prebuilt stylesheet. At build time it scans your source files as plain text, looks for strings that match its utility patterns, and generates CSS only for the ones it finds.
The scanner does not run your code. It cannot evaluate JavaScript. This has a direct consequence for how you write dynamic classes.
Write complete class names
This looks reasonable but fails:
export function BrokenBadge({ color }: { color: "red" | "green" }) {
// Tailwind never sees "text-red-600" or "text-green-600" as whole strings.
return <span className={`text-${color}-600`}>Status</span>
}The scanner finds the fragment text- but no complete utility, so no CSS is generated and the text stays unstyled. The fix is to write every full class name somewhere in the file and select between them:
const colorClasses = {
red: "text-red-600 bg-red-50",
green: "text-green-600 bg-green-50",
} as const
export function Badge({ color }: { color: keyof typeof colorClasses }) {
return (
<span className={`rounded px-2 py-0.5 text-xs font-medium ${colorClasses[color]}`}>
Status
</span>
)
}This lookup-object shape is exactly what cva formalizes in a later chapter.
If the scanner does not see your component files at all, nothing in them gets styled. Check your Tailwind content configuration (v3) or source detection (v4) first when a copied component renders unstyled.
Variants: styling by condition
A variant is a prefix that applies a utility only under a condition. hover:bg-accent applies the background only on hover. Prefixes stack: dark:hover:bg-accent needs both conditions.
The variants you will see most in shadcn/ui source:
| Prefix | Applies when | Typical use |
|---|---|---|
| `hover:` | Pointer is over the element | Button background change |
| `focus-visible:` | Element has focus and the browser decides to show a focus ring, usually keyboard focus | Focus rings |
| `disabled:` | The element is disabled | Dimming, blocking pointer events |
| `md:`, `lg:` | Viewport is at least that width | Responsive layout |
| `dark:` | Dark mode is active | Dark color overrides |
| `data-[state=open]:` | Element has `data-state="open"` | Styling Radix open or closed states |
| `aria-invalid:` | Element has `aria-invalid="true"` | Error styling on inputs |
Prefer `focus-visible:` over `focus:`
focus: fires on mouse clicks too, so a button shows a ring every time someone clicks it. focus-visible: shows the ring mainly for keyboard users. Keyboard users need a visible focus indicator (see the accessibility chapter), so never remove the outline without replacing it. The class string in the shadcn/ui button does this with focus-visible:outline-none focus-visible:ring-2.
Styling Radix state with data attributes
Radix primitives expose their internal state as data-* attributes. A dialog overlay has data-state="open" or "closed". You do not need React state to style these. The attribute is already in the DOM, and a Tailwind variant reads it:
import * as Dialog from "@radix-ui/react-dialog"
export function FadingDialog() {
return (
<Dialog.Root>
<Dialog.Trigger className="rounded-md border px-3 py-2 text-sm hover:bg-gray-100">
Open
</Dialog.Trigger>
<Dialog.Portal>
<Dialog.Overlay className="fixed inset-0 bg-black/50 transition-opacity data-[state=closed]:opacity-0 data-[state=open]:opacity-100" />
<Dialog.Content className="fixed left-1/2 top-1/2 -translate-x-1/2 -translate-y-1/2 rounded-md bg-white p-6 shadow-lg">
<Dialog.Title className="text-lg font-semibold">Hello</Dialog.Title>
<Dialog.Description className="text-sm text-gray-600">
Styled by attributes, not state.
</Dialog.Description>
<Dialog.Close className="mt-4 text-sm underline">Close</Dialog.Close>
</Dialog.Content>
</Dialog.Portal>
</Dialog.Root>
)
}Radix keeps the overlay mounted until a CSS transition or animation on it finishes, so the closing fade plays. The bg-black/50 syntax is the opacity modifier. It sets the color to black at 50% opacity.
Conflicting utilities: the order trap
Suppose a button has px-4 and a caller passes px-2. Which wins?
export function Padded() {
return <div className="px-2 px-4">Which padding?</div>
}Many people expect the last class in the attribute to win. It does not. Both classes have equal specificity, so the one defined later in the generated stylesheet wins. Tailwind decides that order, not you. For px-2 and px-4, the result depends on Tailwind's internal ordering, so you cannot control it by rearranging the string.
This matters for shadcn/ui because components accept a className prop meant to override defaults. Without help, an override like px-2 may silently lose to the default px-4. The cn helper (next chapters) solves this with tailwind-merge, which understands Tailwind's conflict groups and keeps the last conflicting class.
Arbitrary values and the escape hatch
When the design scale has no matching value, use square brackets: w-[22rem], top-[3px], bg-[#1a1a2e], grid-cols-[1fr_2fr]. Underscores stand in for spaces. Arbitrary values are useful for one-offs. If you repeat the same one in several places, it belongs in your theme instead.
Semantic tokens in shadcn/ui classes
You will notice classes such as bg-primary, text-primary-foreground, and border-input in the copied source. These are not default Tailwind colors. They are names mapped to CSS variables in your theme, so a single variable change restyles every component and enables dark mode. The theming chapter covers that mapping. For now, treat them as ordinary utilities that resolve through your configuration.
Practical habits
- Keep class lists in a consistent order, such as layout, spacing, typography, color, state. The Prettier plugin for Tailwind sorts them automatically.
- Use
gap-*on flex and grid containers instead of margins on children. It composes better when you reorder or conditionally render children. - Use
size-*(or matchingh-*andw-*) for square elements such as icon buttons. - Put state styling next to the base style it modifies, for example
opacity-100 disabled:opacity-50. - Prefer variants driven by attributes (
aria-*,data-*) over extra React props when the DOM already carries the state.
Common errors
- Constructed class names.
bg-${x}-500generates nothing. Write full names. - Unscanned files. A component outside the configured source paths gets no CSS.
- Assuming string order resolves conflicts. It does not.
- Removing outlines.
outline-nonewithout afocus-visible:replacement harms keyboard users. - Mixing versions. Tailwind v3 and v4 configure themes and source detection differently. Check which one your project uses before copying setup snippets.
Exercise
A teammate writes <span className={bg-${tone}-100}> where tone is "blue" or "amber", and the background never appears in production. Explain why, and rewrite the code so it works.
Show answer
Tailwind scans source as plain text and cannot evaluate the template literal, so bg-blue-100 and bg-amber-100 never appear as complete strings and no CSS is generated. Use a lookup object with full class names, for example const tones = { blue: "bg-blue-100", amber: "bg-amber-100" } as const and then <span className={tones[tone]}>.
Exercise
A button's source contains px-4, and a caller passes className="px-2" after it in the same string. Why is the result unreliable, and which tool in the shadcn/ui setup addresses this?
Show answer
Both utilities have equal specificity, so the winner is whichever rule Tailwind emits later in the stylesheet, not the one later in the class attribute. The cn helper, which wraps tailwind-merge, resolves conflicting Tailwind utilities so the last one passed wins.
Exercise
Write the class that makes a Radix element fully transparent only while it has data-state="closed".
Show answer
data-[state=closed]:opacity-0