Sub topic 7
Box shadows
This chapter assumes you know basic HTML and CSS, including the box model, and that you can use Tailwind utility classes in a project or playground. It also assumes you have met the color palette and the slash opacity modifier from earlier chapters. Here you will learn the box shadow scale, how shadows behave in practice, and the mistakes that most often make shadows look wrong.
What a box shadow is
A box shadow is a blurred copy of an element's box, drawn behind it. CSS describes it with the box-shadow property. Each shadow has an x offset, a y offset, a blur radius, an optional spread radius, and a color. Tailwind wraps common combinations into named utilities, so you rarely write those values by hand.
Shadows do not affect layout. They take up no space, they do not push neighbors away, and they can be clipped by a parent that has overflow-hidden. They follow the element's border radius, so a rounded-xl element casts a rounded shadow.
The shadow scale
The default scale runs from subtle to dramatic. The class names below are from Tailwind v3. Tailwind v4 shifted the scale down one step, as the callout after the table explains.
| Class | Typical use |
|---|---|
| `shadow-sm` | Very small lift, such as inputs and small buttons |
| `shadow` | Light default shadow for cards and buttons |
| `shadow-md` | Standard card or dropdown |
| `shadow-lg` | Raised panels, popovers |
| `shadow-xl` | Large floating panels |
| `shadow-2xl` | Modals and hero images |
| `shadow-inner` | Shadow drawn inside the box, for inset or pressed looks |
| `shadow-none` | Removes any shadow |
Each step up uses a larger offset and blur. Tailwind's own shadows are layered: most of them stack two shadows, one soft and wide, one tighter. This is why they look more natural than a single box-shadow: 0 4px 10px black.
A first example
Here is a card that uses a shadow from the scale:
<div class="rounded-xl bg-white p-6 shadow-md">
Default card shadow
</div>On a white page, a shadow is faint. Shadows read best against a tinted background, so a light gray page such as bg-slate-100 shows them clearly. If you cannot see a shadow, check the page background before you change the shadow size.
Elevation as a system
Treat the scale as elevation. Elements that sit closer to the user get larger shadows.
- Flat content on the page: no shadow, or a border.
- Cards and inputs:
shadow-smorshadow-md. - Dropdowns and popovers:
shadow-lg. - Modals and dialogs:
shadow-xlorshadow-2xl.
Pick one level per kind of component and keep it consistent. A page where every card has a different shadow looks unplanned. A modal with a smaller shadow than the card behind it looks wrong, because the shadow implies the opposite stacking.
Shadows with state variants
A common pattern is to raise a card when the pointer is over it. Variants work on shadows the same way they work on colors:
<div class="rounded-xl bg-white p-6 shadow-md transition-shadow duration-200 hover:shadow-xl">
Hover to lift
</div>transition-shadow animates only the box-shadow property. Without a transition class, the shadow changes instantly. transition-shadow is more targeted than transition-all, which animates every property and can cause unwanted motion.
The focus: variant also works, but for focus indicators prefer rings, which a later chapter covers. Shadows are weaker as focus indicators because they are soft and low in contrast.
Colored shadows
By default, the shadow is a translucent black. A color utility starting with shadow- changes that color. Pair it with a size class, because the color alone does not turn a shadow on:
<div class="rounded-xl bg-white p-6 shadow-lg shadow-indigo-500/30">
Card with a tinted shadow
</div>shadow-lg selects the size. shadow-indigo-500/30 recolors it to indigo at 30% opacity. Without the opacity part, a fully opaque indigo shadow looks heavy and harsh.
A good rule is to match the shadow hue to the element. A blue button with a blue shadow looks like it glows. A blue button with a black shadow looks flat and slightly dirty. Keep the opacity between about 20% and 40% for most cases.
How it works internally
The size utilities set a CSS custom property for the shadow color and refer to it inside box-shadow. In Tailwind v3, a size class such as shadow-lg produces CSS like this:
.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);
}A color class sets --tw-shadow-color and swaps in the colored version of the shadow. This design has two consequences.
- Because rings and shadows share one
box-shadowdeclaration, they combine without overwriting each other. You can putring-2andshadow-lgon the same element. - A custom
box-shadowthat you write in your own CSS will override all of these layers, including any ring. Use Tailwind utilities for everything on that element, or extend the theme instead.
One-off shadows
When the scale does not have what you need, use arbitrary values in square brackets. Underscores stand in for spaces:
<div class="rounded-lg bg-white p-4 shadow-[0_4px_12px_rgba(0,0,0,0.15)]">
Custom shadow
</div>This is fine for a single use. If you use the same custom shadow in several places, add it to the theme instead. In Tailwind v3, put it under theme.extend.boxShadow. In v4, define a --shadow-* variable inside @theme. Then you can use a named class such as shadow-soft.
Dark mode and shadows
Black shadows are nearly invisible on a dark background. A dark card on a dark page often needs a different treatment. Common options are:
- Replace the shadow with a border, such as
dark:border-slate-700. - Use
dark:shadow-nonetogether with a lighter card color, since a lighter surface already suggests elevation. - Use a stronger black shadow, such as
dark:shadow-black/50.
<div class="rounded-xl border border-transparent bg-white p-6 shadow-md dark:border-slate-700 dark:bg-slate-800 dark:shadow-none">
Adapts to dark mode
</div>Here the transparent border reserves the same space in both modes, so the layout does not shift when dark mode switches the border color on.
Common errors
- Shadow invisible. The page background is white or the shadow is too small. Try a tinted background or a larger size.
- Shadow clipped. A parent with
overflow-hiddenoroverflow-autocuts off the shadow at its edge. Add padding to the parent or remove the overflow rule. - Colored shadow does nothing. There is no size class on the element.
- Hover jumps instead of animating. There is no
transition-shadowclass. - Wrong size in a v4 project. The scale shifted, so
shadowlooks smaller than expected. - Heavy, muddy look. Several large shadows stacked on nested elements. Put the shadow on the outer element only.
Performance note
Large, blurry shadows cost more to paint than small ones, and animating them forces repaints. For a few cards this does not matter. For a long list of items that animate shadows on hover, keep the shadows modest, or animate opacity of a pseudo-element that holds the larger shadow.
Exercise
A button has the classes bg-indigo-600 text-white shadow-indigo-500/40, but no shadow appears. What is missing, and what would you add so the shadow also grows on hover?
Show answer
A shadow size class is missing, because the color class only sets the shadow color. Add shadow-md, then add hover:shadow-lg for the larger hover shadow. Add transition-shadow so the change animates instead of jumping. The result is bg-indigo-600 text-white shadow-md shadow-indigo-500/40 hover:shadow-lg transition-shadow.
Exercise
Your card has shadow-lg and sits inside a wrapper with overflow-hidden. The shadow is cut off along the wrapper's edges. Name two ways to fix this.
Show answer
Add padding to the wrapper so the shadow has room to render inside it, or remove overflow-hidden from the wrapper. If the wrapper needs to clip other content, move the clipping to an inner element and keep the card's shadow on a layer outside it.