Sub topic 1
Utility-First Styling and Why Long Class Lists Appear
Prerequisites
Assume you know basic HTML and CSS, Tailwind utilities such as flex, px-4, and bg-blue-500, and the basics of a component-based framework such as React or Vue.
Utility-First Styling
Tailwind CSS is utility-first: each class applies a small styling rule. For example, px-4 adds horizontal padding, while font-semibold changes the font weight. Combining utilities keeps styles close to the markup and makes small changes easy.
A button may therefore look like this:
<button class="rounded-md bg-blue-600 px-4 py-2 font-semibold text-white hover:bg-blue-700">
Save
</button>Long class lists are normal when an element has many visual decisions. The problem starts when the same list appears in many places.
Spotting Real Duplication
Repeated classes do not always mean you need a CSS abstraction. First ask whether the elements share the same behavior and meaning.
Three action buttons with the same shape and color are probably one reusable button pattern:
<button class="rounded-md bg-blue-600 px-4 py-2 font-semibold text-white hover:bg-blue-700">Save</button>
<button class="rounded-md bg-blue-600 px-4 py-2 font-semibold text-white hover:bg-blue-700">Publish</button>
<button class="rounded-md bg-blue-600 px-4 py-2 font-semibold text-white hover:bg-blue-700">Send</button>Changing the padding now requires editing every copy. This is usually a template-reuse problem, not a CSS problem. The styles are valid; their ownership is scattered.
When Loops and Multi-Cursor Editing Are Enough
If repeated markup comes from data, use a loop. A loop renders one template for each item, so the class list has one source location. Multi-cursor editing is also reasonable for a small, stable set of copies.
Use these approaches when:
- The elements differ mainly by text, URL, or data.
- The pattern is local to one template.
- The class list has no meaningful public interface.
- You do not need the pattern across many screens.
A loop reduces editing work, but it does not create a reusable UI contract.
Extracting a Component
Extract a component when the pattern has behavior, accessibility rules, or variants. A component is a reusable UI unit with an explicit interface.
const variants = {
primary: "bg-blue-600 text-white hover:bg-blue-700",
secondary: "bg-gray-200 text-gray-900 hover:bg-gray-300",
};
export function Button({ variant = "primary", children, ...props }) {
return (
<button
className={`rounded-md px-4 py-2 font-semibold ${variants[variant]}`}
{...props}
>
{children}
</button>
);
}Keep complete class names in the source. Tailwind detects class names during its build process, so constructing names such as bg-${color}-600 can prevent the required styles from being generated.
Exercise
What should you extract first when three buttons share markup but differ only in their labels?
Show answer
Use one reusable component or loop, then pass the labels as data.