Sub topic 11
Colored shadows
This chapter assumes you know the box model, can read Tailwind utility classes, and have already used the shadow size scale (shadow-md, shadow-lg) and the slash opacity modifier from earlier chapters. Colored shadows build on both.
Why shadows have a color
A box shadow is a blurred copy of an element's outline, drawn behind it. By default Tailwind draws that copy in black or near-black at low opacity, which looks neutral on white backgrounds. On colored or dark backgrounds, a black shadow can look muddy. Tinting the shadow with a hue close to the element's own color makes it look like light passing through or glowing around the element.
How shadow color works
Tailwind separates shadow size from shadow color. The size utilities, such as shadow-lg, set the offsets, blur, and spread. The color utilities, such as shadow-indigo-500, set only the color. You combine them on the same element.
<div class="rounded-xl bg-white p-6 shadow-lg shadow-indigo-500/30">
Card with a tinted shadow
</div>Here shadow-lg selects the large shadow size, and shadow-indigo-500/30 recolors it to indigo at 30% opacity. The /30 is the same opacity modifier you used for backgrounds and borders.
Internally, each size utility writes a CSS custom property that holds the shadow color, and the color utility overrides that property. Because of this, the order of the two classes in your HTML does not matter.
Choosing the opacity
A fully opaque colored shadow looks heavy and unnatural. Most tinted shadows work better between 20% and 50% opacity. Try a few values and compare them against your background.
| Class | Effect |
|---|---|
| `shadow-lg` | Default neutral shadow |
| `shadow-lg shadow-indigo-500` | Solid indigo shadow, usually too strong |
| `shadow-lg shadow-indigo-500/30` | Soft indigo glow, a common choice |
| `shadow-lg shadow-black/10` | Lighter neutral shadow |
Using other color sources
Shadow colors accept the same sources as other color utilities:
- Palette colors with a shade number, such as
shadow-emerald-600/40. - Custom theme colors you added earlier, such as
shadow-brand-500/30. - Arbitrary values, such as
shadow-[#1da1f2]/40.
If you write the whole shadow as an arbitrary value, for example shadow-[0_4px_12px_rgba(0,0,0,0.15)], the color is part of that value. In that case a separate shadow-* color class has nothing to override, so put the color in the brackets.
Combining with hover
Shadow colors work with state variants. A common pattern is a button whose glow grows on hover:
<button class="rounded-md bg-indigo-600 px-4 py-2 text-white shadow-md shadow-indigo-500/30 transition hover:shadow-lg hover:shadow-indigo-500/50">
Save changes
</button>The transition class animates the change smoothly.
Common errors
- No visible change. Check that a size utility is present. A color class alone does not add a shadow in v3, because the size utility is what turns the shadow on.
- Shadow too strong. Add or lower the opacity modifier.
- Shadow is clipped. A parent with
overflow-hiddencuts off the shadow. Move the shadow to the clipping element's parent or remove the overflow rule. - Tinted shadow disappears in dark mode. Dark backgrounds hide dark shadows. Use a dark: variant such as
dark:shadow-indigo-400/20, or drop the shadow and use a border instead.
Exercise
A card has the classes rounded-lg bg-white p-4 shadow-xl shadow-emerald-500. The shadow looks too harsh. Which single change makes it a soft green glow, and why does it work?
Show answer
Change shadow-emerald-500 to shadow-emerald-500/30 (or another value between 20 and 50). The slash opacity modifier lowers the alpha of the shadow color while shadow-xl still controls the size, so the shadow stays green but becomes softer.
Exercise
You write shadow-[0_4px_12px_rgba(0,0,0,0.15)] shadow-indigo-500/30 on an element. Which color will the shadow have, and how would you get an indigo shadow?
Show answer
The shadow stays black at 15% opacity, because the arbitrary value defines the color itself and there is no variable for the color class to override. To get indigo, put the color inside the brackets, for example shadow-[0_4px_12px_rgba(99,102,241,0.3)], or use a size utility such as shadow-md with shadow-indigo-500/30.