Skip to main content Skip to docs navigation

关闭按钮

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

示例

¥Example

提供一个选项来消除或关闭带有 .btn-close 的组件。默认样式有限,但高度可定制。修改 Sass 变量以替换默认的 background-image。请务必包含屏幕阅读器的文本,就像我们对 aria-label 所做的那样。

¥Provide an option to dismiss or close a component with .btn-close. Default styling is limited, but highly customizable. Modify the Sass variables to replace the default background-image. Be sure to include text for screen readers, as we’ve done with aria-label.

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

禁用状态

¥Disabled state

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

¥Disabled close buttons change their opacity. We’ve also applied pointer-events: none and user-select: none to preventing hover and active states from triggering.

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

深色版本

¥Dark variant

Deprecated in v5.3.0
Heads up! As of v5.3.0, the .btn-close-white class is deprecated. Instead, use data-bs-theme="dark" to change the color mode of the close button.

data-bs-theme="dark" 添加到 .btn-close 或其父元素,以反转关闭按钮。这使用 filter 属性来反转 background-image 而不覆盖其值。

¥Add data-bs-theme="dark" to the .btn-close, or to its parent element, to invert the close button. This uses the filter property to invert the background-image without overriding its value.

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

变量

¥Variables

Added in v5.3.0

作为 Bootstrap 不断发展的 CSS 变量方法的一部分,关闭按钮现在使用 .btn-close 上的本地 CSS 变量来增强实时自定义。CSS 变量的值是通过 Sass 设置的,因此仍然支持 Sass 自定义。

¥As part of Bootstrap’s evolving CSS variables approach, close button now uses local CSS variables on .btn-close for enhanced real-time customization. Values for the CSS variables are set via Sass, so Sass customization is still supported, too.

--#{$prefix}btn-close-color: #{$btn-close-color};
--#{$prefix}btn-close-bg: #{ escape-svg($btn-close-bg) };
--#{$prefix}btn-close-opacity: #{$btn-close-opacity};
--#{$prefix}btn-close-hover-opacity: #{$btn-close-hover-opacity};
--#{$prefix}btn-close-focus-shadow: #{$btn-close-focus-shadow};
--#{$prefix}btn-close-focus-opacity: #{$btn-close-focus-opacity};
--#{$prefix}btn-close-disabled-opacity: #{$btn-close-disabled-opacity};
--#{$prefix}btn-close-white-filter: #{$btn-close-white-filter};

Sass 变量

¥Sass variables

$btn-close-width:            1em;
$btn-close-height:           $btn-close-width;
$btn-close-padding-x:        .25em;
$btn-close-padding-y:        $btn-close-padding-x;
$btn-close-color:            $black;
$btn-close-bg:               url("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' fill='#{$btn-close-color}'><path d='M.293.293a1 1 0 0 1 1.414 0L8 6.586 14.293.293a1 1 0 1 1 1.414 1.414L9.414 8l6.293 6.293a1 1 0 0 1-1.414 1.414L8 9.414l-6.293 6.293a1 1 0 0 1-1.414-1.414L6.586 8 .293 1.707a1 1 0 0 1 0-1.414'/></svg>");
$btn-close-focus-shadow:     $focus-ring-box-shadow;
$btn-close-opacity:          .5;
$btn-close-hover-opacity:    .75;
$btn-close-focus-opacity:    1;
$btn-close-disabled-opacity: .25;
$btn-close-white-filter:     invert(1) grayscale(100%) brightness(200%);