萬盛學電腦網

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

CSS 樣式化 input 元素完整指南:跨瀏覽器一致性、對齊、尺寸與無障礙

input 元素樣式化核心挑戰

  • 瀏覽器預設樣式差異極大 (邊框、內邊距、字體、圓角、外觀)
  • 部分屬性受 OS 主題影響 (macOS/iOS 圓角、Windows 高對比度模式)
  • 內建 UI 元素 (日期選擇器、檔案上傳按鈕) 樣式化受限

瀏覽器預設尺寸差異參考 (type="text")

瀏覽器高度寬度備註
Firefox22px (含邊框)146px (含邊框)
Safari22px146px
Chrome/Edge約 20-22px約 146px視作業系統主題
IE1124px146px較高

type="submit" 同樣存在高度差異 (Firefox 23px、Safari 21px、IE 25px)。

核心重置樣式 (必做)

input, textarea, select, button {
  /* 字體繼承:input 不會自動繼承父元素字體 */
  font-family: inherit;
  font-size: inherit;
  font-weight: inherit;
  line-height: inherit;
  color: inherit;
  
  /* 盒模型統一 */
  box-sizing: border-box;
  
  /* 移除預設外觀 (需配合具體類型) */
  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none;
  
  /* 移除預設邊框與背景 */
  border: none;
  background: none;
  
  /* 移除 iOS 陰影/圓角 */
  border-radius: 0;
  
  /* 移除 Firefox 內部 padding */
  padding: 0;
  margin: 0;
}

/* 針對性重置 */
input[type="search"] { 
  -webkit-appearance: none;  /* 移除 Safari 搜尋框預設圓角/放大鏡 */
}
input[type="number"]::-webkit-inner-spin-button,
input[type="number"]::-webkit-outer-spin-button {
  -webkit-appearance: none;
  margin: 0;
}
input[type="date"]::-webkit-calendar-picker-indicator {
  opacity: 0;  /* 隱藏預設日曆圖示,可自訂 */
}

文字輸入框垂直置中解決方案

❌ 錯誤:設定 `height` + `line-height` — IE 無法垂直置中

✅ 正確:使用 `padding` 撐開高度,文字自然置中

.input-text {
  /* 推薦:padding 控制高度 */
  padding: 8px 12px;        /* 上下 8px,左右 12px */
  font-size: 1rem;
  line-height: 1.5;         /* 確保行高足夠 */
  min-height: 40px;         /* 觸控友善最小高度 */
  border: 1px solid #ccc;
  border-radius: 4px;
  background: white;
  width: 100%;
  box-sizing: border-box;
}

/* 等效高度計算:font-size * line-height + padding-top + padding-bottom
   1rem * 1.5 + 8px + 8px ≈ 24px + 16px = 40px */

完整 Input 樣式系統範例

:root {
  --input-height: 40px;
  --input-padding-y: 8px;
  --input-padding-x: 12px;
  --input-font-size: 1rem;
  --input-border-color: #d1d5db;
  --input-border-radius: 6px;
  --input-focus-ring: 0 0 0 3px rgba(59, 130, 246, 0.4);
  --input-bg: #fff;
  --input-color: #111827;
  --input-placeholder-color: #9ca3af;
}

.form-input {
  width: 100%;
  height: var(--input-height);
  padding: var(--input-padding-y) var(--input-padding-x);
  font-size: var(--input-font-size);
  font-family: inherit;
  line-height: 1.5;
  color: var(--input-color);
  background-color: var(--input-bg);
  border: 1px solid var(--input-border-color);
  border-radius: var(--input-border-radius);
  box-sizing: border-box;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

.form-input::placeholder { color: var(--input-placeholder-color); }
.form-input:hover { border-color: #9ca3af; }
.form-input:focus {
  outline: none;
  border-color: #3b82f6;
  box-shadow: var(--input-focus-ring);
}
.form-input:disabled {
  background-color: #f3f4f6;
  color: #6b7280;
  cursor: not-allowed;
}
.form-input[aria-invalid="true"] {
  border-color: #ef4444;
}
.form-input[aria-invalid="true"]:focus {
  box-shadow: 0 0 0 3px rgba(239, 68, 68, 0.4);
}

特殊類型 Input 樣式化

Checkbox / Radio (現代純 CSS 方案)

.form-check {
  position: relative;
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
}
.form-check input {
  position: absolute;
  opacity: 0;
  width: 0;
  height: 0;
}
.form-check .checkmark {
  width: 20px;
  height: 20px;
  border: 2px solid var(--input-border-color);
  border-radius: 4px;  /* radio 用 50% */
  background: var(--input-bg);
  transition: all 0.15s;
}
.form-check input:checked + .checkmark {
  background: #3b82f6;
  border-color: #3b82f6;
}
.form-check input:checked + .checkmark::after {
  content: '';
  position: absolute;
  left: 6px; top: 2px;
  width: 5px; height: 10px;
  border: solid white;
  border-width: 0 2px 2px 0;
  transform: rotate(45deg);
}
.form-check input:focus-visible + .checkmark {
  box-shadow: var(--input-focus-ring);
}

Select 下拉選單 (移除預設箭頭)

.form-select {
  /* 基礎樣式同 .form-input */
  padding-right: 40px;  /* 為自訂箭頭留空間 */
  background-image: url("data:image/svg+xml,%3Csvg xmlns='https://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%236b7280' stroke-width='2'%3E%3Cpath d='M6 9l6 6 6-6'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 12px center;
  background-size: 16px;
  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none;
}

File Upload (隱藏原生、自訂觸發)

.file-upload {
  position: relative;
}
.file-upload input[type="file"] {
  position: absolute;
  inset: 0;
  opacity: 0;
  cursor: pointer;
  width: 100%;
  height: 100%;
}
.file-upload .btn { /* 自訂按鈕樣式 */ }

無障礙檢查清單

  1. 每個 `` 關聯 `
  2. 使用 `aria-describedby` 連結錯誤訊息/提示文字
  3. `aria-invalid="true"` 標記驗證失敗狀態
  4. `autocomplete` 屬性正確設定 (email、tel、address、new-password 等)
  5. 焦點狀態明顯可見 (`:focus-visible`),不移除 outline 無替代方案
  6. 錯誤訊息採用 `role="alert"` 或 `aria-live="polite"` 即時宣告
  7. 觸控目標 ≥ 44×44px (WCAG 2.5.5)
copyright © 萬盛學電腦網 all rights reserved