关闭按钮

用于关闭模态框和警告框等内容的通用关闭按钮。

Layer: components

示例

使用 .btn-close 提供一个关闭或用于关闭组件的选项。该图标通过 CSS mask-image 渲染,因此它会继承 .btn-close 类上的 color——无需内联 SVG。请务必为屏幕阅读器包含文本,就像我们使用 aria-label 所做的那样。

HTML
<button type="button" class="btn-close" aria-label="Close"></button>

禁用状态

禁用的关闭按钮会改变其 opacity。我们还应用了 pointer-events: none 和 user-select: none 以防止触发悬停和激活状态。

HTML
<button type="button" class="btn-close" disabled aria-label="Close"></button>

深色变体

将 data-bs-theme="dark" 添加到 .btn-close 或其父元素上,以反转关闭按钮。在深色模式下会自动发生。

HTML
<div data-bs-theme="dark">
  <button type="button" class="btn-close" aria-label="Close"></button>
  <button type="button" class="btn-close" disabled aria-label="Close"></button>
</div>

CSS

变量

Close button use local CSS variables on .btn-close for real-time customization. Values for the CSS variables are generated from Sass maps unique to each component and applied to the aforementioned class.

// stylelint-disable-next-line scss/dollar-variable-default
$btn-close-tokens: defaults(
  (
    --btn-close-size: 1.25rem,
    --btn-close-color: var(--fg-body),
    --btn-close-icon: #{escape-svg(url("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'><path d='M12 0a4 4 0 0 1 4 4v8a4 4 0 0 1-4 4H4a4 4 0 0 1-4-4V4a4 4 0 0 1 4-4zm-.646 4.646a.5.5 0 0 0-.707 0L8 7.293 5.354 4.646a.5.5 0 1 0-.708.708L7.293 8l-2.647 2.647a.5.5 0 1 0 .708.707L8 8.707l2.647 2.646a.5.5 0 1 0 .707-.707L8.707 8l2.646-2.646a.5.5 0 0 0 0-.708z'/></svg>"))},
    --btn-close-opacity: .5,
    --btn-close-hover-opacity: .75,
    --btn-close-focus-opacity: .85,
    --btn-close-disabled-opacity: .25,
  ),
  $btn-close-tokens
);