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 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" 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"/>

<feMerge>

<feMergeNode/>

<feMergeNode in="SourceGraphic"/>

</feMerge>

</filter>

</svg>

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.

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 CSS3/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: