XML technologies / CSS / CSS Cookbook / Images / Using multiple PNGs with transparent alpha channels
(Excerpt from “CSS Kochbuch” by Christopher Schmitt)
Problem
Include several PNG files with transparent alpha channels in your pages.
Solution
Use Drew McLellan’s updated Sleight script to enable transparent alpha channels in Internet Explorer versions 5.5 through 6.
Save this code in your own JavaScript file or download it directly from McLellan’s website.
if (navigator.platform == "Win32" &&
navigator.appName == "Microsoft Internet Explorer" &&
window.attachEvent) {
window.attachEvent("onload", fnLoadPngs);
}
function fnLoadPngs( ) {
var rslt = navigator.appVersion.match(/MSIE (\d+\.\d+)/, '');
var itsAllGood = (rslt != null && Number(rslt[1]) >= 5.5);
for (var i = document.all.length - 1, obj = null;
(obj = document.all[i]); i--) {
if (itsAllGood &&
obj.currentStyle.backgroundImage.match(/\.png/i) != null) {
this.fnFixPng(obj);
obj.attachEvent("onpropertychange",
this.fnPropertyChanged);
}
}
}
function fnPropertyChanged( ) {
if (window.event.propertyName == "style.backgroundImage") {
var el = window.event.srcElement;
if (!el.currentStyle.backgroundImage.match(/x\.gif/i)) {
var bg = el.currentStyle.backgroundImage;
var src = bg.substring(5,bg.length-2);
el.filters.item(0).src = src;
el.style.backgroundImage = "url(x.gif)";
}
}
}
function fnFixPng(obj) {
var bg = obj.currentStyle.backgroundImage;
var src = bg.substring(5,bg.length-2);
obj.style.filter =
"progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"
+ src + "', sizingMethod='scale')";
obj.style.backgroundImage = "url(x.gif)";
}Include the JavaScript file in the web page by inserting the following code into its section head:
<script src="/_assets/js/bgsleight.js" type="text/javascript"></script>Note:
You will probably need to adapt the JavaScript file path. Here, the file is in the folder js, which is inside the folder _assets.
The one-pixel GIF file, called x.gif in the script, must also exist on your web server, and its path must be adjusted in the script.
Discussion
Transparent alpha channels have become almost standard in modern browsers. Native PNG support is available in Netscape Navigator 6+, Firefox 1.5+, Opera, Safari and Internet Explorer 7 for Windows, for example. Unfortunately, the still widely used Internet Explorer 6 for Windows does not yet offer it.
To work around this, Aaron Boodman created JavaScript using Microsoft’s proprietary property filter to support PNGs with transparent alpha channels in Internet Explorer 5.5 through 6 for Windows. Browsers with native PNG support are unaffected.
Drew McLellan refined Boodman’s method so the JavaScript works for background images as well as images embedded in HTML (see Drew McLellan: Sleight of hand).
McLellan’s JavaScript runs when the page loads. It searches the HTML source for elements img referencing images with the extension png.
When the code finds such a tag img, it changes the HTML dynamically, first replacing the original PNG with a transparent one-pixel GIF.
Next, the PNG file is given Internet Explorer’s property filter to enable transparent alpha channels in the browser. This works only when the PNG is defined as a background image.
The PNG therefore appears on the page as the transparent GIF’s background image.
Note:
When creating the page, be sure to define the PNG’s width and height in CSS with the properties width and height. Otherwise the script will not run correctly.
Copyright of the German edition © 2007 by O’Reilly Verlag GmbH & Co. KG
You may print the online version for your private use.
Otherwise, this chapter from “CSS Kochbuch” is subject to the same conditions as the bound edition: the work, including all its parts, is protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
O’Reilly Verlag GmbH & Co. KG, Balthasarstr. 81, 50670 Köln