萬盛學電腦網

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

跨瀏覽器隱藏 input 文字終極方案:IE6/7/8、Firefox、Opera 兼容性測試總結

隱藏 `` 原生 `value` 文字同時保留表單語意,是圖片替換按鈕的常見需求。各瀏覽器渲染差異巨大,本文實測 5 種方案在主流瀏覽器的表現,給出最佳選擇。

測試基礎樣式

input {
  height: 20px;
  width: 50px;
  border: 1px solid #ccc;
  background: #f9f9f9;
  overflow: hidden;  /* 關鍵:截斷溢出 */
}

方案測試矩陣

IE6 / IE7

方法代碼結果
line-height 巨大`line-height: 100em;`✅ 完美隱藏
padding-top 擠出`padding-top: 60px; height: 0;`✅ 完美隱藏
line-height 中等`line-height: 8em;`✅ 完美隱藏
font-size 巨大`font-size: 80em;`✅ 完美隱藏
line-height 極大`line-height: 25em;`✅ 完美隱藏

Firefox (現代版本)

方法代碼結果
text-indent 負值`text-indent: -999px;`✅ 完美隱藏
font-size 歸零`font-size: 0;`✅ 完美隱藏

Opera (Presto 內核舊版)

方法代碼結果
padding-top 擠出`padding-top: 60px; height: 0;`✅ 唯一有效方案

結論:無單一方案全兼容

❌ 不存在一套 CSS 同時完美覆蓋 IE6/7、Firefox、Opera。

最佳實踐策略

  1. 現代項目(IE9+):font-size: 0; line-height: 0; text-indent: -9999px; 三重保險
  2. 需兼容 IE6/7:採用 條件註釋 分發差異代碼
    <!--[if lte IE 7]>
      input.hide-text { line-height: 100em; }
    <![endif]-->
    <!--[if gt IE 7]>