Wall textureRobbie the Robot with a transperant background
Robby the robot

Recently I discussed the drop-shadow filter. Right now we're in an interesting place with respect to the filter: Chrome, Safari and the latest versions of Firefox support the filter, while older versions of Mozilla's browser use an SVG equivalent, and IE9 and earlier use a proprietary Microsoft version.

In this article I’m going to show you how to write the effect for all browsers, so you can achieve the benefits of a true, dynamic drop shadow for all elements.

The CSS version first, as a refresher:

-webkit-filter: drop-shadow(12px 12px 7px rgba(0,0,0,0.5)); filter: drop-shadow(12px 12px 7px rgba(0,0,0,0.5));

The syntax is very straightforward: the values for the filter represent, in order, horizontal offset, vertical offset, blur and the color of the shadow (given as an rgba value to create a realistic shadow against any background).

Firefox (pre v35)

Now the SVG version. You can either save this as a separate file (shadow.svg) or embed it on a web page. The code is as follows:

<svg height="0" width="0" xmlns="http://www.w3.org/2000/svg">
	<filter id="drop-shadow">
		<feGaussianBlur in="SourceAlpha" stdDeviation="2.2"/>
		<feOffset dx="12" dy="12" result="offsetblur"/>
		<feFlood flood-color="rgba(0,0,0,0.5)"/>
		<feComposite in2="offsetblur" operator="in"/>
			<feMergeNode in="SourceGraphic"/>

The SVG syntax is considerably more complex, and I’m not going to explain all of it here. The good news is that you only need to alter four values: stdDeviation is the amount of blur; dx is the horizontal offset and dy the vertical, with flood-color being the color of the shadow.


Finally, the equivalent for Internet Explorer 9 and earlier:

/* for IE 8 & 9 */
	-ms-filter: "progid:DXImageTransform.Microsoft.Dropshadow(OffX=12, OffY=12, Color='#444')";
	/* For IE 5.5 - 7 */
	filter: "progid:DXImageTransform.Microsoft.Dropshadow(OffX=12, OffY=12, Color='#444')";

You’ll find that the visual quality is not nearly as good in IE – and you do have to do some re-jiggering and testing to translate the CSS/SVG values into legacy DX filters – but the dropshadow will appear at least appear in Internet Explorer.

Bringing It All Together

I’d suggest combining all these approaches as a class, as you’d usually want to apply the effect to more than one element on the same page… and naturally, you’d want all of the shadows on the page to fall in the same direction, to ensure a consistent visual appearance:

.shadowed {
	filter: "progid:DXImageTransform.Microsoft.Dropshadow(OffX=12, OffY=12,
	filter: url(#drop-shadow);
	-webkit-filter: drop-shadow(12px 12px 7px rgba(0,0,0,0.5));
	filter: drop-shadow(12px 12px 7px rgba(0,0,0,0.5));

(If you embedded the SVG code directly on the page, you’d use just the id value: note that it matches the id on the SVG code sample).

That’s it! You can now apply the class to a transparent PNG or any other element to gain a true drop-shadow effect across modern browsers.

Enjoy this piece? I invite you to follow me at twitter.com/dudleystorey to learn more.
Check out the CodePen demo for this article at https://codepen.io/dudleystorey/pen/EaMQBj