萬盛學電腦網

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

HTML <button> 與 <input type="button"> 顏色樣式完全指南

前言:兩種按鈕元素的異同

HTML 提供兩種建立按鈕的方式:<button> 與 <input type="button|submit|reset">。兩者最大差異在於 內容模型:<button> 為容器,可包含文字、圖片、圖示、甚至其他行內元素;<input> 為空元素,僅能透過 value 顯示純文字。現代開發強烈推薦使用 <button>,語意更清晰、樣式彈性更大、無障礙支援更完善。

核心屬性:type 必須明確宣告

<button type="button">  普通按鈕,無預設行為
<button type="submit">  表單送出(預設值)
<button type="reset">   表單重置
重要: IE 預設 type="button",其他瀏覽器與標準預設 type="submit"。永遠顯式寫出 type,避免表單誤送出。

CSS 樣式控制:從基礎到進階

1. 基礎重置:消除瀏覽器預設差異

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) */
}

2. 主要/次要/危險按鈕設計系統

.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; }

3. 尺寸變體

.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; }

4. 圖示按鈕

<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>

5. 載入狀態

<button class="btn btn-primary" disabled>
  <span class="spinner" aria-hidden="true"></span>
  <span>處理中...</span>
</button>

input[type=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(原生語意)。
  • [ ] 顏色非唯一資訊傳遞手段(搭配圖示、文字)。
  • [ ] 觸控目標 ≥ 44×44px(WCAG 2.5.5)。

常見陷阱與解決方案

問題原因解決
按鈕在表單中意外送出未指定 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 變數管理主題色、重視焦點與載入狀態,是專業前端的標準配備。

copyright © 萬盛學電腦網 all rights reserved