Thursday, December 3rd, 2009

Cool cross-browser styling with CSS and IE’s filter

A few new and cool CSS3 styles become realistic options when building a website if you combine them with IE’s filter.

View demo

A few new and cool CSS3 styles become realistic options if you are designing website. That is: as long as you combine them with IE’s counterpart filter.

The list of things that Safari, Chrome and Firefox support is getting larger and larger. Not all functionality is that interesting since we still have to make sure that IE users receive a functional site. And if you’re building something for a customer, you have to make sure that the website looks like what the customer expects it to look like. Most of the time that means that the website must be more or less an exact copy of the designs in Photoshop.

But what is easily forgotten is that for years Internet Explorer sort of supports some CSS styles through the proprietary filter property. I mostly use filter for setting opacity or forcing alpha channels in a PNG24, but when I saw a text shadow in a website I realized that IE can do that trick already for years. So I came up with this list of things that should work on almost any browser:

  • Text shadow
  • Box shadow
  • Blur
  • Gradient background
  • Opacity
  • PNG24 alpha

A little on browser support

A little warning about the “works in”-lines you’ll seewith each example. I didn’t dig very deep to see which version of Opera started to support text-shadow. If see in a support chart that Opera 9.62 supports text-shadow, I haven’t dug deeper to if it was also supported in Opera 9.61. Especially when I relate it to the small share of people who use that browser. If you can point a source that speaks of an earlier version, I’m more than happy to correct the article.

Things you can do with CSS and filters combined

Text shadow

Works in IE 5.5+, Firefox 3.1b+, Safari 3+, Chrome 2+ and Opera 9.62+
Used sparingly can create a nice subtle effect and is a great way to make something a bit more stylish. Watch out with loss of anti-alias. More on that issue at the end of the article.

Text-shadow in IE8

Text-shadow in IE8

Text-shadow in Firefox 3.5.5

Text-shadow in Firefox 3.5.5

.textShadow {
	width: 250px;
	text-shadow: 1px 1px 1px #cccccc; /* CSS3-compatible browsers */
	filter: dropshadow(color=#cccccc, offx=1, offy=1) /* IE */
}

Blur

Works in IE 5.5+, Firefox 3.1b+, Safari 3+, Chrome 2+ and Opera 9.62+
This one is a bit of hack: CSS3’s text-shadow is misused to create a blur. It was a trick I saw on www.css-tricks.com (hence the name, I guess). And although you can see the differences between the two screenshots, both are definitely blurs. A conditional comment was used to make the text in IE a bit darker so it would match with other browsers.

Blur in IE8

Blur in IE8

Blur in Firefox 3.5.5

Blur in Firefox 3.5.5

.blur {
	width: 250px;
	color: #ddd;
	text-shadow: 0 0 2px #aaaaaa;
	text-shadow: 0 0 4px #aaaaaa;
	text-shadow: 0 0 6px #aaaaaa;
	filter:
		progid:DXImageTransform.Microsoft.MotionBlur(strength=1,direction=310)
		progid:DXImageTransform.Microsoft.Blur(pixelradius=2);
}
<!--[if IE]>
	<style type="text/css">
		/* This style is for IE browsers only by
		   using IE's Conditional Comments */
		.blur {
			color: #aaaaaa;
		}
	</style>
<![endif]-->

Opacity

Works in IE 5.5+ and in all major browsers for years and is safe to use
Definitely the most used combination of CSS and IE’s filter. I mostly use it to make the website almost invisible in favor of an absolute placed div which floats like popup window on top of the greyed out website.

Opacity 0.5 in IE8

Opacity 0.5 in IE8

Opacity 0.5 in Firefox 3.5.5

Opacity 0.5 in Firefox 3.5.5

.opacity {
	width: 250px;
	opacity: 0.5;
	-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=50)";/*IE8+*/
	filter: alpha(opacity=50); /* IE7 and lower */
}

Gradient

Works in IE 5.5+, Firefox 3.6+, Safari4, Chrome 3+
The soon to be released Firefox 3.6 supports gradient background. Safari and Chrome already supports that cool CSS3 feature. Although you can do more with CSS3’s background gradient than IE’s filter, there’s nothing to stop you from using a simple gradient. Well, poor browser support may stop you. Firefox 3.6 still has to be released and Opera does not seem to support gradients at all.

So the browser support for gradients is less than desirable. But since all major browsers except Opera now supports gradients in their latest versions, it will become a useful styling tool soon.

Background gradient in IE8

Background gradient in IE8

Background gradient in Chrome 3.0

Background gradient in Chrome 3.0

