以下 CSS 在 IE7+ 顯示為一條 6px 綠色虛線空行,但在 IE6 卻變成粗虛線且高度遠超 6px:
#abcline {
border-top: 6px dashed #7BD676;
border-color: transparent;
border-left: 0px solid #7BD676;
border-right: 0px solid #7BD676;
border-bottom: 0px solid #7BD676;
width: 960px; height: 0px;
}
border-color: transparent,會將透明色渲染為黑色或預設色,產生可見邊框。border-left/right/bottom: 0px 產生無效代碼,增加解析負擔。height: 0 配合 border-top 的盒模型渲染有誤差,導致高度異常。#abcline {
width: 960px;
height: 0;
border-top: 6px dashed transparent; /* 現代瀏覽器顯示透明虛線 */
filter: Chroma(color=#000000); /* IE6/7 濾鏡將黑色變透明 */
_border-top: 6px dashed #000000; /* IE6 hack:先給黑色虛線供濾鏡處理 */
}
原理:IE 專屬 filter: Chroma(color=#000000) 會將指定顏色(此處黑色)設為透明。現代瀏覽器直接識別 transparent 關鍵字,忽略 filter。
若僅需 6px 視覺空行,不必使用 border:
/* 方法 1:margin 撐開 */
#abcline { margin-top: 6px; }
/* 方法 2:空 div + 高度控制 */
#abcline { height: 6px; overflow: hidden; font-size: 0; line-height: 0; }
後者相容性最佳,無任何 border 渲染差異。