萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網頁製作 >> Div CSS 教學

CSS 移除連結點擊虛線框:outline:none 與 hideFocus 跨瀏覽器方案

問題

連結被點擊時出現虛線框(焦點輪廓),松開滑鼠後仍殘留,影響視覺美觀。

方案一:HTML 屬性(IE 專用)

<a href="..." hidefocus="true">連結</a>

方案二:CSS outline: none(Firefox 1.5+、Chrome、Safari、Edge、Opera)

.HideFocus { outline: none; }

套用:<a class="HideFocus">... 或全域 a { outline:none; }。

方案三:CSS Expression 動態注入(IE5+,不推薦,效能差)

.HideFocus {
  hide-focus: expression(this.hideFocus=true); /* IE */
  outline: none;                               /* Firefox+ */
}

方案四:HTC 行為檔案(IE 徹底移除)+ CSS 修正 Firefox

link.htc:

<public:attach event="onfocus" onevent="this.hideFocus=true" />

CSS:

a { behavior: url(link.htc); }
a:focus { outline: 0; } /* Firefox 補強 */

適用範圍

同樣適用於 <input>、<button> 等可聚焦元素。

無障礙警示

移除焦點輪廓會嚴重損害鍵盤導航使用者體驗。建議保留焦點樣式,改為自訂明顯風格:

a:focus {
  outline: 2px solid #007bff;
  outline-offset: 2px;
  /* 或 */
  box-shadow: 0 0 0 3px rgba(0,123,255,.4);
}

既美觀又無障礙,符合 WCAG 2.1 SC 2.4.7 Focus Visible。

copyright © 萬盛學電腦網 all rights reserved