demosthenes.info

I’m Dudley Storey, the author of Pro CSS3 Animation. This is my blog, where I talk about web design and development with , and . To receive more information, including news, updates, and tips, you should follow me on Twitter or add me on Google+.

web developer guide

my books

Book cover of Pro CSS3 AnimationPro CSS3 Animation, Apress, 2013

my other blogs

Massive Head Canon: Intelligent discussion of movies, books, games, and technology.

my projects

A Sass color keyword system for designers. Replaces CSS defaults with improved hues and more memorable, relevant color names.

CSSslidy: an auto-generated #RWD image slider. 3.8K of JS, no JQuery. Drop in images, add a line of CSS. Done.

tipster.ioAutomatically provides local tipping customs and percentages for services anywhere.

Wall textureRobbie the Robot with a transperant background
Robby the robot

Creating a True Cross-Browser Drop Shadow Effect With CSS & SVG

css / shadows

Estimated reading time: 2 minutes, 26 seconds

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

IE

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

.shadowed {
filter: "progid:DXImageTransform.Microsoft.Dropshadow(OffX=12, OffY=12,
Color='#444')";
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. Check out this example on CodePen

comments powered by Disqus

This site helps millions of visitors while remaining ad-free. For less than the price of a cup of coffee, you can help pay for bandwidth and server costs while encouraging further articles.