.gradient {
	background: -moz-linear-gradient(top, bottom,from(#cccccc),to(#888888));
	background: -webkit-gradient(linear, left top, left bottom, from(#cccccc), to(#888888)) no-repeat;
	background; -o-gradient(top, bottom,from(#cccccc),to(#888888));
	-ms-filter: "progid:DXImageTransform.Microsoft.gradient (GradientType=0, startColorstr=#ffcccccc, endColorstr=#ff888888)"; /* IE8+ */
	filter: progid:DXImageTransform.Microsoft.gradient (GradientType=0, startColorstr=#ffcccccc, endColorstr=#ff888888); /* IE7 and lower */
}

Box shadow

Works in IE 5.5+, Firefox 3.6+, Safari 3, Chrome 1
If you look at the screenshots below you see a striking difference in what is called a “Shadow“. IE’s box shadow looks more like the right and bottom side of a 3D object than a real life shadow. CSS3’s box-shadow provides a shadow that’s much more realistic.

Box shadow in IE8Box shadow in Firefox 3.5.5

.boxShadow {
	-moz-box-shadow: 5px 5px 5px #888;
	-webkit-box-shadow: 5px 5px 5px #888;
	-ms-filter: "progid:DXImageTransform.Microsoft.Shadow(color=#888888,direction=125,strength=5)"; /* IE8+ */
	filter: progid:DXImageTransform.Microsoft.Shadow(color=#888888,direction=125,strength=5); /* IE7 and lower */
	background: #cccccc;
}

Use alpha channel in PNG24 images

Not really a CSS feature, neither something that lacks in modern IE browsers. But it is dearly missed in IE6. The only way to use alpha channels in IE6 (although many have found different ways apply it) is by using the filter’s AlphaImageLoader. Although this filter can save the day, it has many bugs you need to be aware of:

  • It can only be used as a background image.
  • You cannot position it like a normal background image, nor can the image repeat itself on the x or y axis.
  • Links or form elements inside a box with a transparent PNG24 are not clickable. You have to make sure that such elements have a parent that has position: relative.
  • Having lots of relative positioned elements scattered around can add extra complexity to your website if it has absolute positioned items floating on top of each other as well.
  • You’ll lose text anti-alias.

This technique is only needed in IE6. Other modern browsers use the alpha channel as it ought to be.

.imageAlpha {
	display: block;
	width: 250px;
	height: 40px;
	padding: 5px 10px;
	background: #aaf url(alpha.png) 0 0 repeat-x; /* PNG24 image with alpha layer that works in all modern browsers */
}
* html .imageAlpha { /* these styles are only picked up by IE6 */
	background-image: none;
	filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='alpha.png', sizingMethod='scale')
}

Filter bugs

Filter can clear Cleartype

The main drawback of using IE’s filter is that in some cases the element that has a filter style in IE, will lose it’s anti-aliasing. That may not sound like a big deal, because we have lived for a long time without Microsoft’s Cleartype, but it can get real ugly with larger fonts as you can see in the screenshots below. If you use a filter and it does remove the Cleartype, it’s best to keep the font-size 16 pixels or lower.

IE filters in Arial 16pxIE filters in Georgia 19px

Filters can only be applied to boxes

Filters can only be applied to block elements like div or p and they need a width or height. If not, the filter won’t work. The exception to the rule is IE8, which can show filter effects without a width or height.

View demo

Bookmark and Share

Categories & tagging

Category: CSS
Tags: , , ,
5 Responses
Follow responses to this entry through this RSS.
  1. senshikaze

    so… i need to bend over ass-backwards to make ie look like a real browser?
    nice.
    How bout people upgrade off of “my first browser” and onto a real one?

  2. R Hampton

    I’m getting a strange problem in IE7 but not IE6. The shadow filter renders my white text with a thin dark outline on letters like “O” ). I’ve tried turning ClearType on and off within IE7 but I can’t see a difference. Is it a problem with just my browser or with all IE7 browsers?

    FYI – I have a DIV with background-color:#bee48a; inside that is an unordered list containing A HREF tags with color: #fff; text-transform: uppercase; text-decoration: none; text-shadow: #693 0.1em 0.1em 0.2em; filter: progid:DXImageTransform.Microsoft.Shadow(color=’#669933′, direction=135, strength=2);

  3. Wouter Bos

    Hi,

    I did a quick test in IE8 and I have the same problem. The only solution I see is keeping the font-size at 16px or lower. And don’t use font-weight: bold either, since that seems to trigger the bug too. Than you shouldn’t have any problem.

  4. Chrissteven81

    senshikaze- You are bending over backwards for other browsers IE has very nice ways of doing it far more clean. more like we are bending over backwards for other browsers lol.

  5. Ali

    IMPORTANT::
    To solve the problem of fuzzy text in IE when using a filter, wrap the content of filtered element and set it’s position style to relative.
    EXAMPLE:

    CONTENT

Leave a response