| 類型 | Firefox/Safari | IE |
type=text | 高 22px (含邊框) / 寬 146px | 高 24px / 寬 146px |
type=submit | 高 23px (含陰影) / 寬 75px | 高 25px / 寬 73px |
Safari submit 高 21px / 寬 73px。
核心避坑三原則
1. 禁止給 text 輸入框設 height
height 會導致 IE 文字貼左上角,無法垂直置中。正解:用 padding 撐開高度:
input[type="text"] {
padding: 6px 8px; /* 上下 6px → 視覺高度 ~28px,文字自動垂直置中 */
/* 不要寫 height */
font-family: inherit; /* 關鍵:繼承父層字體 */
font-size: 100%;
}
2. input 不繼承字體,必須顯式聲明
input, textarea, select, button {
font-family: inherit;
font-size: 100%;
}
3. 背景圖片尺寸與輸入框內邊距配合
- 背景圖寬高 = 輸入框
width + padding-left/right、height (由 padding 決定)
- 文字輸入區域不被背景圖標誌遮擋
- 使用
background-position: left center 或 right center 定位圖標
完美搜尋框範本
.search-box {
position: relative;
width: 300px;
}
.search-box input[type="text"] {
width: 100%;
padding: 8px 36px 8px 12px; /* 右留 36px 給搜尋圖示 */
border: 1px solid #ccc;
border-radius: 4px;
font: inherit; /* 繼承字體族、大小、行高 */
box-sizing: border-box; /* 含 padding 在 width 內 */
background: #fff url(search-icon.svg) no-repeat right 10px center;
}
.search-box input[type="text"]:focus {
outline: none;
border-color: #007bff;
box-shadow: 0 0 0 2px rgba(0,123,255,.25);
}
進階:統一表單控制項基線
/* Normalize.css 片段 */
input, button, select, textarea {
font-family: inherit;
font-size: 100%;
line-height: 1.15;
margin: 0;
}
button, input { overflow: visible; }
button, select { text-transform: none; }
button, [type="button"], [type="reset"], [type="submit"] {
-webkit-appearance: button;
}
[type="search"] {
-webkit-appearance: textfield;
outline-offset: -2px;
}
採用成熟的 Normalize/Reset 而非手工修補,覆蓋更全面。
copyright ©
萬盛學電腦網 all rights reserved