{"id":8465,"date":"2006-10-20T23:33:29","date_gmt":"2006-10-21T03:33:29","guid":{"rendered":"https:\/\/htmlcssjs.wpengine.com\/?p=8465"},"modified":"2014-10-08T16:49:49","modified_gmt":"2014-10-08T20:49:49","slug":"cross-browser-png-transparency","status":"publish","type":"post","link":"https:\/\/htmlcssjavascript.com\/web\/cross-browser-png-transparency\/","title":{"rendered":"Cross Browser PNG Transparency"},"content":{"rendered":"<p>(I wrote this post at my old job. Since IE6 will be around for some time to come, the techniques in use here are still useful going forward as more and more designers are going to want to use Transparent PNGs in their designs. I&#8217;m going to add onto this article at some point in the near future to fully flesh out all the possibilities. See <a href=\"http:\/\/www.drunkenfist.com\/304\/2007\/04\/04\/cross-browser-png-transparency-part-2\/\">PART 2 of this article<\/a>)<br \/>\n<!--more--><br \/>\nWith the announcement that Internet Explorer 7 would have PNG alpha channel support, there was much rejoicing in the web design and development space (okay, at least in my corner of that space.) The ability of the PNG format to present true transparency and variable opacity has been one of the most tantalizing untapped features out there. Anyone who\u2019s ever worked with transparent GIFs for more than ten seconds should automatically know of what I speak- stack two of them or put one on a background that isn\u2019t matched to it and things start to get ugly in a hurry. IE7 looks like it\u2019s finally going to hand out some native PNG goodness. Someone buy Redmond a beer.<\/p>\n<p>I mention \u201cnative\u201d support because IE, since 5.5 (which was released 475 years ago, I think) has been able to display PNGs with all the neat bells and whistles. The thing is, to take advantage of that ability developers have had to cross over into a land of nonstandard and somewhat confusing code.<\/p>\n<p>Personally, I shied away from it for longer than I probably should have for that very reason.<\/p>\n<p>All that changed recently as we had a tool tip design for a client that pretty much demanded full alpha PNGs. We looked at the design and thought, \u201cthat looks really nice, we should see if we make that work.\u201d It fell to me to make it work in IE6, the target browser for the implementation. We also targeted Firefox to ensure that the styles (as well as the rest of the application) would work on a standards-compliant browser.<\/p>\n<p>As you might have guessed by the fact that there\u2019s a tutorial in front of you, it worked (better than expected actually) and the process was interesting enough that I thought I\u2019d share the experience. I\u2019ve mocked up a few simplified examples that should cover all implementation variations in a way that is easier to digest than the full blown implementation.<\/p>\n<p>Firefox: As a formality, I\u2019ll touch on getting this to work in Firefox\u2026 Firefox\u2019s implementation couldn\u2019t be more straightforward. Simply source the PNG you want to use in an &lt;IMG&gt; tag or in a standard CSS background property declaration. Simply insert the image as is or apply that style, the same way you would apply any other style to a suitable HTML Element and voila you\u2019ve got a transparent image.<\/p>\n<p>Internet Explorer: Where the Firefox implementation was straightforward, the IE version is anything but. For starters, if you have any hopes of producing fully standards compliant code and don\u2019t want to use any browser dependant hacks or extensions look away now. The following system uses all sorts of things that folks obsessed with code purity might find distasteful. Personally, while I would love to produce nothing but \u201cpure\u201d standards compliant code (and I do when I can); there are shortcomings on both ends (both in browser compliance and in the design of the specifications themselves) that get in the way.<\/p>\n<p>It\u2019s not something I lose any sleep over.<\/p>\n<h3>Simple Image Replacement<\/h3>\n<p>Here\u2019s an example:<br \/>\n<iframe src=\"\/web\/samples\/transparency.htm\" style=\"width:80%; height:200px;\"><\/iframe><\/p>\n<p>Our first variation is probably the most straightforward usage of a PNG: inserting a PNG right into the document as an &lt;IMG&gt;. As we mentioned earlier, doing this in Firefox (and any other browser with full PNG support) is as easy as setting the correct SRC attribute and watching the accolades flow in.<\/p>\n<p><code>&lt;img src=&quot;example1_1.png&quot; \/&gt;<\/code><\/p>\n<p>Internet Explorer 6, on the other hand, needs a little trickery in order to get things to work in the way that we expect. For starters, there needs to be some sort of container for the image. This can be a DIV, a TD, a P, etc. Anything that can enclose the image and cascade styles down. I\u2019m going to use a DIV here.<\/p>\n<p><code>&lt;div id=&quot;example&quot;&gt;&lt;img src=&quot;example1_1.png&quot; \/&gt; &lt;\/div&gt;<\/code><\/p>\n<p>The basic idea is this; we have the PNG in place for regular browsers and then use a couple of proprietary Microsoft extensions to fool IE into rendering it correctly. I\u2019ll go through each of the specifics in turn.<\/p>\n<p>Now that we\u2019ve got it put together structurally, it\u2019s time to style it and make it work the way nature intended. Here are the styles that will make this all go:<\/p>\n<p>First, the cross browser styles to set the position and size of the containing DIV and the image size:<\/p>\n<p>#example {<br \/>\nposition:absolute;<br \/>\nwidth:200px;<br \/>\nheight:100px;<br \/>\nz-index:1;<br \/>\nleft: 50px;<br \/>\ntop: 10px;<\/p>\n<p>}<br \/>\n#example img{<br \/>\nheight:100px;<br \/>\nwidth:200px;<br \/>\n}<\/p>\n<p>Pretty basic so far, as you can see. We then need to add the IE specific styles in order to get the whole thing to work in Microsoft\u2019s browser:<\/p>\n<p>#example img{<br \/>\nfilter:progid:DXImageTransform.Microsoft.Alpha(opacity=0);<br \/>\n}<\/p>\n<p>#example{<br \/>\nfilter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\u2019example1_1.png\u2019,sizingMethod=\u2019image\u2019)<br \/>\n}<\/p>\n<p>I\u2019ll go through each of these in detail. The first line sets the Alpha of the image to 0, effectively hiding it from the user. Internet Explorer has a set of Direct X filters that extend CSS in interesting and powerful ways. This is one of the most useful. Here, it takes one argument, \u201copacity\u201d and a value, \u201c0\u201d, which sets the image to be fully transparent.<\/p>\n<p>Why would we want to hide the image? Keep reading and the beauty (horror?) of this technique will become clear.<\/p>\n<p>The second style, applies the AlphaImageLoader filter to load a new copy of the image we just hid into the containing DIV. It\u2019s that image that will be processed by the DX filter and rendered correctly. Now hiding the image itself makes sense, no?<\/p>\n<p>The AlphaImageLoader filter takes two parameters, the SRC and the sizingMethod. There are three different settings for sizingMethod, we\u2019ll touch on all three as we move through this article. The one I used, &#8216;image&#8217; is for situations like this where you want to replicate the display of a PNG image that is being displayed inline. We\u2019ll cover images used as backgrounds in the next post on this topic.<\/p>\n<p>One interesting (and important) thing to note about the SRC in the AlphaImageLoader filter is that it\u2019s relative from the rendered document and not from the style sheet. This is just one more reason why root relative links are the way to go\u2026<\/p>\n<p>Another equally important note is that, for the filter to work, the image has to have &#8216;layout.&#8217; The concept of &#8216;layout&#8217; is one beyond the scope of this article, but in general it\u2019s a Boolean flag used by IE\u2019s rendering engine to determine how certain elements are handled. For the filter to work properly, hasLayout MUST be set to true. For the sake of this document we\u2019ll just note that adding proper height and width declarations to the image will set hasLayout correctly.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>(I wrote this post at my old job. Since IE6 will be around for some time to come, the techniques in use here are still useful going forward as more and more designers are going to want to use Transparent PNGs in their designs. I&#8217;m going to add onto this article at some point in [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-8465","post","type-post","status-publish","format-standard","hentry","category-web"],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p3uLY6-2cx","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/htmlcssjavascript.com\/wp-json\/wp\/v2\/posts\/8465","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/htmlcssjavascript.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/htmlcssjavascript.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/htmlcssjavascript.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/htmlcssjavascript.com\/wp-json\/wp\/v2\/comments?post=8465"}],"version-history":[{"count":0,"href":"https:\/\/htmlcssjavascript.com\/wp-json\/wp\/v2\/posts\/8465\/revisions"}],"wp:attachment":[{"href":"https:\/\/htmlcssjavascript.com\/wp-json\/wp\/v2\/media?parent=8465"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/htmlcssjavascript.com\/wp-json\/wp\/v2\/categories?post=8465"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/htmlcssjavascript.com\/wp-json\/wp\/v2\/tags?post=8465"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}