Add a Shadow to Images with a Transparent Background
Topic: CSS
Published on Updated
Typically when I need to add a shadow to something with CSS, I reach for the box-shadow
CSS property. Though, this doesn’t have the desired effect when applying it to an image with a transparent background.
For example:
|
|
Will result in an image that looks like this:
INSERT IMAGE HERE
The shadow is placed behind the full image, showing a rectangular shape. It’s not doing what we expect it to do.
It is possible to add a shadow to the image contents, though! You can use the CSS filter
property to apply a drop shadow to the image:
|
|
Will result in an image that looks like this:
INSERT IMAGE HERE
References
- I found out about this through a tweet by @JoshWComeau.
🔥 `box-shadow` doesn't work well with transparent images. `filter: drop-shadow` does what you'd expect, adding a shadow to the image's contents.
— Josh W. Comeau (@JoshWComeau) July 30, 2020
.my-img {
filter: drop-shadow(1px 2px 3px black);
}
CSS filters are so rad 🥰
Pen: https://t.co/rVKgSlijt1 pic.twitter.com/6rwc7gG1Ro - MDN Docs for
box-shadow
. - MDN Docs for
filter: dropshadow()
. - I took the
box-shadow
styles from the TailwindCSS docs.