Sub topic 3
Adjusting transparency with the opacity modifier
This chapter assumes you can already apply palette colors such as bg-indigo-600 and text-slate-900, and that you know the CSS rgb() notation with an alpha channel. It covers the slash syntax that makes one color translucent, how it differs from the opacity-* utility, and the version differences that cause most mistakes.
What the modifier does
A color utility accepts an opacity modifier. This is a slash and a number written directly after the color name:
<div class="border border-white/20 bg-black/50 p-4 text-white">
Semi-transparent panel
</div>This one element uses three different colors:
bg-black/50is a black background at 50% opacity.border-white/20is a white border at 20% opacity.text-whiteis fully opaque white text.
The number is a percentage. /0 is invisible and /100 is the same as leaving the modifier off. Each class changes only the color it names, so the text in the example stays fully readable while the background lets the page show through.
The modifier works on every utility that takes a color:
| Utility | Example | Effect |
|---|---|---|
| Background | `bg-indigo-600/80` | Translucent fill |
| Text | `text-slate-900/70` | Softer text |
| Border | `border-slate-300/50` | Fainter outline |
| Divide | `divide-white/10` | Fainter lines between siblings |
| Ring | `ring-indigo-500/40` | Softer focus ring |
| Shadow color | `shadow-indigo-500/30` | Tinted, lighter shadow |
The shadow row is the one that matters most in practice. A shadow color with no opacity looks heavy, so shadow-lg shadow-indigo-500/30 is the usual way to tint a shadow.
How it works internally
The modifier changes the color value, not the element. Tailwind generates a different declaration for each class.
In Tailwind v3, colors are stored in a form that has an alpha placeholder. The class bg-black/50 compiles to roughly this:
.bg-black\/50 {
background-color: rgb(0 0 0 / 0.5);
}In Tailwind v4, the same class compiles to a color-mix() expression. The result is the same: the color is blended with transparent at the percentage you gave.
Both outputs set only the alpha channel of one property. The element's other properties, and the elements inside it, are not affected. That is the key difference from the opacity-* utility.
Modifier vs `opacity-*`
These two classes look similar and behave very differently:
| Row | `bg-black/50` | `opacity-50` |
|---|---|---|
| What becomes translucent | Only the background color | The whole element, including text, borders, shadows, and children |
| Child elements | Unchanged | Faded along with the parent |
| Can differ per property | Yes | No |
| Stacking context | Not created | Created (values below 100 create one) |
Use opacity-* when you want to fade an entire element, such as a disabled button or a fade-in animation. Use the modifier when you want a see-through surface that holds readable content.
<!-- Wrong for a readable overlay: the text fades with the box -->
<div class="bg-black text-white opacity-50 p-4">Hard to read</div>
<!-- Better: only the background is translucent -->
<div class="bg-black/50 text-white p-4">Easy to read</div>The second form is the one you want for captions over photos, sticky headers, and modal backdrops.
Which values are available
The available values depend on your Tailwind version.
- In v3, the modifier accepts the keys of the
opacitytheme scale: 0, 5, 10, 20, 25, 30, 40, 50, 60, 70, 75, 80, 90, 95, and 100. A class likebg-black/37produces no CSS in v3. - In v4, any integer percentage works, so
bg-black/37is valid.
For a value outside the v3 scale, use brackets. Both forms below work in v3 and v4:
<div class="bg-black/[0.37] text-white p-4">Fractional alpha</div>
<div class="bg-black/[37%] text-white p-4">Percent alpha</div>The bracket form also combines with arbitrary colors. In this example, the hex color is converted to a translucent version of itself:
<div class="border border-[#1da1f2] bg-[#1da1f2]/10 p-4 shadow-[0_4px_12px_rgba(0,0,0,0.15)]">
One-off values
</div>The border is fully opaque, the background is 10% of the same blue, and the shadow carries its own alpha inside rgba(). An arbitrary shadow value like this one is written out in full, so the modifier does not apply to it.
Seeing the effect
Transparency only shows when something is behind the element. The next example puts three panels over a gradient so that you can compare the alpha levels:
<div class="bg-gradient-to-r from-fuchsia-500 to-cyan-400 p-8">
<div class="grid gap-4">
<div class="rounded-lg bg-white/10 p-4 text-white">white/10</div>
<div class="rounded-lg bg-white/40 p-4 text-slate-900">white/40</div>
<div class="rounded-lg bg-white/80 p-4 text-slate-900">white/80</div>
</div>
</div>The first panel is barely visible, and the last is almost solid white. In v4, the bg-gradient-to-r class is renamed bg-linear-to-r. The old name still works but is deprecated.
Translucent panels often pair well with a blur of what is behind them:
<div class="rounded-xl border border-white/30 bg-white/20 p-6 text-white backdrop-blur-md">
Frosted glass card
</div>backdrop-blur-md blurs the content behind the element. Without it, a translucent background can make text over a busy image hard to read.
Common errors
Using the removed `bg-opacity-*` classes
Tailwind v2 and v3 had separate classes such as bg-opacity-50, text-opacity-50, and border-opacity-50. These were deprecated in v3 and removed in v4. The slash form replaces them:
<!-- Old -->
<div class="bg-black bg-opacity-50"></div>
<!-- Current -->
<div class="bg-black/50"></div>Custom colors that ignore the modifier
In v3, the modifier works on colors that Tailwind can parse, such as hex values. If you define a color as a bare CSS variable, Tailwind cannot insert an alpha channel. The usual v3 fix is to store the channels in the variable and use the <alpha-value> placeholder:
// tailwind.config.js (Tailwind CSS v3)
module.exports = {
theme: {
extend: {
colors: {
brand: 'rgb(var(--brand-rgb) / <alpha-value>)',
},
},
},
}With --brand-rgb: 99 102 241; defined in your CSS, bg-brand/50 now works. In v4, colors defined with @theme accept the modifier automatically, because color-mix() works on any color value.
Reading contrast against the wrong background
The contrast of text on bg-white/80 depends on whatever is behind the panel, so it changes when the page behind it changes. Check contrast against the lightest and darkest areas the panel can sit over. When in doubt, raise the opacity until the text passes. Chapter 15 covers contrast checks in detail.
Overlapping translucent layers
Two translucent layers blend. A bg-black/50 panel inside a bg-black/50 section looks darker than either alone, at roughly 75% black. This is correct behavior but often surprises people when they nest cards.
Pairing with state variants
The modifier combines with variants. A button can fade its fill on hover:
<button class="rounded-md bg-indigo-600/90 px-4 py-2 text-white hover:bg-indigo-600 focus:ring-2 focus:ring-indigo-500/50">
Save changes
</button>The button is slightly translucent at rest and fully solid on hover. The focus ring is also softened to 50%. A ring that faint on a white page may fail contrast requirements, so test it.
Exercise
A card needs a dark, 60% opaque background, and the white text inside it must stay fully opaque and readable. Which classes should you use on the card, and why is opacity-60 the wrong choice? Assume Tailwind v3.
Show answer
Use bg-black/60 text-white. The slash modifier changes only the alpha channel of the background color, so the text keeps full opacity. opacity-60 applies to the whole element, so the text, border, and any child elements would fade to 60% as well, which lowers contrast.
Exercise
In Tailwind v3, bg-indigo-600/45 produces no background. What is the cause, and what are two ways to fix it?
Show answer
The value 45 is not in the default v3 opacity scale (which includes 40 and 50), so no CSS is generated. Fix it either by using an arbitrary value, bg-indigo-600/[0.45] or bg-indigo-600/[45%], or by adding 45 to the opacity scale under theme.extend in tailwind.config.js. In v4, bg-indigo-600/45 works without changes.