HTML 提供兩種建立按鈕的方式:<button> 與 <input type="button|submit|reset">。兩者最大差異在於 內容模型:<button> 為容器,可包含文字、圖片、圖示、甚至其他行內元素;<input> 為空元素,僅能透過 value 顯示純文字。現代開發強烈推薦使用 <button>,語意更清晰、樣式彈性更大、無障礙支援更完善。
<button type="button"> 普通按鈕,無預設行為
<button type="submit"> 表單送出(預設值)
<button type="reset"> 表單重置
重要: IE 預設type="button",其他瀏覽器與標準預設type="submit"。永遠顯式寫出 type,避免表單誤送出。
button {
/* 移除預設外觀 */
appearance: none;
-webkit-appearance: none;
-moz-appearance: none;
/* 統一基底 */
border: none;
background: transparent;
padding: 0;
margin: 0;
font: inherit; /* 繼承父級字體 */
color: inherit; /* 繼承父級顏色 */
cursor: pointer; /* 手型游標 */
text-align: left; /* 內容靠左(預設 center) */
}
.btn {
display: inline-flex;
align-items: center;
justify-content: center;
gap: 0.5rem;
padding: 0.625rem 1.25rem;
font-size: 0.875rem;
font-weight: 500;
line-height: 1.5;
border-radius: 0.375rem;
border: 1px solid transparent;
transition: all 0.15s ease;
white-space: nowrap;
}
/* 主要按鈕 */
.btn-primary {
background-color: #2563eb;
color: #fff;
border-color: #2563eb;
}
.btn-primary:hover { background-color: #1d4ed8; border-color: #1d4ed8; }
.btn-primary:active { background-color: #1e40af; }
.btn-primary:focus-visible { outline: 2px solid #2563eb; outline-offset: 2px; }
.btn-primary:disabled { opacity: 0.5; cursor: not-allowed; }
/* 次要按鈕(外框) */
.btn-secondary {
background-color: transparent;
color: #374151;
border-color: #d1d5db;
}
.btn-secondary:hover { background-color: #f3f4f6; border-color: #9ca3af; }
/* 危險按鈕 */
.btn-danger {
background-color: #dc2626;
color: #fff;
border-color: #dc2626;
}
.btn-danger:hover { background-color: #b91c1c; border-color: #b91c1c; }
/* 幽靈按鈕 */
.btn-ghost {
background-color: transparent;
color: #2563eb;
border-color: transparent;
}
.btn-ghost:hover { background-color: #eff6ff; }
.btn-sm { padding: 0.375rem 0.75rem; font-size: 0.8125rem; border-radius: 0.25rem; }
.btn-lg { padding: 0.875rem 1.5rem; font-size: 1rem; border-radius: 0.5rem; }
<button class="btn btn-primary">
<svg class="w-4 h-4" aria-hidden="true">...</svg>
<span>下載</span>
</button>
<button class="btn btn-icon" aria-label="刪除">
<svg class="w-5 h-5">...</svg>
</button>
<button class="btn btn-primary" disabled>
<span class="spinner" aria-hidden="true"></span>
<span>處理中...</span>
</button>
若必須使用 <input>(如舊系統相容),樣式控制較受限:
::before/::after 偽元素(空元素)。padding 在某些舊瀏覽器表現不一。appearance: none 重置。input[type="button"],
input[type="submit"],
input[type="reset"] {
appearance: none;
-webkit-appearance: none;
padding: 0.625rem 1.25rem;
/* 其他同 .btn 基礎樣式 */
}
aria-label)。:focus-visible),鍵盤操作可見。disabled 而非 aria-disabled + CSS(原生語意)。| 問題 | 原因 | 解決 |
|---|---|---|
| 按鈕在表單中意外送出 | 未指定 type,預設 submit | 加上 type="button" |
| iOS 灰色圓角無法覆蓋 | -webkit-appearance 預設樣式 | appearance: none; -webkit-appearance: none; |
| Firefox 內部焦點虛線 | ::-moz-focus-inner 預設邊框 | button::-moz-focus-inner { border: 0; } |
| 按鈕高度與 input 不一致 | 盒模型/行高差異 | 統一 line-height、box-sizing: border-box |
按鈕是互動介面的核心元件,投入時間建立一套完整、可復用、無障礙的按鈕系統,將為專案長期維護節省大量成本。選擇 <button>、善用 CSS 變數管理主題色、重視焦點與載入狀態,是專業前端的標準配備。