惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Security Archives - TechRepublic
Security Archives - TechRepublic
博客园 - 三生石上(FineUI控件)
云风的 BLOG
云风的 BLOG
C
Check Point Blog
Engineering at Meta
Engineering at Meta
Y
Y Combinator Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Forbes - Security
Forbes - Security
IT之家
IT之家
L
LINUX DO - 最新话题
N
News and Events Feed by Topic
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
雷峰网
雷峰网
N
News | PayPal Newsroom
The Last Watchdog
The Last Watchdog
V
Visual Studio Blog
月光博客
月光博客
Microsoft Azure Blog
Microsoft Azure Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Webroot Blog
Webroot Blog
TaoSecurity Blog
TaoSecurity Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Schneier on Security
Schneier on Security
P
Privacy International News Feed
G
Google Developers Blog
博客园 - 聂微东
博客园 - 叶小钗
M
MIT News - Artificial intelligence
Apple Machine Learning Research
Apple Machine Learning Research
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
WordPress大学
WordPress大学
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Simon Willison's Weblog
Simon Willison's Weblog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
宝玉的分享
宝玉的分享
H
Hacker News: Front Page
Martin Fowler
Martin Fowler
L
Lohrmann on Cybersecurity
G
GRAHAM CLULEY
酷 壳 – CoolShell
酷 壳 – CoolShell
罗磊的独立博客
T
The Exploit Database - CXSecurity.com
S
Security @ Cisco Blogs
博客园_首页
AWS News Blog
AWS News Blog
P
Proofpoint News Feed
人人都是产品经理
人人都是产品经理
Help Net Security
Help Net Security
Google DeepMind News
Google DeepMind News
T
Threatpost

博客园 - zzh

关于div高度自适应问题,兼容IE和火狐 C#利用短信猫收发短信息的方法 String[3]: the Size property has an invalid size of 0. - zzh XML Serializable Generic Dictionary C#中的多线程——线程同步基础 安装window service 中出现Set Service Login对话框 input style兼容IE6的方案 - zzh - 博客园 document.body.scrollTop为0的处理办法 js 获取浏览器高度和宽度值(多浏览器) Assembly Manifest 通俗简易手册 C# Enumeration 使用 关于页面的已终止操作 - zzh - 博客园 DOM标准与IE的html元素事件模型区别 访问ASP.NET临时文件夹的权限问题 打架必备!擒敌拳1-16动连贯动作 分解动作 Ajax错误提示Sys未定义解决方法 - zzh - 博客园 IE上关于PNG做背景,层上超链接不能点击的解决办法 - zzh - 博客园 解决js中onMouseOut事件冒泡的问题 javascript 中的this 与事件注册 及event
Making IE use PNG Alpha transparency
zzh · 2011-03-10 · via 博客园 - zzh

Deprecated

This fix is deprecated and is no longer supported (meaning that I will not help you to get it working with your pages). It was only written to fill the gap while waiting for Internet Explorer to implement PNG alpha. That has now happened in IE 7. Although this fix can make it work in IE 6 as well, that is counter productive for the future of the Web. IE 6 is a major problem to Web developers (IE 7 is a problem as well, but for now, let's overlook that, since it does at least implement PNG alpha), and the sooner it stops being used, the better.

Instead of using this hack, users of IE 6 should be encouraged to upgrade to IE 7. Users who cannot upgrade to IE 7 (because IE 7 is not being released for most Windows operating systems) should use a better browser, such as Opera or Firefox. They have been abandoned by Microsoft.
What is PNG?

PNG is an image format that combines the best features of GIF and JPG/JPEG, the traditional and most common image formats used on the World Wide Web. PNG is supported by virtually all major and minor browsers that are in use today.
JPEG format approximates the original picture, so the quality can be quite low, but it allows the images to be significantly smaller in file size than the equivalent raw image (pixmap or Windows Bitmap). JPEGs do not support Transparency, but images can have up to 16 million colours. Athough the compression technique uses approximation, for natural photographs, JPEG compression usually produces the smallest file size of any of the standard image formats with the approximation being almost undetectable to the naked eye. However, GIF is more traditionally used when pictures contain large areas of the same colour, as the approximation used by JPEG becomes quite noticeable. JPEG does not support multiple image layers to allow animation, but its related format - MPEG - does.
GIF format reduces the file size of the original image by recognising patterns in the raw image data, effectively making a zipped version of the image. This means the displayed image is identical to the original but requires the image to have a maximum of 256 unique colours. GIF format allows one colour to represent 100% transparency (known as binary transparency). GIF also supports multiple image layers to allow animation.
PNG format compresses images in a similar way to GIF, but images can have up to 280 thousand billion unique colours (normally, a maximum of 16 million colours are used). PNG also supports binary transparency, as well as full Alpha, meaning that each pixel of the image can also have one of 256 different levels of transparency (or 65 thousand levels if you really want to). PNG format produces files with approximately the same file size as the equivalent GIF image, assuming they have the same number of colours. PNG does not support multiple image layers to allow animation, but its related format - MNG - does.
What is wrong with the PNG format?

Well, ... nothing. The problem is that the most popular browser - Internet Explorer 6- for Windows - does not natively understand Alpha transparency, even though Gecko browsers, KHTML/WebKit browsers, Opera, Internet Explorer Mac and several minor browsers all do. It displays normal PNGs correctly, and version 4 and up can handle binary transparency if the palette is saved with the image, but as soon as any Alpha transparency is applied, the transparency is replaced with the image's background colour. This is what Internet Explorer 6- normally shows when a PNG image with alpha transparency is placed over another image (the green box is there just so that you can see what's happening):

