<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>

<channel>
	<title>Awethentik Blog</title>
	<atom:link href="http://www.awethentik.com/blogs/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.awethentik.com/blogs</link>
	<description>The Perfect Balance of Web Design + Development</description>
	<pubDate>Wed, 02 Jul 2008 20:50:38 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5.1</generator>
	<language>en</language>
			<item>
		<title>Multi-Platform/Cross-Browser Transparent PNGs</title>
		<link>http://www.awethentik.com/blogs/2008/07/multi-platform-cross-browser-transparent-pngs/</link>
		<comments>http://www.awethentik.com/blogs/2008/07/multi-platform-cross-browser-transparent-pngs/#comments</comments>
		<pubDate>Wed, 02 Jul 2008 20:34:59 +0000</pubDate>
		<dc:creator>awethentik</dc:creator>
		
		<category><![CDATA[Web Development]]></category>

		<category><![CDATA[alpha-channel]]></category>

		<category><![CDATA[alphaimageloader]]></category>

		<category><![CDATA[png]]></category>

		<category><![CDATA[sleight]]></category>

		<category><![CDATA[super sleight]]></category>

		<category><![CDATA[transparency]]></category>

		<category><![CDATA[transparent png]]></category>

		<guid isPermaLink="false">http://www.awethentik.com/blogs/?p=3</guid>
		<description><![CDATA[Newer breeds of browser such as Firefox and Safari have offered support for PNG images with full alpha channel transparency for a few years. With the use of hacks, support has been available in Internet Explorer 5.5 and 6, but the hacks are non-ideal and have been tricky to use. With IE7 winning masses of [...]]]></description>
			<content:encoded><![CDATA[<p>Newer breeds of browser such as Firefox and Safari have offered support for <span class="caps">PNG</span> images with full alpha channel transparency for a few years. With the use of hacks, support has been available in Internet Explorer 5.5 and 6, but the hacks are non-ideal and have been tricky to use. With IE7 winning masses of users from earlier versions over the last year, full <span class="caps">PNG</span> alpha-channel transparency is becoming more of a reality for day-to-day use.</p>
<p>However, there are still numbers of IE6 users out there who we can’t leave out in the cold this Christmas, so in this article I’m going to look what we can do to support IE6 users whilst taking full advantage of transparency for the majority of a site’s visitors.</p>
<h3>So what’s alpha channel transparency?</h3>
<p>Cast your minds back to the Ghost of Christmas Past, the humble <span class="caps">GIF</span>. Images in <span class="caps">GIF</span> format offer transparency, but that transparency is either on or off for any given pixel. Each pixel’s either fully transparent, or a solid colour. In <span class="caps">GIF</span>, transparency is effectively just a special colour you can chose for a pixel.</p>
<p>The <span class="caps">PNG</span> format tackles the problem rather differently. As well as having any colour you chose, each pixel also carries a separate channel of information detailing how transparent it is. This <a href="http://www.w3.org/TR/PNG-DataRep.html#DR.Alpha-channel">alpha channel</a> enables a pixel to be fully transparent, fully opaque, or critically, <em>any step in between</em>.</p>
<p>This enables designers to produce images that can have, for example, soft edges without any of the ‘halo effect’ traditionally associated with <span class="caps">GIF</span> transparency. If you’ve ever worked on a site that has different colour schemes and therefore requires multiple versions of each graphic against a different colour, you’ll immediately see the benefit.</p>
<p>What’s perhaps more interesting than that, however, is the extra creative freedom this gives designers in creating beautiful sites that can remain web-like in their ability to adjust, scale and reflow.</p>
<h3>The Internet Explorer problem</h3>
<p>Up until IE7, there has been no fully native support for <span class="caps">PNG</span> alpha channel transparency in Internet Explorer. However, since IE5.5 there has been some support in the form of proprietary filter called the AlphaImageLoader. Internet Explorer filters can be applied directly in your <span class="caps">CSS</span> (for both inline and background images), or by setting the same <span class="caps">CSS</span> property with JavaScript.</p>
<p><strong><span class="caps">CSS</span>:</strong></p>
<ol class="code">
<li><code>img {</code></li>
<li class="tab1"><code>filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(...);</code></li>
<li><code>}</code></li>
<li class="source">Source:  <a title="Download the above code as a textfile" href="/code/supersleight-transparent-png-in-ie6/1.txt">/code/supersleight-transparent-png-in-ie6/1.txt</a></li>
</ol>
<p><strong>JavaScript:</strong></p>
<ol class="code">
<li><code>img.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(...)";</code></li>
<li class="source">Source:  <a title="Download the above code as a textfile" href="/code/supersleight-transparent-png-in-ie6/2.txt">/code/supersleight-transparent-png-in-ie6/2.txt</a></li>
</ol>
<p>That may sound like a problem solved, but all is not as it may appear. Firstly, as you may realise, there’s no <span class="caps">CSS</span> property called <code>filter</code> in the <a href="http://www.w3.org/TR/CSS21/propidx.html">W3C <span class="caps">CSS</span> spec</a>. It’s a proprietary extension added by Microsoft that could potentially cause other browsers to reject your entire <span class="caps">CSS</span> rule.</p>
<p>Secondly, AlphaImageLoader does not magically add full <span class="caps">PNG</span> transparency support so that a <span class="caps">PNG</span> in the page will just start working. Instead, when applied to an element in the page, it draws a new rendering surface in the same space that element occupies and loads a <span class="caps">PNG</span> into it. If that sounds weird, it’s because that’s precisely what it is. However, by and large the result is that <span class="caps">PNG</span>s with an alpha channel can be accommodated.</p>
<h3>The pitfalls</h3>
<p>So, whilst support for <span class="caps">PNG</span> transparency in IE5.5 and 6 is possible, it’s not without its problems.</p>
<h4>Background images cannot be positioned or repeated</h4>
<p>The AlphaImageLoader does work for background images, but only for the simplest of cases. If your design requires the image to be tiled (<code>background-repeat</code>) or positioned (<code>background-position</code>) you’re out of luck. The AlphaImageLoader allows you to set a <code>sizingMethod</code> to either <code>crop</code> the image (if necessary) or to <code>scale</code> it to fit. Not massively useful, but something at least.</p>
<h4>Delayed loading and resource use</h4>
<p>The AlphaImageLoader can be quite slow to load, and appears to consume more resources than a standard image when applied. Typically, you’d need to add thousands of <span class="caps">GIF</span>s or <span class="caps">JPEG</span>s to a page before you saw any noticeable impact on the browser, but with the AlphaImageLoader filter applied Internet Explorer can become sluggish after just a handful of alpha channel <span class="caps">PNG</span>s.</p>
<p>The other noticeable effect is that as more instances of the AlphaImageLoader are applied, the longer it takes to render the <span class="caps">PNG</span>s with their transparency. The user sees the <span class="caps">PNG</span> load in its original non-supported state (with black or grey areas where transparency should be) before one by one the filter kicks in and makes them properly transparent.</p>
<p>Both the issue of sluggish behaviour and delayed load only really manifest themselves with volume and size of image. Use just a couple of instances and it’s fine, but be careful adding more than five or six. As ever, test, test, test.</p>
<h4>Links become unclickable, forms unfocusable</h4>
<p>This is a big one. There’s a bug/weirdness with AlphaImageLoader that sometimes prevents interaction with links and forms when a <span class="caps">PNG</span> background image is used. This is sometimes reported as a <code>z-index</code> issue, but I don’t believe it is. Rather, it’s an artefact of that weird way the filter gets applied to the document almost outside of the normal render process.</p>
<p>Often this can be solved by giving the links or form elements <code>hasLayout</code> using <code>position: relative;</code> where possible. However, this doesn’t always work and the non-interaction problem <strong>cannot always be solved</strong>. You may find yourself having to go back to the drawing board.</p>
<h3>Sidestepping the danger zones</h3>
<p>Frankly, it’s pretty bad news if you design a site, have that design signed off by your client, build it and then find out only at the end (because you don’t know what might trigger a problem) that your search field can’t be focused in IE6. That’s an absolute nightmare, and whilst it’s not likely to happen, it’s possible that it might. It’s happened to me. So what can you do?</p>
<p>The best approach I’ve found to this scenario is</p>
<ol>
<li>Isolate the <span class="caps">PNG</span> or <span class="caps">PNG</span>s that are causing the problem. Step through the <span class="caps">PNG</span>s in your page, commenting them out one by one and retesting. Typically it’ll be the nearest <span class="caps">PNG</span> to the problem, so try there first. Keep going until you can click your links or focus your form fields.</li>
<li>This is where you really need luck on your side, because you’re going to have to fake it. This will depend on the design of the site, but some way or other create a replacement <span class="caps">GIF</span> or <span class="caps">JPEG</span> image that will give you an acceptable result. Then use <a href="http://www.quirksmode.org/css/condcom.html">conditional comments</a> to serve that image to only users of IE older than version 7.</li>
</ol>
<p>A hack, you say? Well, you started it chum.</p>
<h3>Applying AlphaImageLoader</h3>
<p>Because the <code>filter</code> property is invalid <span class="caps">CSS</span>, the safest pragmatic approach is to apply it selectively with JavaScript for only Internet Explorer versions 5.5 and 6. This helps ensure that by default you’re serving standard <span class="caps">CSS</span> to browsers that support both the <span class="caps">CSS</span> and <span class="caps">PNG</span> standards correct, and then selectively patching up only the browsers that need it.</p>
<p>Several years ago, <a href="http://youngpup.net">Aaron Boodman</a> wrote and released a script called <a href="http://youngpup.net/projects/sleight/">sleight</a> for doing just that. However, sleight dealt only with images in the page, and not background images applied with <span class="caps">CSS</span>. Building on top of Aaron’s work, I hacked sleight and came up with <a href="http://allinthehead.com/retro/289/sleight-update-alpha-png-backgrounds-in-ie">bgsleight</a> for applying the filter to background images instead. That was in 2003, and over the years I’ve made a couple of improvements here and there to keep it ticking over and to resolve conflicts between sleight and bgsleight when used together. However, with alpha channel <span class="caps">PNG</span>s becoming much more widespread, it’s time for a new version.</p>
<h3>Introducing SuperSleight</h3>
<p>SuperSleight adds a number of new and useful features that have come from the day-to-day needs of working with <span class="caps">PNG</span>s.</p>
<ul>
<li>Works with both inline and background images, replacing the need for both sleight and bgsleight</li>
<li>Will automatically apply <code>position: relative</code> to links and form fields if they don’t already have <code>position</code> set. (Can be disabled.)</li>
<li>Can be run on the entire document, or just a selected part where you know the <span class="caps">PNG</span>s are. This is better for performance.</li>
<li>Detects background images set to <code>no-repeat</code> and sets the <code>scaleMode</code> to <code>crop</code> rather than <code>scale</code>.</li>
<li>Can be re-applied by any other JavaScript in the page – useful if new content has been loaded by an Ajax request.</li>
</ul>
<p><strong> <a href="/code/supersleight-transparent-png-in-ie6/supersleight.zip">Download SuperSleight</a> </strong></p>
<h4>Implementation</h4>
<p>Getting SuperSleight running on a page is quite straightforward, you just need to link the supplied JavaScript file (or the minified version if you prefer) into your document <strong>inside conditional comments</strong> so that it is delivered to only Internet Explorer 6 or older.</p>
<ol class="code">
<li class="cmnt"><code>&lt;!--[if lte IE 6]&gt;</code></li>
<li class="tab1 cmnt"><code>&lt;script type="text/javascript" src="supersleight-min.js"&gt;&lt;/script&gt;</code></li>
<li class="cmnt"><code>&lt;![endif]&#8211;&gt;</code></li>
<li class="source">Source:  <a title="Download the above code as a textfile" href="/code/supersleight-transparent-png-in-ie6/3.txt">/code/supersleight-transparent-png-in-ie6/3.txt</a></li>
</ol>
<p>Supplied with the JavaScript is a simple transparent <span class="caps">GIF</span> file. The script replaces the existing <span class="caps">PNG</span> with this before re-layering the <span class="caps">PNG</span> over the top using AlphaImageLoaded. You can change the name or path of the image in the top of the JavaScript file, where you’ll also find the option to turn off the adding of <code>position: relative</code> to links and fields if you don’t want that.</p>
<p>The script is kicked off with a call to <code>supersleight.init()</code> at the bottom. The scope of the script can be limited to just one part of the page by passing an ID of an element to <code>supersleight.limitTo()</code>. And that’s all there is to it.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.awethentik.com/blogs/2008/07/multi-platform-cross-browser-transparent-pngs/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
