瀏覽器默認差異實測 (無任何 CSS 時)
| 類型 | Firefox/Safari | IE/Edge (Legacy) | Chrome |
| text (高度) | 22px (含邊框) | 24px | ≈22px |
| text (寬度) | 146px | 146px | 146px |
| submit (高度) | 23px (含陰影) | 25px | ≈21px |
| submit (寬度) | 75px | 73px | 73px |
四大核心避坑原則
1. 不要直接設 `height` — 導致 IE 文字靠上
/* ❌ 錯誤 */
input[type="text"] { height: 30px; }
/* ✅ 正確:用 padding 撐高,文字自動垂直居中 */
input[type="text"] {
padding: 6px 10px; /* 上下 6px → 視覺高度 ≈ 28px */
font-size: 14px; line-height: 1.5;
box-sizing: border-box; /* 含 padding 在 width 內 */
}
2. 強制聲明字體 (Input 不繼承父級字體)
input, textarea, select, button {
font-family: inherit; /* 或明確指定系統字體棧 */
font-size: 100%; /* 修正 Chrome/FF 縮小問題 */
}
3. 精確控制寬度,配合背景圖對齊
.search-input {
width: 220px; /* 精確像素 */
padding: 6px 36px 6px 10px; /* 右側留 36px 給放大鏡圖標 */
background: #fff url(search.svg) no-repeat right 8px center;
border: 1px solid #ddd; border-radius: 4px;
}
4. 統一重置按鈕基準 (跨瀏覽器最痛點)
button,
input[type="button"],
input[type="submit"],
input[type="reset"] {
-webkit-appearance: none; -moz-appearance: none; appearance: none;
border: none; background: none; padding: 0; margin: 0;
font: inherit; color: inherit; cursor: pointer; line-height: normal;
}
button::-moz-focus-inner,
input::-moz-focus-inner { border: 0; padding: 0; } /* FF 內部邊距 */
現代表單樣式最佳實踐
- **統一設計令牌**:`--input-h: 40px; --input-padding-x: 12px; --input-radius: 6px;`。
- **狀態完整**:`:hover`、`:focus-visible`、`:disabled`、`:read-only`、`.error`、`.success`。
- **原生驗證 UI**:`:invalid:not(:placeholder-shown)` 紅框 + `::after` 提示,零 JS。
- **無障礙**:`