在IE6中使用背景透明的PNG圖片

來源:互聯網
上載者:User

Directly inserting images in PNG format with transparency will leave you with a white spot when display in

Internet
Explorer. It makes your image looks ugly and this is really the last
thing any web designer or webmaster would want on their website. Here’s a solution on how to solve this white spot and let the PNG transparency recovers.

Let’s take a look at the difference of PNG images (with transparency) on both
major
browsers: Mozilla Firefox and Internet Explorer 6

Mozilla Firefox

PNG with transparency blends in nicely with no problem

Internet Explorer 6

Internet Explorer does not turn your transparency transparent, thus leaving you with a big ugly white spot.

Solution

Create a container to store your image. In this case I use a <div>.
Create your <div> inside your <body>, just like this.

<body> <div class="flower"></div> </body>

Next, create a <style> if you dont have one. Make sure they are between your <head> </head>. Put the following

css
inside.

<style>
body {background-color:#000}
div.flower {background:url(flower-transparent.png) no-repeat; height:100px; width:100px}
</style>

The CSS codes above displays your PNG image in a <div>. Works fine for Mozilla Firefox, but not for Internet Explorer. To get it working cross browser, create another set of css just for Internet Explorer right below your <style> </style>. Insert the following
codes.

<!–[if gte IE 5]>
<style type="text/css">
div.flower {
background:none;
filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=’flower.png’ ,sizingMethod=’crop’);
}
</style>
<![endif]–>

Your IE should now give you a perfect blend like the picture below.

Download my working example.

<!-- -->

PS:1、IE版本判斷function isIE6(){

if(navigator.userAgent.indexOf("MSIE")>0) { 
//是否是IE瀏覽器 

if(navigator.userAgent.indexOf("MSIE 6.0")>0) 
{ 
return true;
}
}
return false;

}2、在js中設定png圖片透明if(isIE6()){
div1.style.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod = crop;)";    
    div1.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = '../aws_skins/_def51/img/nav_first_2.png';
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.