Sub topic 2
Spotting real duplication
Tailwind projects accumulate long class lists. Some of that repetition is a real maintenance problem, and some is harmless. This chapter helps you tell the two apart before you reach for a fix. It assumes you know basic HTML and CSS (classes, the cascade), common Tailwind utilities such as flex, px-4, and bg-blue-500, and have seen a component-based framework like React or Vue, or template partials.
Why long class lists appear
Tailwind is utility-first. Each class does one small job, such as px-4 for horizontal padding or font-semibold for weight. You build a design by combining many of them on one element. The result is that a single button can carry six or eight classes, and a card can carry a dozen.
This is a trade-off of the approach, not a mistake. You get no naming overhead, no jumping between files, and no CSS that grows with every new element. In return, the markup is longer, and the same combination can show up in many places.
The problem: the same list in many places
Here are three buttons from a small admin screen.
<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>The three class lists are identical. Now suppose the design changes: primary buttons should have rounded-lg and a focus ring. You have to find every button, in every file, and edit each one. Miss one and the interface is inconsistent.
That is the cost of duplication: one decision lives in many places, so changing it means many edits and any missed edit creates drift.
It is a template problem, not a CSS problem
A common first reaction is "Tailwind is generating bloated CSS." That is not what is happening. Tailwind emits each utility class once, no matter how many elements use it. Whether px-4 appears on three buttons or three hundred, the stylesheet contains one .px-4 rule.
The repetition lives in your templates: your HTML, JSX, or Vue files. Compare this with traditional CSS, where you might write .btn-primary once in a stylesheet and use the name everywhere. There the decision is stored in one place, in CSS. With Tailwind, the decision is a combination of classes, and the combination is stored wherever you write it.
This changes what you should look for. You are not trying to shrink CSS output. You are trying to give a repeated design decision a single home. Often the right home is a template-level tool such as a component, a loop, or a partial, because that is where the repetition is.
Real duplication versus coincidence
Two elements with the same classes are not always duplicates. Ask whether they represent the same design decision.
Real duplication means the elements are the same thing, and when you change one you would want the others to change too. Coincidental similarity means the elements happen to look alike today but could reasonably diverge.
Consider these two elements.
<div class="rounded-lg border border-gray-200 p-4 shadow-sm">Pricing plan</div>
<div class="rounded-lg border border-gray-200 p-4 shadow-sm">Error log entry</div>They match, but a pricing card and a log entry are different concepts. If marketing wants pricing cards to have a larger shadow, the log entry should not change. Merging them into one abstraction ties together two things that have different reasons to change. You would later fight that abstraction with overrides.
Use these questions as a test.
| Question | Real duplication if... | Coincidence if... |
|---|---|---|
| Do they have the same name in the design? | Both are "primary button" or "form label". | You would describe them with different names. |
| Do they change together? | A design update would apply to all of them. | An update to one would not apply to the others. |
| Does the markup repeat too? | Structure (icon plus label, wrapper plus heading) repeats. | Only a few classes overlap. |
| Is the repetition meaningful to a reader? | You could say "these are all X". | You can only say "these have the same padding". |
A useful shortcut is the "rule of three". Tolerate the first repeat. When you write the same combination a third time, and the answers above say it is one concept, treat it as real duplication.
Partial overlap is usually fine
Class lists that share a few utilities are not duplication in the harmful sense. Compare these.
<button class="rounded-md bg-blue-600 px-4 py-2 text-white">Save</button>
<a class="rounded-md bg-green-600 px-4 py-2 text-white">Download</a>
<span class="rounded-md bg-gray-900 px-4 py-2 text-white">Beta</span>All three share rounded-md px-4 py-2 text-white, but they are a button, a link, and a badge. Pulling out the shared four classes would create an abstraction with no meaning in your design vocabulary. Extra indirection costs more than the repeated classes do. Look for repeated whole concepts, not repeated fragments.
Spotting duplication in practice
You can often find duplication by looking, but a quick search helps on larger codebases. This command lists the most frequent complete class or className values in a src folder.
grep -rhoE '(class|className)="[^"]*"' src | sort | uniq -c | sort -rn | head -n 10The output is a count followed by the attribute. A line like 14 class="rounded-md bg-blue-600 px-4 py-2 font-semibold text-white hover:bg-blue-700" is a strong candidate. Remember it only catches exact matches. Classes written in a different order, or with one extra utility, are counted separately, so treat the output as a starting point and read the candidates.
Drift: the sign that the problem is already real
Duplication tends to reveal itself through inconsistency. Look at the same three buttons a few months later.
<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-3 py-2 font-semibold text-white hover:bg-blue-700">Publish</button>
<button class="rounded-md bg-blue-500 px-4 py-2 font-semibold text-white hover:bg-blue-600">Send</button>The second button has px-3. The third uses a lighter blue. Nobody decided these changes. They came from copying, tweaking, and forgetting. If you see near-identical lists with small differences, you are looking at drift. It tells you the elements were meant to be the same concept, which is strong evidence of real duplication.
Choosing where to go next
Once you decide duplication is real, the right fix depends on its shape. This diagram shows the decision at a high level. The later chapters cover each option in detail.
Three families of fix exist. A loop handles repetition that comes from rendering a list, because you write the class list once inside the loop body. A component handles repeated structure and classes together, and gives the concept a name in your framework. A CSS class built with @apply gives a name to a class list when you cannot or do not want to wrap it in a component, such as in plain HTML or CMS-generated markup.
Common mistakes when judging duplication
- Abstracting on the first repeat. Two uses are not enough evidence about how a concept will vary. Wait for a third, or for a clear design name.
- Extracting fragments. Pulling
px-4 py-2into its own class gives you a name with no meaning, and you will still need other classes beside it. - Treating CSS size as the metric. Tailwind's output does not grow with repetition, so deduplicating does not shrink it.
- Ignoring drift. Near-duplicates with small differences are often the clearest sign that a shared definition is missing.
- Merging different concepts that look alike. If the elements will diverge later, a shared abstraction becomes a source of overrides.
Check your understanding
Exercise
Tailwind generates one .px-4 rule no matter how many elements use it. Given that, what is the actual cost of repeating the same long class list on 20 buttons?
Show answer
The cost is in the templates, not in the CSS output. The design decision for "what a primary button looks like" lives in 20 places, so a change needs 20 edits, and a missed edit causes inconsistency (drift). Stylesheet size is not affected.
Exercise
You find these two elements: <div class="rounded-lg border p-4 shadow-sm"> used for a pricing card, and the same classes used for a log entry row. Should you extract them into one shared component? Give two questions from this chapter that support your answer.
Show answer
Probably not. They are different design concepts: they would not have the same name in the design, and a change to pricing cards (for example a bigger shadow) would not be wanted on log entries, so they do not change together. Sharing one abstraction would later force overrides.
Exercise
Your search with grep ... | sort | uniq -c shows a list repeated 9 times, plus three near-copies that differ by one class each (px-3 instead of px-4, bg-blue-500 instead of bg-blue-600). What does this suggest, and what should you check before extracting?
Show answer
The near-copies suggest drift: the elements were meant to be the same concept, which supports real duplication. Before extracting, confirm with the design (or the team) which version is correct, so the shared definition uses the intended values rather than copying a mistake.