萬盛學電腦網

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

IE6/7 移除 button、input 預設邊框與焦點外框完整解法

表單元素在 IE6/7 預設擁有立體邊框與焦點虛線框,嚴重影響視覺一致性。以下提供三種去除方案,涵蓋邊框與焦點線雙重需求。

HTML 基礎結構

<div class="wrap">
  <input type="text" class="input_txt">
  <input type="submit" value="submit" class="input_btn">
  <input type="button" value="提交" class="input_btn">
</div>

方案一:標準 CSS 重置(推薦)

input { margin: 0; padding: 0; }
.wrap { background-color: #0f0; }
.input_txt, .input_btn { border: 0 none; }

簡單、標準、相容性最好。搭配 background 自訂外觀。

方案二:IE 專用濾鏡 Hack(不推薦,僅供參考)

<!--[if IE]>
<style>
  input { margin: 0; padding: 0; filter: chroma(color=#000000); border: none; }
  .wrap { background-color: #0f0; }
</style>
<![endif]-->

利用 chroma 濾鏡將特定顏色透明化,但實測在真實 IE7 環境常有副作用(如文字消失),不建議生產環境使用。

去除焦點虛線框

a:focus, *:focus { noFocusLine: expression(this.onFocus=this.blur()); }

IE 專有 expression 強制失焦,但會觸發無障礙問題(鍵盤使用者無法看到焦點)。現代做法改用:

:focus { outline: none; } /* 標準寫法 */
:focus-visible { outline: 2px solid #0066cc; } /* 僅鍵盤聚焦顯示,滑鼠點擊不顯示 */

最佳實踐總結

  1. 統一使用方案一重置邊框,再自訂 border、background、border-radius。
  2. 焦點樣式保留 outline 或自訂 box-shadow,確保鍵盤可用性。
  3. 若需極致一致外觀,考慮以 <button> 替代 <input type="button">,樣式控制更靈活。
copyright © 萬盛學電腦網 all rights reserved