Sub topic 7
Box shadows
This chapter assumes you know basic HTML and CSS, including the box model, and that you can already use Tailwind utility classes in a project or playground. You should also have read the earlier chapters on color utilities and borders.
What a box shadow does
A box shadow is a blurred copy of an element's box, drawn behind it. It suggests elevation: the larger and softer the shadow, the higher the element appears to sit above the page. A shadow does not take up space. It never changes layout or pushes neighbors away.
Tailwind exposes shadows as a named scale. You pick a size and the framework supplies the offset, blur, spread, and color.
The shadow scale
| Class | Typical use |
|---|---|
| `shadow-sm` | Subtle lift for inputs and small buttons |
| `shadow` | Default, general-purpose lift |
| `shadow-md` | Cards at rest |
| `shadow-lg` | Dropdowns and raised cards |
| `shadow-xl` | Popovers and modals |
| `shadow-2xl` | Large overlays that must stand out |
| `shadow-none` | Removes a shadow |
Exact names shift between versions. Tailwind v4 renamed some steps, so shadow-sm in v4 corresponds to what v3 called shadow. Check the documentation for the version in your project before relying on a name.
<div class="rounded-xl bg-white p-6 shadow-lg shadow-indigo-500/30">
Card with a tinted shadow
</div>Here rounded-xl rounds the corners, and the shadow follows the rounded shape. shadow-lg selects the large size. The second class, shadow-indigo-500/30, recolors the shadow, which the chapter on colored shadows covers in more detail.
Shadows and color
The default shadow color is a translucent black. On a white page this looks natural. On a dark background, a black shadow is nearly invisible, so you may need a lighter background for the element or a colored shadow to get any visible effect.
Shadow color utilities use the same palette and the same slash opacity syntax you met earlier. shadow-indigo-500/30 means "indigo at shade 500, 30% opaque." A tinted, low-opacity shadow often looks more refined than a heavy gray one, because it picks up the hue of the element it belongs to.
Shadows and state
Shadows combine with variants such as hover: and focus:. A common pattern raises a card when the pointer is over it.
<div class="rounded-xl bg-white p-6 shadow-md transition-shadow hover:shadow-xl">
Hover to raise
</div>transition-shadow animates the change. Without it, the shadow jumps between sizes.
Common errors
- Shadow not visible. The element may sit on a background of the same tone, or a parent with
overflow-hiddenmay be clipping it. Move the shadow to the clipping element's parent. - Wrong size name after upgrading. Utility names changed between v3 and v4, so a shadow can look smaller or larger than before.
- Heavy shadows everywhere. Large shadows on every element flatten the sense of depth. Reserve the bigger sizes for elements that really float.
- Shadow on a transparent element. An element with no background still casts a box-shaped shadow, which looks odd. Give it a background first.
Choosing a size
Exercise
A card uses shadow-md and should rise to shadow-xl on hover with a smooth animation. Which classes do you add to its class list?
Show answer
Add hover:shadow-xl to set the larger shadow on hover, and transition-shadow so the change animates instead of jumping. The full list is shadow-md transition-shadow hover:shadow-xl.
Exercise
A card has shadow-lg but no shadow appears on one side. Its parent has overflow-hidden. What is the likely cause, and how do you fix it?
Show answer
The parent clips anything that extends beyond its box, including the shadow. Remove overflow-hidden from the parent, or add padding to the parent so the shadow has room to render inside it.