日本語

CSS drop-shadow: Shadow That Follows a Transparent Image’s Shape

​​CSS​HTML,Playwright

When you apply box-shadow to a PNG with a transparent background, the shadow follows the rectangular box of the image. filter: drop-shadow() makes the shadow follow the non-transparent parts of the image. Use it when you want a cutout product photo, logo, icon, or character illustration to look like it floats above the background.

css
img {
  filter: drop-shadow(8px 8px 4px rgb(0 0 0 / 0.4));
}
Note

The test environment was Ubuntu 24.04, Node.js 22.23.4, Playwright 1.63.0, and headless Chromium 153. I drew a 200×200 PNG of a cat's face with a transparent background, placed it on a white page, took screenshots with Playwright, read the pixel colors at fixed positions, and compared them. I did not test Firefox or Safari.

box-shadow shadows the box, drop-shadow shadows the shape

Three rows comparing shadows. Row 1, with a Japanese label meaning "box-shadow on a transparent PNG", shows a rectangular shadow offset to the bottom right, outside a light blue cat-face image. Row 2, with a Japanese label meaning "drop-shadow on a transparent PNG", shows a shadow offset to the bottom right that follows the cat's ears and round face. Row 3, with a Japanese label meaning "drop-shadow on a JPEG with a white background", shows the cat's face inside a white rectangle, with a rectangular shadow offset to the bottom right of the whole white rectangle.
Chromium screenshots of box-shadow on a transparent PNG (top), drop-shadow on a transparent PNG (middle), and drop-shadow on a JPEG with a white background (bottom). The labels in the image are in Japanese and were added for the figure.

To make the difference clear, I applied a solid black shadow with no blur, offset 10px to the right and 10px down. The bottom edge of the cat's chin is 10px above the bottom edge of the image.

Location checked box-shadow: 10px 10px 0 #000 filter: drop-shadow(10px 10px 0 #000)
6px below the chin (inside the image box) rgb(255, 255, 255) rgb(0, 0, 0)
5px outside the bottom-right corner of the image box rgb(0, 0, 0) rgb(255, 255, 255)
Transparent top-left corner inside the image box rgb(255, 255, 255) rgb(255, 255, 255)

With box-shadow, the area below the chin stayed white, and a black shadow appeared outside the image box. Transparent parts inside the box got no shadow. With drop-shadow() it was the reverse: the shadow appeared below the chin, and the area outside the box corner stayed white.

The MDN page for drop-shadow() also explains that box-shadow creates a rectangular shadow behind the whole element box, while drop-shadow() creates a shadow that matches the image's own shape (its alpha channel).

Two things change when you convert from box-shadow

spread and inset are not allowed, and comma-separated lists are not allowed

drop-shadow() accepts only the horizontal and vertical offsets, the blur, and the color. It does not accept the box-shadow spread value or the inset keyword (from the syntax description on the MDN page for drop-shadow()).

If you write an invalid value, the whole declaration is ignored. In a stylesheet, I wrote a valid declaration followed by one with a spread value. getComputedStyle() still reported the earlier valid value for filter.

css
.photo {
  filter: drop-shadow(10px 10px 0 #000);
  /* spread (a fourth length) is not allowed, so this line is ignored */
  filter: drop-shadow(10px 10px 0 5px #000);
}

CSS.supports() returned false for a value with a spread and for a value with inset. A comma-separated list of two shadows, like box-shadow allows, also returned false and was ignored. To stack shadows, leave out the comma and separate the drop-shadow() functions with spaces (see the section "Stacking shadows also shadows the earlier shadow" below).

Blur values look the same at about half the box-shadow value

The same 20px value spreads differently in each case. I applied an unoffset black shadow to a 100×100 light blue square and read the colors moving outward from the right edge.

From the right edge box-shadow: 0 0 20px drop-shadow(0 0 20px) drop-shadow(0 0 10px)
0px rgb(132, 132, 132) rgb(130, 130, 130) rgb(133, 133, 133)
10px rgb(215, 215, 215) rgb(178, 178, 178) rgb(219, 219, 219)
20px rgb(250, 250, 250) rgb(217, 217, 217) rgb(252, 252, 252)
30px rgb(255, 255, 255) rgb(240, 240, 240) rgb(255, 255, 255)
40px rgb(255, 255, 255) rgb(251, 251, 251) rgb(255, 255, 255)

The drop-shadow(0 0 20px) shadow spread about twice as far as box-shadow. With drop-shadow(0 0 10px), the sequence of colors was almost the same as box-shadow: 0 0 20px.

MDN describes the third value of drop-shadow() as the standard deviation of a Gaussian blur. In the MDN page for box-shadow, the third value is the blur radius (<blur-radius>). When you convert from box-shadow, halving the blur value gives a similar look (see the table above). In the first image, the box-shadow blur is 8px, and the drop-shadow() blur is 4px.

If you omit the color, the shadow uses the text color

When you omit the color, as in drop-shadow(10px 10px), the shadow uses the element's color. For an image with color: #d85878, the shadow color was rgb(216, 88, 120), and getComputedStyle() returned drop-shadow(rgb(216, 88, 120) 10px 10px 0px). For the same image, box-shadow: 10px 10px 0 without a color also used rgb(216, 88, 120). This matches box-shadow. If you want a black or gray shadow, specify the color.

If the shadow is rectangular, check that the image background is transparent

drop-shadow() follows the shape because it detects the transparent parts of the image and builds the shadow from the rest. When I applied drop-shadow() to a JPEG with a white background, the white background was treated as part of the shape, and the shadow became rectangular (row 3 of the first image). In the pixel values, the area below the chin stayed white and the area outside the image box turned black, the same result as box-shadow.

To get a shadow that follows the shape, use an image with a transparent background, such as a transparent PNG. In this article, the shadow followed the shape only for the transparent PNG.

Semi-transparent parts also get a lighter shadow

drop-shadow() changes the shadow's density to match each pixel's opacity. I drew the cat's face in a PNG at 50% opacity and applied a black drop-shadow(10px 10px 0 #000).

Location checked Without shadow With drop-shadow
6px below the chin rgb(255, 255, 255) rgb(127, 127, 127)
Inside the face rgb(127, 227, 243) rgb(63, 163, 179)

The shadow below the chin was gray, not black. The shadow of a semi-transparent image is as faint as the image itself. The inside of the face was also darker than without the shadow. The shadow was drawn behind the semi-transparent image, and it showed through the image.

Stacking shadows also shadows the earlier shadow

When you separate drop-shadow() functions with spaces, each one applies in order from left to right. The second drop-shadow() treats the shadow created by the first one as part of the shape, as well as the original image.

css
img {
  filter: drop-shadow(10px 10px 0 red) drop-shadow(10px 10px 0 blue);
}

With this declaration, the area 6px below the chin was red (rgb(255, 0, 0)). The position 10px right and 9px down from there was blue (rgb(0, 0, 255)). Moving 10px left and 10px up from that position, the original image was transparent. Moving 20px left and 20px up put that position inside the cat's face. So the blue appears 20px away from the original image, and it is a shadow of the red shadow.

The MDN page for drop-shadow() includes an example of stacking several drop-shadow() functions on one element: four red shadows, each offset 1px diagonally in one of the four directions.

​Posted by 86