Skip to main content

Components

Close button

A generic close button for dismissing alerts, modals, offcanvas and toasts — the thing it dismisses is native.

How it works

.btn-close is only an icon. What it closes depends on where it lives: inside a <form method="dialog"> it closes the surrounding <dialog> (alerts); with popovertarget and popovertargetaction="hide" it closes a popover (modal, offcanvas, toasts). The data-bs-dismiss attribute is inert.

Example

Always give it an aria-label; the element has no text.

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

Disabled state

Disabled close buttons drop to 25 % opacity and lose pointer events.

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

Dark variant

On a dark background, either add data-bs-theme="dark" to the button or an ancestor, or use the legacy .btn-close-white class. Both set --bs-btn-close-filter to invert the icon. In the theme's dark mode (explicit or automatic) the inversion is applied for you.

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>
HTML
<button type="button" class="btn-close btn-close-white" aria-label="Close"></button>
<button type="button" class="btn-close btn-close-white" disabled aria-label="Close"></button>

Closing a dialog

Put the button in a <form method="dialog"> as a submit button. Submitting the form closes the nearest open <dialog> — no script, and it works for any dialog that has the open attribute.

Ringside seats are sold out. Balcony still available.
HTML
<dialog open class="alert alert-info alert-dismissible" aria-label="Notice">
  Ringside seats are sold out. Balcony still available.
  <form method="dialog">
    <button type="submit" class="btn-close" aria-label="Close"></button>
  </form>
</dialog>

For a modal opened by popovertarget, use the popover attributes instead. The modal page shows the combined pattern that works for both paths.

<button type="button" class="btn-close" popovertarget="my-modal" popovertargetaction="hide" aria-label="Close"></button>

Accessibility

  • Use a real <button>. It is focusable and activates with Enter and Space.
  • The aria-label is required. "Close" is fine; "Close notice" is better when several are on screen.
  • The focus ring is the theme's gold ring at 40 % opacity.

CSS

.btn-close {
  --bs-btn-close-color: #000;
  --bs-btn-close-opacity: 0.5;
  --bs-btn-close-hover-opacity: 0.75;
  --bs-btn-close-focus-shadow: 0 0 0 0.25rem rgba(184, 148, 31, 0.4);   // gold focus ring
  --bs-btn-close-focus-opacity: 1;
  --bs-btn-close-disabled-opacity: 0.25;
}

[data-bs-theme="dark"],
.btn-close-white {
  --bs-btn-close-filter: invert(1) grayscale(100%) brightness(200%);
}