Sub topic 3
Styling with Tailwind utilities
This chapter assumes you know React function components and props, basic TypeScript, and how to run a Vite or Next.js project with npm. It covers the part of Tailwind CSS that shadcn/ui depends on. You will see how utility classes work, how state variants and responsive prefixes apply, and where utility styling causes problems.
The working model
Tailwind CSS is a utility-first framework. Each class sets one CSS declaration, or a small group of them. Instead of writing a .card rule in a stylesheet, you compose the look from classes in the markup.
export function Badge({ children }: { children: React.ReactNode }) {
return (
<span className="inline-flex items-center rounded-md bg-blue-600 px-2 py-1 text-xs font-medium text-white">
{children}
</span>
)
}Tailwind scans your source files for class names and generates CSS only for the ones it finds. This has a consequence for components. Tailwind reads class names as plain text, so it cannot see a name built at runtime.
// Broken: Tailwind never sees "bg-red-600" as a complete string.
const color = "red"
const bad = `bg-${color}-600`
// Works: every full class name appears in the source.
const good = isError ? "bg-red-600" : "bg-blue-600"Why shadcn/ui relies on this
A headless component, such as a Radix primitive, ships behavior and accessibility but no appearance. shadcn/ui supplies the appearance as Tailwind classes written directly in the component file you copy into your project. Styling a component therefore means editing a string of classes. No theme API or override layer is involved.
State and responsive variants
A variant prefix applies a utility only under a condition. The prefix comes first, followed by a colon.
| Prefix | Applies when | Example |
|---|---|---|
| `hover:` | The pointer is over the element | `hover:bg-accent` |
| `focus-visible:` | Keyboard focus is on the element | `focus-visible:ring-2` |
| `disabled:` | The element is disabled | `disabled:opacity-50` |
| `md:` | The viewport is at least 768px wide | `md:flex-row` |
| `dark:` | Dark mode is active | `dark:bg-slate-900` |
Radix primitives expose their internal state as data attributes, and Tailwind can style against them. An open dialog has data-state="open", so a class such as data-[state=open]:bg-accent styles it with no extra JavaScript.
Common errors
- Dynamic class names. Build whole class strings and choose between them, as shown above.
- Conflicting utilities. If a string contains both
px-2andpx-4, the winner depends on the order of rules in the generated CSS, not on the order in your string. The next chapter shows howcnresolves this. - Removed focus styles. Writing
outline-nonewithout a replacement such asfocus-visible:ring-2leaves keyboard users with no visible focus. - Long class strings. Repeated groups of classes belong in a component, not in a shared CSS class.
Exercise
A teammate writes className={text-${size}} where size is "sm" or "lg", and the text never changes size in production. What is the cause, and how do you fix it?
Show answer
Tailwind scans source files for complete class names and cannot see text-sm or text-lg in a template string built at runtime, so it never generates that CSS. Write the full names in the source, for example size === "sm" ? "text-sm" : "text-lg", or use a lookup object whose values are full class strings.