萬盛學電腦網

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

十款零圖片 CSS3 精美按鈕:漸層、立體、發光、Neumorphism

按鈕設計演進

早期依賴圖片 Sprite、滑動門;CSS3 帶來 `border-radius`、`box-shadow`、`gradient`、`transition`、`transform`,實現**零圖片、可縮放、可主題化、易維護**的按鈕系統。

十大風格代碼精華

1. 扁平漸層按鈕 (Material 風)

.btn-flat {
  padding: 10px 24px; border-radius: 4px;
  background: linear-gradient(135deg, #42a5f5, #1e88e5);
  color: #fff; font-weight: 500; border: none; cursor: pointer;
  box-shadow: 0 2px 5px rgba(30,136,229,.4);
  transition: transform .1s, box-shadow .1s;
}
.btn-flat:hover { box-shadow: 0 4px 12px rgba(30,136,229,.5); }
.btn-flat:active { transform: translateY(1px); box-shadow: 0 1px 3px rgba(30,136,229,.4); }

2. 發光脈動按鈕 (CTA 主按鈕)

.btn-glow {
  background: linear-gradient(90deg, #ff6b6b, #ee5a5a);
  box-shadow: 0 0 0 rgba(238,90,90,.4);
  animation: pulse 2s infinite;
}
@keyframes pulse { 50% { box-shadow: 0 0 20px 5px rgba(238,90,90,.6); } }

3. 立體壓縮按鈕 (3D 按下感)

.btn-3d {
  background: linear-gradient(#4a90d9, #3070b0);
  box-shadow: 0 4px 0 #205080, 0 6px 4px rgba(0,0,0,.2);
  transition: all .05s;
}
.btn-3d:active { transform: translateY(4px); box-shadow: 0 0 0 #205080, 0 2px 2px rgba(0,0,0,.2); }

4. Neumorphism 柔性 UI (新拟态)

.btn-neu {
  background: #e0e5ec; border-radius: 12px;
  box-shadow: 8px 8px 16px #bebebe, -8px -8px 16px #fff;
  transition: box-shadow .2s;
}
.btn-neu:active { box-shadow: inset 4px 4px 8px #bebebe, inset -4px -4px 8px #fff; }

5. 邊框動畫按鈕 (Border Flow)

.btn-border {
  position: relative; background: transparent; border: 2px solid transparent;
  background-clip: padding-box; /* 關鍵:背景不繪到邊框下 */
}
.btn-border::before {
  content: ''; position: absolute; inset: -2px;
  border-radius: inherit;
  background: linear-gradient(90deg, #00f, #0ff, #0f0, #ff0, #f0f, #00f);
  background-size: 400%; z-index: -1; animation: borderFlow 3s linear infinite;
}
@keyframes borderFlow { to { background-position: 400% 0; } }

6. 圖標 + 文字響應式按鈕

.btn-icon { display: inline-flex; align-items: center; gap: 8px; }
.btn-icon svg { width: 1em; height: 1em; transition: transform .2s; }
.btn-icon:hover svg { transform: translateX(3px); }

7. 社交登入品牌按鈕 (GitHub/Google/GitLab)

.btn-github { background: #24292e; color: #fff; }
.btn-google { background: #fff; color: #3c4043; border: 1px solid #dadce0; }
.btn-gitlab { background: #fc6d26; color: #fff; }

8. 糖果玻璃質感

.btn-glass {
  background: rgba(255,255,255,.2); backdrop-filter: blur(10px);
  border: 1px solid rgba(255,255,255,.3); box-shadow: 0 8px 32px rgba(0,0,0,.1);
}

9. 條紋載入狀態按鈕

.btn-loading { position: relative; color: transparent; pointer-events: none; }
.btn-loading::after {
  content: ''; position: absolute; inset: 0; border-radius: inherit;
  background: repeating-linear-gradient(-45deg, transparent, transparent 10px, rgba(255,255,255,.3) 10px, rgba(255,255,255,.3) 20px);
  animation: stripe 1s linear infinite;
}

10. 可訪問性完整模板

<button class="btn btn-primary" type="button" aria-pressed="false">
  <svg aria-hidden="true">...</svg> 儲存
</button>

CSS 中 `:focus-visible` 保證鍵盤聚焦可見,`disabled` 狀態自動降低對比度與 `cursor: not-allowed`。

設計系統建議

  • 用 **CSS 變數** 定義色板、圓角、陰影、間距,一鍵切換主題/暗黑模式。
  • 組件庫輸出 **無樣式核心邏輯**,樣式層由設計令牌驅動。
  • 按鈕尺寸採用 **T-shirt Sizing**:`--btn-size-sm: 32px; --btn-size-md: 40px; --btn-size-lg: 48px;`。
copyright © 萬盛學電腦網 all rights reserved