Skip to content
Tutorials Center
My learning

Topics

Lesson 37 of 139 ยท The Box Model

Box Shadows

Master CSS box-shadow, from offsets, blur and spread to inset and layered shadows, and build a consistent elevation system that looks natural on light and dark pages.

Shadows tell your eyes that something is lifted off the page. A card with a soft shadow feels like it floats; a button whose shadow grows on hover feels like it rises toward your finger; a dialog with a deep shadow clearly sits above everything else. The box-shadow property creates all of these.

Good shadows are subtle. Most beginners make them too dark and too sharp. In this lesson you will learn what each value does, how inset and multiple shadows work, and how to build a small elevation system so every shadow on a site feels consistent.

The anatomy of a shadow

A shadow is a copy of the element's box, painted behind it, shifted, blurred and colored. The full syntax has up to four lengths, a color and an optional inset keyword:

CSS
.card {
  /* x-offset  y-offset  blur  spread  color */
  box-shadow: 0 4px 12px 0 rgb(0 0 0 / 0.15);
}
  • x-offset: moves the shadow right (positive) or left (negative).
  • y-offset: moves it down (positive) or up (negative). Light usually comes from above, so most shadows use a positive y-offset.
  • blur radius (optional, default 0): how soft the edge is. 0 gives a hard edge. It can't be negative.
  • spread radius (optional, default 0): grows the shadow (positive) or shrinks it (negative) before blurring.
  • color (optional): defaults to currentColor. In practice you almost always set it, usually as a transparent black.

The first two lengths are required. If there are three, the third is blur; if four, the fourth is spread.

Shadow playground

Drag the sliders and notice:

  • With blur at 0, the shadow is a solid shifted copy of the box.
  • A negative spread with a bigger y-offset gives a shadow that only peeks out at the bottom, which looks very natural.
  • Opacity matters most. Real shadows are rarely darker than 10โ€“25% black.

Shadows follow the shape, not the layout

A shadow takes the shape of the element's border box, including its rounded corners. And, like an outline, it takes no space in the layout: it can spill over neighbors, and adding one never moves anything. That makes shadows safe to animate on hover.

Two consequences to keep in mind:

  • A parent with overflow: hidden clips its children's shadows. If a card's shadow is cut off, check the containers around it.
  • A shadow is drawn only outside the box (for normal shadows). It never shows through a transparent background, so a shadow behind a semi-transparent element won't darken its interior.

Inset shadows

Add the inset keyword and the shadow is drawn inside the box, as if the element were pressed into the page. Offsets work in reverse visually: a positive y-offset darkens the top inner edge.

CSS
.input {
  box-shadow: inset 0 2px 4px rgb(0 0 0 / 0.08);
}

Inset shadows are drawn above the background but below the content. They are great for pressed buttons, wells, progress tracks and input fields.

Multiple shadows

Separate shadows with commas to stack them. The first shadow is drawn on top:

CSS
.card {
  box-shadow:
    0 1px 2px rgb(0 0 0 / 0.06),
    0 4px 12px rgb(0 0 0 / 0.08);
}

Real objects cast a sharp, dark shadow close to them and a large, faint one farther away. Layering a tight shadow with a soft one imitates that and looks far more natural than any single shadow.

Spread with zero blur draws solid rings, so multiple shadows can also act as extra "borders" that take no space:

Inset, layered and ring shadows

Compare the first two tiles. The single dark shadow looks like a sticker; the layered one looks like paper resting on a desk.

Building an elevation system

On a real site, you want just a handful of shadows, each meaning a level of height: a resting card, a hovered card, a dropdown, a modal. Store them as custom properties and use only those:

CSS
:root {
  --shadow-color: 220 40% 20%;
  --elevation-1: 0 1px 2px hsl(var(--shadow-color) / 0.08),
                 0 1px 3px hsl(var(--shadow-color) / 0.06);
  --elevation-2: 0 2px 4px hsl(var(--shadow-color) / 0.06),
                 0 6px 12px hsl(var(--shadow-color) / 0.08);
  --elevation-3: 0 4px 8px hsl(var(--shadow-color) / 0.06),
                 0 16px 32px hsl(var(--shadow-color) / 0.12);
}

.card          { box-shadow: var(--elevation-1); }
.card:hover    { box-shadow: var(--elevation-2); }
.dropdown      { box-shadow: var(--elevation-2); }
.dialog        { box-shadow: var(--elevation-3); }

Two tricks make this look better:

  • Tint your shadows. Pure black shadows look muddy on colored backgrounds. A dark, desaturated version of your background color (like the blue-gray above) looks richer.
  • Higher means softer and farther. As elevation increases, increase the y-offset and blur together, and keep the opacity low.
Elevation on hover

The card rises slightly and its shadow grows, so it really looks lifted. You will learn more about smooth changes like this in Transitions.

Performance. Animating box-shadow makes the browser repaint the shadow on every frame. For a few cards that is fine. For many elements, a common trick is to put the large shadow on a ::after pseudo-element and animate its opacity instead, which is much cheaper. See Motion, Performance and prefers-reduced-motion.

Shadows on dark backgrounds

On a dark page, dark shadows barely show. Designers usually signal elevation there by making higher surfaces slightly lighter, and keep a faint shadow, or a subtle light ring, to separate them:

CSS
.dark .card {
  background: #1e293b;
  box-shadow:
    0 0 0 1px rgb(255 255 255 / 0.06),
    0 8px 24px rgb(0 0 0 / 0.5);
}

Dark Mode covers this in more depth.

box-shadow always uses the rectangle (or rounded rectangle) of the box. To shadow the actual shape of a transparent PNG, an SVG icon or text, use filter: drop-shadow() from Filters, or text-shadow for text, covered in Text Shadows.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. In box-shadow: 2px 6px 10px 4px black, what is the 10px?

    The order is x-offset, y-offset, blur, spread, so the third length is the blur radius.
  2. 2. Which shadow looks pressed into the surface?

    The inset keyword draws the shadow inside the box, so the element looks sunken.
  3. 3. In a comma-separated list of shadows, which one is drawn on top?

    Shadows are painted in reverse order, so the first shadow in the list ends up on top.
  4. 4. A card's shadow is cut off at the edges. What is the most likely cause?

    Shadows overflow the element's box, so a clipping ancestor cuts them off. Negative blur isn't even allowed.
  5. 5. Why are layered shadows usually better than one big dark shadow?

    Real objects cast both a crisp contact shadow and a broad, faint one. Layering them looks natural; neither option affects layout.

Summary

  • box-shadow takes an x-offset, a y-offset, an optional blur and spread, a color and an optional inset.
  • Shadows follow the border box and its rounded corners, take no layout space and can be clipped by overflow.
  • inset shadows draw inside the box for pressed or sunken looks.
  • Comma-separated shadows stack, first on top; layering a tight and a soft shadow looks most natural.
  • Zero-blur spread shadows draw solid rings that act like borders without taking space.
  • Keep a small set of elevation levels in custom properties, with low opacity and tinted colors.

Next: Overflow, where you decide what happens when content doesn't fit its box.