在 IE6/IE7 中,給 `` 設置背景圖片並保留 `border` 會出現難看的**黑色邊框**。本文提供三種徹底解決方案,兼容現代瀏覽器。
<style>
.hk { height:22px; border:#999 1px solid; background:#E5E5E5 url(btn.png) repeat-x; cursor:pointer; }
</style>
<input type="submit" value="登陸" class="hk" style="width:70px" />
IE6/7 顯示:按鈕四周出現 1px 黑框,破壞視覺一致性。
.hk {
height: 22px;
border: none; /* 關鍵:去除原生 border */
background: url(btn_with_border.png) no-repeat; /* 背景圖已含邊框 */
cursor: pointer;
}
<style>
.pn { height:22px; border:none; background:#E5E5E5 url(pn.png) repeat-x; cursor:pointer; }
.l_pn { display:inline-block; *display:inline; *zoom:1; border:#999 1px solid; } /* IE6/7 inline-block Hack */
</style>
<label class="l_pn" style="width:70px">
<input type="submit" value="登陸" class="pn" style="width:70px" />
</label>
.hk {
border: 1px solid #999;
_border: none; /* IE6 忽略 */
*border: none; /* IE7 忽略 */
border: 1px solid #999\9; /* IE8+ 保留 */
}
缺點:Hack 混亂,易隨瀏覽器更新失效。
<button class="btn" type="submit">登陸</button>
.btn {
position: relative;
border: 1px solid #999;
background: #E5E5E5 url(pn.png) repeat-x;
padding: 0 12px;
height: 28px;
line-height: 26px; /* 垂直居中 */
cursor: pointer;
overflow: visible; /* IE 修正內邊距 */
}
/* 移除 IE 內部神秘 padding */
.btn::-moz-focus-inner { border: 0; padding: 0; }
徹底理解 IE 表單控件渲染機制,才能在兼容性與可維護性間做出最優權衡。