Thursday, December 6, 2007

PNG FIX FOR XHTML DIV'S BACKGROUND

AN UNUSEFUL INTRODUCTION
Many word have been written over the problem of the *.png's transparency visualization in IE<7 versions. Now I'll drop some lines too. In particular, I found it interesting to try to fix this problem when you need to put transparent images in some DIV's background.



THE PROBLEM NOWADAYS The most well-known method to solve the similar problem with the *.png's transparency in the IMG tag is to use the javscript published here. It is a quite simple script, which simply searches for all of the IMG tags with an ID assigned, get their SRC property, and add a particular STYLE attribute.



A DIFFERENT POINT OF VIEW
To let IE previous than the 7 version display *.png's with transparency correctly, this script simply adds one non-standard line to your CSS definitions. This is it.

filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='image.png', sizingMethod='scale');

This is a non-standard patch to the CSS 2.1, created by Microsoft for their browsers only. You can check out the official documentation in the MS KB294714.

What I am about to propose is a different approach to the problem, completely dedicated to the DIV tag, with no javascript usage at all. But, how is it possible to distinguish browsers without server-side or client-side scripts?



THE PROBLEM OF DISTINGUISHING BROWSERS
Obviously, it is not a solution to simply use both the MS special CSS property and the standard

background:url('image.png');

attribute, because the second one will be red even by MSIE, vanishing what previously done. But never mind! There is almost one easy trick to solve this problem.



THE SOLUTION
Probably the easiest way is to simply modifify your style sheet and add the standard and the MS proprietary attributes to the CSS in two separated selectors. Only one of them should be understood by MSIE. To be more clear, let me give you an example:

/* this will be understood by all of the browsers */
.menu_holder
{
width:20px;
height:80px;
background-position:center;
filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='image.png', sizingMethod='scale');
}

/* this will be understood by all of the non-IE browsers */
html > body .menu_holder
{
background:url("image.png");
}

To be honest I prefer to use a PHP style sheet parser and the NET_USERAGENT_DETECT PEAR class, but I am a quite strange person...

Doing what's explained in this page should solve all of your div's background trasparency troubles in Internet Explorer.

1 comment:

Anonymous said...

Anonymous