跳转到内容
按钮·免费

按钮组(HTML + CSS)

纯 CSS 写的主要、次要、危险和低调四种按钮,另有禁用和加载状态。每个按钮至少 44 × 44 px,用键盘操作时显示焦点环。深色模式会换一套颜色,对比度不变差。

免费安装。

如果你的 Android 设备上装了 PocketCode,这会在应用里打开此页面。

预览

buttons.html · 77 行
<!--
  Button set in plain HTML and CSS: primary, secondary, danger and quiet, plus a loading state.
  Every button is at least 44 x 44 px and shows a focus ring when reached with the keyboard.
  While a request runs, set aria-busy="true" and aria-disabled="true" on the button: the label is hidden
  behind a spinner but screen readers still read it. aria-disabled does not block clicks, so your click
  handler should return early while aria-busy is set. Remove both attributes when it finishes.
  Colours live in the CSS variables; dark mode follows the system setting.
-->
<style>
  .btn {
    --btn-bg: #1f5fd1;
    --btn-fg: #ffffff;
    --btn-border: transparent;
    --btn-ring: #1f5fd1;
    position: relative;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    min-width: 44px;
    min-height: 44px;
    padding: 10px 18px;
    border: 1px solid var(--btn-border);
    border-radius: 10px;
    background: var(--btn-bg);
    color: var(--btn-fg);
    font: 600 15px/1.2 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
    text-decoration: none;
    cursor: pointer;
    transition: filter 0.15s ease;
  }
  .btn-secondary { --btn-bg: transparent; --btn-fg: #1f5fd1; --btn-border: #7c7c88; }
  .btn-danger { --btn-bg: #b3261e; --btn-fg: #ffffff; --btn-ring: #b3261e; }
  .btn-quiet { --btn-bg: transparent; --btn-fg: #1b1b1f; --btn-ring: #1b1b1f; }

  @media (prefers-color-scheme: dark) {
    .btn { --btn-bg: #8ab4ff; --btn-fg: #0b1a33; --btn-ring: #8ab4ff; }
    .btn-secondary { --btn-bg: transparent; --btn-fg: #8ab4ff; --btn-border: #7c7c88; }
    .btn-danger { --btn-bg: #ffb4ab; --btn-fg: #5c0f0a; --btn-ring: #ffb4ab; }
    .btn-quiet { --btn-bg: transparent; --btn-fg: #ececf1; --btn-ring: #ececf1; }
  }

  .btn:hover { filter: brightness(1.1); }
  .btn:active { filter: brightness(0.92); }
  .btn:focus-visible { outline: 2px solid var(--btn-ring); outline-offset: 2px; }
  .btn:disabled,
  .btn[aria-disabled="true"] { opacity: 0.5; cursor: not-allowed; filter: none; }

  .btn[aria-busy="true"] { opacity: 1; color: transparent; cursor: progress; }
  .btn[aria-busy="true"]::after {
    content: "";
    position: absolute;
    width: 18px;
    height: 18px;
    border: 2px solid var(--btn-fg);
    border-right-color: transparent;
    border-radius: 50%;
    animation: btn-spin 0.8s linear infinite;
  }
  @keyframes btn-spin { to { transform: rotate(360deg); } }
  @media (prefers-reduced-motion: reduce) {
    .btn { transition: none; }
    .btn[aria-busy="true"]::after { animation-duration: 2.4s; }
  }

  .btn-row { display: flex; flex-wrap: wrap; gap: 12px; }
</style>

<div class="btn-row">
  <button class="btn" type="button">Save changes</button>
  <button class="btn btn-secondary" type="button">Cancel</button>
  <button class="btn btn-danger" type="button">Delete</button>
  <button class="btn btn-quiet" type="button">Skip for now</button>
  <button class="btn" type="button" aria-busy="true" aria-disabled="true">Saving</button>
  <button class="btn" type="button" disabled>Not available</button>
  <a class="btn btn-secondary" href="#pricing">See pricing</a>
</div>

1.0.0 的变更

首个版本。

相关