Sub topic 11
Colored shadows
What this chapter covers
A box shadow normally comes out black or gray. Tailwind lets you tint it with the same color utilities you already use for text, backgrounds, and borders. This chapter explains how the size class and the color class combine, how opacity works on shadows, and how to apply colored shadows on hover, in dark mode, and with arbitrary values.
It assumes you know the shadow size scale (shadow-sm, shadow-md, shadow-lg), the default palette with its shade numbers, and the slash opacity modifier such as bg-black/50. It also assumes you can run a Tailwind project or playground.
The basic pattern
A colored shadow uses two classes. One picks the size and one picks the color.
<div class="rounded-xl bg-white p-6 shadow-lg shadow-indigo-500/30">
Card with a tinted shadow
</div>shadow-lgselects the large shadow geometry: offsets, blur, and spread.shadow-indigo-500/30recolors that shadow to indigo 500 at 30% opacity.
The color class does nothing alone. shadow-indigo-500 on an element without a size class only sets a color value. No shadow exists to apply it to, so nothing appears. Always pair a color with a size.
Class order in the HTML does not matter. Tailwind decides precedence, so shadow-indigo-500/30 shadow-lg renders the same as the example above.
How it works internally
Shadows in Tailwind are built from CSS custom properties, not fixed values. This explains why two separate classes can cooperate.
In Tailwind v3, shadow-lg sets two variables: one holding the default black shadow and one holding the same geometry written with a --tw-shadow-color placeholder. The color utility then sets --tw-shadow-color and tells the element to use the second version.
The generated CSS looks roughly like this:
/* Simplified from Tailwind v3 output */
.shadow-lg {
--tw-shadow: 0 10px 15px -3px rgb(0 0 0 / 0.1), 0 4px 6px -4px rgb(0 0 0 / 0.1);
--tw-shadow-colored: 0 10px 15px -3px var(--tw-shadow-color), 0 4px 6px -4px var(--tw-shadow-color);
box-shadow: var(--tw-ring-offset-shadow, 0 0 #0000), var(--tw-ring-shadow, 0 0 #0000), var(--tw-shadow);
}
.shadow-indigo-500\/30 {
--tw-shadow-color: rgb(99 102 241 / 0.3);
--tw-shadow: var(--tw-shadow-colored);
}Tailwind v4 reaches the same result with a different internal layout. The shadow reads --tw-shadow-color directly and falls back to its default black when no color is set. You write the same classes in both versions, but the generated CSS differs, so check which version your project uses before reading compiled output.
The box-shadow line also lists the ring variables. Rings and shadows share the single box-shadow property. A ring from focus:ring-2 and a colored shadow can therefore appear together without overwriting each other.
Choosing colors and opacity
Shadow color utilities accept every name and shade in the default palette, plus any custom colors you add to the theme. The slash modifier sets opacity for the shadow only.
| Class | Effect |
|---|---|
| `shadow-lg` | Default black shadow at its built-in low opacity |
| `shadow-lg shadow-indigo-500` | Fully opaque indigo shadow |
| `shadow-lg shadow-indigo-500/30` | Indigo shadow at 30% opacity |
| `shadow-lg shadow-black/25` | Darker neutral shadow |
| `shadow-lg shadow-transparent` | Shadow geometry kept, but invisible |
Two practical points follow from this.
- A fully opaque colored shadow looks heavy. The built-in black shadows are only about 10% opaque. Start with
/20to/40for colored shadows and raise it only if the effect is too faint. - Pick a shade close to the element's own color. A blue button with a
shadow-blue-500/40shadow looks like it glows. A blue button with a red shadow looks like a rendering mistake.
Colors also come out differently on different backgrounds. A shadow at 30% opacity is clearly visible on white, but it nearly vanishes on a dark slate background. Check the result on the surface where it will actually appear.
Colored shadows on a button
A common use is a button that has a soft glow matching its fill. This example is a complete page you can open in a browser. It uses the Tailwind v3 Play CDN script. In a real project, you would build with your normal setup instead.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="grid min-h-screen place-items-center bg-slate-50">
<button
class="rounded-md bg-indigo-600 px-5 py-2.5 font-medium text-white shadow-md shadow-indigo-500/30 transition-shadow hover:bg-indigo-500 hover:shadow-lg hover:shadow-indigo-500/50 focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:ring-offset-2"
>
Save changes
</button>
</body>
</html>Reading the shadow classes in order:
shadow-md shadow-indigo-500/30is the resting state: a medium shadow, tinted at 30%.hover:shadow-lg hover:shadow-indigo-500/50grows the shadow and strengthens the tint when the pointer is over the button.transition-shadowanimates the change. Without it, the shadow jumps between states.focus:ring-2 ... focus:ring-offset-2adds a keyboard focus indicator. The ring and the shadow coexist because both write into the samebox-shadowlist.
The hover variant applies to either part independently. You can change only the color (hover:shadow-indigo-500/50), only the size (hover:shadow-lg), or both, as above.
Dark mode
Shadows are hard to see on dark surfaces, because there is little light for a shadow to darken. Two approaches work.
<div class="rounded-xl bg-white p-6 shadow-lg shadow-slate-900/10 dark:bg-slate-800 dark:shadow-none dark:ring-1 dark:ring-white/10">
Adapts to dark mode
</div>- Remove the shadow with
dark:shadow-noneand use a faint light ring or border to define the edge. This is the more reliable approach on dark backgrounds. - Switch to a colored glow, such as
dark:shadow-indigo-400/20. A light colored shadow can read as a glow on dark surfaces where a black shadow would be invisible.
Neither is universally better. Use the ring approach for cards and panels. Use the glow approach for accent elements such as primary buttons.
Arbitrary values
Use square brackets when the geometry or color is not in your theme. Underscores replace spaces inside the brackets.
<div class="rounded-lg bg-white p-4 shadow-[0_4px_12px_rgba(0,0,0,0.15)]">
Custom geometry
</div>
<div class="rounded-lg bg-white p-4 shadow-[0_8px_24px_-4px] shadow-[color:#1da1f2]/40">
Custom geometry, custom color
</div>In the second example, the first class defines only the geometry. The second class supplies the color. The color: prefix is a type hint. Tailwind cannot always tell from shadow-[#1da1f2] whether the brackets hold a shadow definition or a color, so the hint removes the ambiguity.
If a color is reused in several places, define it in the theme instead (see the chapter on custom colors). Arbitrary values are best kept for true one-offs.
Common errors
- Adding only a color class.
shadow-indigo-500with no size class produces no visible shadow. Addshadow-mdor another size. - Using
shadow-noneand expecting a color to survive.shadow-noneremoves the geometry, so the color has nothing to tint. - Relying on the shadow for information. A faint colored glow around an error field is not enough to signal an error. Pair it with a visible border, text, or an icon.
- Opacity too high. Opaque colored shadows look like colored borders offset to one side. Lower the slash value.
- Missing the transition. Hover changes snap unless you add
transition-shadow(ortransition-all). - Mixing v3 and v4 size names. v4 renamed the smaller sizes: what v3 called
shadow-smisshadow-xsin v4, and v3's plainshadowisshadow-sm. The color classes are unchanged.
Accessibility notes
Colored shadows are decoration. They do not count toward contrast requirements for text or UI component boundaries. If a component's edge is only visible because of a faint shadow, add a border or ring that meets contrast requirements against the background. For focus states, use a ring (as in the button above) rather than a shadow alone, because a soft shadow is easy to miss.
Exercise
A teammate writes <div class="rounded-lg bg-white p-4 shadow-emerald-500/40"> and sees no shadow. What is wrong, and how do you fix it so the shadow turns slightly stronger and larger on hover?
Show answer
The element has a shadow color but no shadow size class, so there is no box-shadow to tint. Add a size such as shadow-md, then add hover variants for the larger size and stronger tint: rounded-lg bg-white p-4 shadow-md shadow-emerald-500/40 transition-shadow hover:shadow-lg hover:shadow-emerald-500/60.
Exercise
You have a card with shadow-lg shadow-slate-900/10 that looks fine in light mode but loses its edge on a dark slate background. Name two dark mode changes that restore a visible edge.
Show answer
Either replace the shadow with a faint ring using dark:shadow-none dark:ring-1 dark:ring-white/10, or switch to a lighter colored glow such as dark:shadow-indigo-400/20. The ring is more reliable for cards and panels. The glow suits accent elements.