But this is what it should show:

'But you said you could make it work!'

And I can! You see, Internet Explorer supports some extra style features for providing transitions and filters to images, so you can do wipes and fade-ins and lots of other tricks that should really be left to PowerPoint. However, two of these can help here. One is the AlphaImageLoader, which applies an image to the background of any element whose height and width are both explicitly set (or if the position: absolute; style is set) and supports full Alpha transparency. It is supported from Internet Explorer 5.5. The second is Alpha, which can be used to provide Alpha transparency to any element, making the element transparent. There are two versions of this filter, one that works in all versions of Internet Explorer 4+ for Windows, and one that works in version 5.5 and above. As the AlphaImageLoader works only from 5.5, I will use the Alpha for version 5.5+ as well. IE 7 does support alpha transparency, so I will exclude it from this fix.

Using these filters this way means that no JavaScript is required, no browser detects are required and no conditional comments are required. All browsers that do not understand Alpha (including earlier versions of IE for Windows and IE5.5/Win with stylesheets disabled - a very difficult thing to do!) will display the image in the non-Alpha format.

You may also notice that I set display:inline-block on the span. This is actually the effect we need, since the span will be displaying the image, and it needs to be allowed to accept size. IE only partially understands inline-block, but fortunately it understands enough to use it here (this is only needed in IE 6 when you use a strict doctype - other versions always asume inline-block when you give dimensions, however it is a good idea to always include it just in case you change doctypes later). Unfortunately this may have unwanted alignment effects in other browsers, but that will be solved by using conditional comments (see below).
<span style="width:400px;height:32px;display:inline-block;
/* the height and width should match those of the image */
filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='varyAlpha.png');
/* apply the background image with Alpha in IE5.5/Win. The src should match that of the image */
"><img style="
filter:progid:DXImageTransform.Microsoft.Alpha(opacity=0);
/* make the real image fully transparent in IE5.5/Win, so the Alpha image can show through */
" src="varyAlpha.png" width="400" height="32" border="0" alt=""></span>

If you need the image to display at any other size than its natural size, change this:
(src='varyAlpha.png')

To this:
(src='varyAlpha.png',sizingMethod='scale')

And make sure that the size of the image in the IMG tag, and the size of the span match the chosen size.
— Result:

Just make sure that there is no whitespace (tab, space or linebreak characters) between the IMG and the SPAN tags.
Making it validate

This is not valid CSS, and it may cause problems in IE 7. If you want to make it valid, you can put the extra CSS in a conditional comment, and use that to protect IE 7, make it work in IE 5.5-6, and allow your page to validate. However, this would need to contain one CSS rule for each image, as the filter needs to be different for each image. For this reason, I assign an ID to each span, and then use the ID to set the filter.
<!--[if gte IE 5.5]>
<![if lt IE 7]>
<style type="text/css">
#alImg1 img, #alImg2 img { filter:progid:DXImageTransform.Microsoft.Alpha(opacity=0); }
#alImg1, #alImg2 { display: inline-block; }
#alImg1 { filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='varyAlpha.png'); }
#alImg2 { filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='alphaTwo.png'); }
</style>
<![endif]>
<![endif]-->

...

<span id="alImg1" style="width:400px;height:32px;">
<img src="varyAlpha.png" width="400" height="32" border="0" alt=""></span>
<span id="alImg2" style="width:30px;height:50px;">
<img src="alphaTwo.png" width="30" height="50" border="0" alt=""></span>