利用 絕對定位,將一張帶有圓角透明區域的 PNG 圖片作為邊框層,精確覆蓋在原始 <img> 上方,形成圓角邊框視覺效果。關鍵在於解決 IE6 不支援 PNG Alpha 透明。
<div class="img-wrapper">
<img src="photo.jpg" alt="照片" class="target-img">
<span class="rounded-frame"></span> <!-- 圓角邊框層 -->
</div>
.img-wrapper {
position: relative;
display: inline-block; /* 包裹圖片尺寸 */
line-height: 0; /* 消除底部間隙 */
}
.target-img { display: block; vertical-align: bottom; }
.rounded-frame {
position: absolute;
top: 0; left: 0; right: 0; bottom: 0;
background: url(round-frame.png) no-repeat; /* 含透明圓角的 PNG */
cursor: pointer; /* 可點擊穿透到圖片 */
/* IE6 需要 hasLayout 觸發 */
_zoom: 1;
}
一張 60x60px 的 PNG,四角透明弧形,邊框區域不透明(或半透明漸變)。

原理:遍歷頁面指定標籤,用 VML (Vector Markup Language) 重繪 PNG 透明區域。支援 <img>、background-image、<input type="image"> 等所有場景。
<!--[if IE 6]>
<script src="DD_belatedPNG.js"></script>
<script>
DD_belatedPNG.fix('.rounded-frame, .other-png-bg, img.png-fix');
</script>
<![endif]-->
推薦庫:DD_belatedPNG(最穩健、支援背景平鋪/定位、體積小)。
本文原代碼為壓縮混淆版 EvPNG,功能相同但難維護,不建議直接使用。
.rounded-frame {
/* 現代瀏覽器 */
background: url(round-frame.png) no-repeat;
/* IE6 濾鏡覆蓋 */
_background: none;
_filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(
src='round-frame.png', sizingMethod='scale'
);
}
缺點:
<img> 標籤無效。sizingMethod='scale' 會拉伸圖片,破壞圓角比例(需製作精確尺寸 PNG)。:hover、無法設定 background-position/repeat。position:relative 修復)。border-radius + box-shadow + overflow:hidden,零圖片、零 Hack、全瀏覽器支援(IE9+)。border-radius (IE9+) / PIE.htc (IE6-8) / 方案一 JS 修復。.rounded-img {
border-radius: 8px;
overflow: hidden; /* 裁切內部 img 圓角 */
display: inline-block;
/* IE6-8 降級:直角或引入 PIE.htc */
_behavior: url(PIE.htc); /* 需正確路徑 */
}
.rounded-img img { display: block; width: 100%; height: auto; }