Sub topic 3
Adjusting transparency with the opacity modifier
This chapter assumes you know basic HTML and CSS, can write Tailwind utility classes, and have a project or playground where you can try them. You should also recognize hex and RGB color notation.
What the modifier does
Color utilities such as bg-black or border-white produce a fully opaque color. To make a color partly transparent, add a slash and a number from 0 to 100 after the color name. The number is the opacity as a percentage.
<div class="border border-white/20 bg-black/50 p-4 text-white">
Semi-transparent panel
</div>Here the background is 50% black and the border is 20% white. The text has no modifier, so it stays fully opaque.
The slash syntax works on any color utility: text-*, bg-*, border-*, ring-*, divide-*, shadow-*, and others.
Why not use the opacity utility?
Tailwind also has opacity-50. It fades the whole element, including its text, borders, and children. The slash modifier changes only one color on one property.
| Class | What becomes transparent |
|---|---|
| `bg-black/50` | Only the background color |
| `opacity-50` | The entire element and everything inside it |
If you want a dimmed background behind readable text, use bg-black/50. Using opacity-50 on the same element would also fade the text.
Choosing values
The default scale includes steps such as 0, 5, 10, 20, 25, 30, 40, 50, 60, 70, 75, 80, 90, 95, and 100. Pick the nearest step, such as bg-indigo-500/25.
For other values, use square brackets or a bare number. In Tailwind v3.3 and later, and in v4, any integer works. In older versions, a bracketed value like bg-black/[0.35] is the safe form.
<div class="bg-indigo-500/25 border border-indigo-500/60 p-4 text-indigo-900">
Tinted notice
</div>This pattern pairs a light tint of a color with a stronger edge of the same color. It is a common way to build alerts and badges.
Combining with variants
Variants go in front of the whole class, so each state can have its own opacity.
<button class="rounded-md bg-indigo-600/80 px-4 py-2 text-white hover:bg-indigo-600">
Hover me
</button>The button is slightly see-through at rest and becomes solid on hover.
Common errors
- Putting the slash on the wrong side. The modifier belongs after the color, as in
bg-black/50. Writingbg-black-50asks for a shade named 50, which does not exist for black. - Expecting readable text on a transparent background. The final contrast depends on whatever sits behind the element. Check contrast against the real backdrop, not just the color you wrote.
- Using
opacity-*when you wanted a translucent background. This fades the text too. - Stacking transparent layers by accident. Two
bg-black/50elements nested together produce a darker result than either alone, because the colors blend.
How it works
Tailwind reads the color and the opacity from the class name and combines them into one CSS color value with an alpha channel. Only the property named in the utility receives that color. That is why other properties on the element stay opaque.
Exercise
A card needs a dark, 60% opaque black background, but its white text must stay fully opaque. Which classes would you use, and why not opacity-60?
Show answer
Use bg-black/60 text-white. The slash modifier applies 60% opacity only to the background color. opacity-60 would fade the whole element, including the text, which hurts readability.
Exercise
What does border-white/20 do differently from border-white?
Show answer
It sets the border to white at 20% opacity, so it appears as a faint line that blends with whatever is behind it. border-white is solid white.