鏈接聚焦時的虛線框(焦點環)影響視覺美觀,本文提供雙管齊下方案:IE 用 HTC 行為文件強制模糊焦點,現代瀏覽器用 outline:0 移除輪廓。
建立 link.htc:
<public:attach event="onfocus" onevent="hscfsy()"/>
<script language="javascript">
function hscfsy() { this.blur(); }
</script>
CSS 中引用:
a {
display:block; width:100px; height:30px; line-height:30px; text-align:center;
behavior: url(link.htc); /* IE 專屬 */
}
a:hover { background:#ddd; }
a:focus { outline:0; }Firefox/Opera/Safari/Chrome/IE8+ 直接支援:
a:focus { outline:0; }
<!DOCTYPE html>
<html><head>
<style>
a {
display:block; width:100px; height:30px; line-height:30px; text-align:center;
overflow:hidden; behavior:url(link.htc);
}
a:hover { background:#ddd; }
a:focus { outline:0; } /* 非 IE */
</style>
</head><body>
<a href="#" title="示例">測試鏈接</a>
</body></html>
outline:1px dotted #999 或自定義明顯的 :focus 樣式,而非完全移除。behavior 支援,現代項目請直接用 :focus-visible 偽類平衡美觀與無障礙:focus:not(:focus-visible) { outline:0; } /* 鼠標點擊不顯示 */
:focus-visible { outline:2px solid #0066CC; } /* 鍵盤導航顯示明顯焦點 */