Box Shadows and Filters

Intermediate · 10 min

How box-shadow's five numbers work, how to layer shadows so they look physical, and when filter, drop-shadow() and backdrop-filter are the better tool.

Before thisBorders, Radius and Outlines

Kevin Powell · 10 min

Box Shadows and Filters

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Kevin PowellReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • `box-shadow: x y blur spread color` paints a blurred copy of the element's box behind it. It never affects layout, and it follows `border-radius`.
  • Shadows stack: a comma-separated list is painted first-on-top. Several soft, low-opacity shadows of growing size look more physical than one hard shadow.
  • `inset` flips a shadow to the inside of the box, which is how pressed buttons and recessed fields are drawn.
  • `filter: drop-shadow()` shadows the visible shape (alpha channel) of the element, so it works on transparent PNGs, SVGs, clipped shapes and speech-bubble tails, where `box-shadow` can only shadow the rectangle. It has no `spread` or `inset`.
  • `filter` functions chain left to right (`blur`, `brightness`, `grayscale` and others), and `backdrop-filter` applies them to whatever is behind the element, so it needs a semi-transparent background to show.

What a shadow is

A shadow in CSS is a copy of the element’s box, offset, blurred and painted behind it. That is all. It doesn’t take up space or push neighbors around. It also doesn’t change what the mouse hits. It is paint, laid down in the area around the element, which is also why it follows border-radius: it copies the rounded box.

.card {
  box-shadow: 0 4px 12px 0 rgb(0 0 0 / 0.15);
  /*          x y  blur  spread color */
}

The values are horizontal offset, vertical offset, blur radius, spread and color. A positive offset moves the shadow right and down. Spread grows or shrinks the copy before blurring. A negative spread with a positive offset gives the “shadow only on one side” look, since the shrunken copy hides behind the box on the other sides. inset puts the shadow inside the box, over the background but under the content, which draws a pressed or recessed surface.

Layering for realism

One shadow looks flat because real light does not behave like a single blur. A real shadow is dark and tight near the object, then fades gradually. Stacking layers imitates that:

.card {
  box-shadow:
    0 1px 2px rgb(0 0 0 / 0.08),
    0 4px 8px rgb(0 0 0 / 0.08),
    0 16px 32px rgb(0 0 0 / 0.1);
}

The shadows are separated by commas, the first on top. Each layer roughly doubles in offset and blur, with low opacity so they add up rather than muddying. A useful habit is to keep shadow colors a transparent version of the page’s hue, not pure black, so they sit naturally on colored backgrounds.

A shadow can also act as a border that follows the curve: box-shadow: 0 0 0 2px red is a ring with no blur, and it ignores layout. Prefer outline for focus, since a shadow vanishes in forced-colors modes (see borders, radius and outlines).

drop-shadow(): shadow the shape, not the box

box-shadow always shadows the rectangle (rounded if you set a radius). Put it on a transparent PNG, an SVG logo, or a speech bubble with a tail, and you get a rectangle where you wanted the outline of the image. The filter property with drop-shadow() shadows whatever pixels are painted, using the alpha channel:

.logo   { filter: drop-shadow(0 4px 6px rgb(0 0 0 / 0.3)); }
.bubble { filter: drop-shadow(0 2px 4px rgb(0 0 0 / 0.2)); }  /* includes the ::after tail */

The trade-offs: drop-shadow() takes x, y, blur and color, but has no spread and no inset. It also runs on the rendered result of the element and its children, so it does the right thing after a clip-path, where box-shadow is cut off.

Other filter functions

filter applies image-processing steps to the element and everything in it: blur(), brightness(), contrast(), grayscale(), hue-rotate(), invert(), opacity(), saturate() and sepia(). They chain in order, so filter: grayscale(1) contrast(1.2) is not the same as the reverse. A common pattern is dimming a card until hover:

.thumb { filter: grayscale(1) brightness(0.9); transition: filter 0.2s; }
.thumb:hover { filter: none; }

Two side effects to know. A non-none filter creates a stacking context, which changes how z-index behaves inside it. It also becomes the containing block for position: fixed descendants. In a Chromium test, a position: fixed; left: 0; top: 0 child inside a filter: blur(0px) parent at 100px, 100px rendered at 100px, 100px, not at the viewport corner.

backdrop-filter: filter what’s behind

filter changes the element. backdrop-filter changes what shows through it, which is how frosted glass is made:

.glass {
  background: rgb(255 255 255 / 0.2);
  backdrop-filter: blur(12px) saturate(1.4);
}

It requires a background that isn’t fully opaque, or there is nothing to see through. It reached all major browsers in September 2024, so as of 2026 it is newly available, not old. Large blurred areas are costly to paint, which matters for the animation lessons later.

What the video covers

Kevin Powell’s “Why are there two ways to make shadows in CSS? box-shadow vs filter: drop-shadow()” is a nine and a half minute video from May 2020. It compares box-shadow with filter: drop-shadow(), and its description points to MDN for the other filter properties. I could not read a transcript, so the examples are not described. This is the oldest pick in the group, but the behavior it describes has not changed. It does not cover layered shadows or backdrop-filter, so those parts of this lesson are the prose’s own.

Where this fits

Shadows follow the box you shaped in borders, radius and outlines. The next lesson, clip-path, masks and blend modes, clips an element’s paint (shadows included), which is exactly when drop-shadow() earns its place. Filters and shadows also create or interact with stacking contexts, and animation performance covers why animating a blur is expensive.

Check yourself

  1. 01You put `box-shadow` on an element with `clip-path: polygon(...)` and the shadow is gone. Why, and what do you do instead?

    `clip-path` clips everything the element paints, including its own box shadow. Put the shadow on a wrapper with `filter: drop-shadow(...)`, so the shadow is applied after the child has been clipped and follows the clipped shape.

  2. 02What are the five values in `box-shadow: 4px 8px 12px 2px rgb(0 0 0 / 0.3)`?

    Horizontal offset 4px, vertical offset 8px, blur radius 12px, spread 2px (grows the shadow's box before blurring) and a color at 30% opacity. Offsets can be negative; spread can be too.

  3. 03Why do realistic UI shadows usually use two or three layers?

    A real shadow has a tight dark core near the object and a wide soft falloff farther away. One blur value can't do both, so you stack a small, sharper shadow and a large, fainter one.

  4. 04A frosted-glass panel uses `backdrop-filter: blur(10px)` but nothing looks blurred. What's missing?

    The panel's own background is probably opaque. `backdrop-filter` blurs what is behind the element, which is only visible through a transparent or semi-transparent background such as `rgb(255 255 255 / 0.2)`.

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark