<input type="text"> 在 Firefox/Chrome/Safari/IE 中預設高度、垂直對齊、字體繼承皆不同,導致搜尋框、登入框難以統一。本文提供實測有效的統一方案。
| 瀏覽器 | type=text 高度 | type=text 寬度 | type=submit 高度 | type=submit 寬度 |
|---|---|---|---|---|
| Firefox | 22px | 146px | 23px | 75px |
| Safari | 22px | 146px | 21px | 73px |
| IE | 24px | 146px | 25px | 73px |
height 會導致 IE 文字貼左上,無法垂直置中。正確做法:padding: 6px 8px; 由內距撐開高度,IE 文字同步置中。input 不繼承父層字體。必須直接設定:font-family: inherit; font-size: 100%; 或指定具體字體堆疊。width + padding 總寬高與圖片一致,避免文字遮蓋圖示或留白過大。.search-input {
border: 1px solid #ccc;
border-radius: 4px;
padding: 6px 30px 6px 10px; /* 右側留空給搜尋圖示 */
font-family: "Microsoft JhengHei", "Helvetica Neue", Helvetica, Arial, sans-serif;
font-size: 14px;
line-height: 1.5;
width: 220px;
background: url(search-icon.svg) no-repeat right 8px center;
box-sizing: border-box; /* 含 padding 在 width 內 */
}
.search-input:focus { outline: none; border-color: #0066cc; box-shadow: 0 0 0 2px rgba(0,102,204,.2); }
現代專案建議在 Reset 階段加入:
input, textarea, select, button {
font-family: inherit;
font-size: 100%;
line-height: 1.15;
margin: 0;
}
input[type="search"] { -webkit-appearance: none; appearance: none; } /* 移除 Safari 圓角 */
掌握這三大關鍵,input 跨瀏覽器樣式不再是噩夢。