Sub topic 6
Merging classes and resolving conflicts
This chapter assumes you know Tailwind utility classes, basic React components, and how the CSS cascade works. It builds on the variant props from the previous chapter. Here you will see why overriding classes on a component can fail, and how to fix it.
Why overrides fail
A reusable component usually accepts a className prop so callers can adjust it. The obvious implementation joins the strings:
export function Button({ className = "", children, ...props }) {
return (
<button
className={`rounded-md px-4 py-2 bg-blue-600 text-white ${className}`}
{...props}
>
{children}
</button>
);
}Now a caller writes <Button className="px-8">Wide</Button>. The element gets both px-4 and px-8. Which one wins?
Not the one that appears later in the class attribute. The browser ignores the order of names in the attribute. It applies the rule that comes later in the generated stylesheet. Tailwind decides that order, and it is not the order you wrote. For two utilities that set the same property, the winner can be either one, and you can't control it from the markup.
Resolving conflicts with tailwind-merge
The tailwind-merge package understands which utilities conflict. It keeps the last one and removes the earlier ones, so only one class sets each property.
import { twMerge } from "tailwind-merge";
twMerge("px-2 py-1", "p-3"); // "p-3"Notice that p-3 replaces both px-2 and py-1. The library knows that p-3 covers horizontal and vertical padding. A plain string comparison could not do this.
Order matters in the arguments. Later arguments win:
import { twMerge } from "tailwind-merge";
console.log(twMerge("px-4 py-2", "px-8")); // "py-2 px-8"
console.log(twMerge("bg-blue-600", "hover:bg-blue-700", "bg-red-600")); // "hover:bg-blue-700 bg-red-600"The second call shows that variants are tracked separately. hover:bg-blue-700 does not conflict with bg-red-600, because they apply under different conditions.
Using it in a component
Put the defaults first and the caller's className last:
import { twMerge } from "tailwind-merge";
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", className, children, ...props }) {
return (
<button
className={twMerge(
"rounded-md px-4 py-2 font-semibold",
variants[variant],
className
)}
{...props}
>
{children}
</button>
);
}Now <Button className="px-8"> renders px-8 and drops px-4. The variant map still holds complete class strings, so Tailwind can detect every class in the source.
Many projects wrap this in a small helper, often named cn, that also accepts conditional values through the clsx package:
import { clsx } from "clsx";
import { twMerge } from "tailwind-merge";
export function cn(...inputs) {
return twMerge(clsx(inputs));
}
// cn("px-2", false && "hidden", { "font-bold": true }, "px-4")
// returns "font-bold px-4"Common errors
- Putting the caller's classes first.
twMerge(className, defaults)lets the defaults win, so the override is silently lost. - Building class names from fragments. `
bg-${color}-600never appears as a complete string, so Tailwind does not generate it.twMerge` cannot fix this. Map names to full strings instead. - Expecting it to fix custom classes.
twMergeknows Tailwind's built-in utilities. A class you wrote yourself, such as.btn-primary, is unknown to it unless you configure the library. - Merging everything by default. It adds a small runtime cost and another dependency. If a component never accepts overrides, plain string joining is enough.
Exercise
A component renders className={px-4 py-2 ${className}}. A caller passes p-6. Why might the padding not change reliably, and what change fixes it?
Show answer
Both px-4/py-2 and p-6 end up on the element. The browser picks the winner by the order of rules in the generated stylesheet, not by the order in the class attribute, so the result is unreliable. Wrap the string in twMerge("px-4 py-2", className), which removes px-4 and py-2 because p-6 conflicts with them and comes later.
Exercise
What does twMerge("bg-blue-600 hover:bg-blue-700", "bg-red-600") return, and why is hover:bg-blue-700 kept?
Show answer
It returns "hover:bg-blue-700 bg-red-600". The hover class applies only under a different condition (the hover state), so it does not conflict with the plain bg-red-600. Only classes with the same variant and the same property conflict.