需求:`` 保留語意 value 供無障礙/SEO,但視覺上用背景圖替代文字。核心難點:跨瀏覽器隱藏原生 value 文字且不影響佈局。
input {
margin: 5px 0;
width: 200px;
height: 20px;
display: block;
overflow: hidden; /* 關鍵:截斷溢出 */
background: #f9f9f9;
border: 1px solid orange;
}
.hide-text {
text-indent: -200em; /* 將文字推出可視區 */
/* IE 專屬:保留 value 供表單提交 */
*ie: expression(this.value = "提交");
}
✅ 現代瀏覽器完美
⚠️ Opera 早期版本可能殘留像素
⚠️ IE Expression 非標準、效能差
.hide-padding {
padding-top: 20px; /* 大於 height */
height: 0 !important; /* 實際高度由 padding 撐開 */
}
✅ 邏輯簡單
⚠️ Firefox 會被 padding 撐大實際高度
⚠️ 需精確計算 padding 值
.hide-font {
font-size: 100em; /* 文字巨大化,超出容器被 overflow:hidden 切掉 */
}
✅ IE6+/FF/Chrome/Opera/Safari 全通過
✅ 無副作用、無 Hack
⚠️ 極大字體可能觸發瀏覽器渲染上限(極罕見)
| 方案 | IE6/7 | IE8+ | Firefox | Chrome | Opera | Safari | 推薦度 |
|---|---|---|---|---|---|---|---|
| text-indent | ✅ | ✅ | ✅ | ✅ | ⚠️ | ✅ | ⭐⭐⭐ |
| padding-top | ✅ | ✅ | ⚠️ 高度異常 | ✅ | ✅ | ✅ | ⭐⭐ |
| font-size:100em | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ⭐⭐⭐⭐⭐ |
<button type="submit" class="btn-submit">
<span class="text">提交</span>
</button>
.btn-submit {
width: 80px; height: 32px;
background: url(btn-bg.png) no-repeat;
border: 0; cursor: pointer;
overflow: hidden;
}
.btn-submit .text {
display: block;
text-indent: -9999px; /* 或 font-size: 0; line-height: 0; */
}
優勢:<button> 語意正確、可包含圖標、原生支援表單提交、無需 Hack。