A decade of Canvas at your command, powered by our custom AI engineStart building
Bootstrap 5Utilities

Bootstrap 5 Close Button

The Bootstrap 5 Close Button is a pre-styled dismissal control rendered as a transparent button with an embedded × icon, built using a CSS mask rather than an icon font. It is used wherever users need to explicitly dismiss or close a UI element — modals, alerts, offcanvas panels, and toast notifications. Bootstrap ships it as a standalone component so it can be dropped into any container without pulling in additional dependencies.

Primary Class

.close-button

Common Use Cases

  • Dismissing a cookie consent alert banner fixed to the bottom of the viewport
  • Closing an offcanvas navigation drawer on mobile when the user taps outside or finishes browsing
  • Allowing users to manually dismiss a toast notification confirming a successful form submission
  • Removing individual filter tags from a search results interface where each tag has its own close control

Variants & Classes

VariantDescription
Close Button DefaultStandard close button with Bootstrap's default styling.
Close Button ResponsiveResponsive variant that adapts to different screen sizes.

Code Example

<div class="alert alert-warning alert-dismissible fade show" role="alert">
  <strong>Session expiring soon.</strong> Your session will expire in 5 minutes. Save your work to avoid losing changes.
  <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>

<!-- Standalone close button (disabled state) -->
<button type="button" class="btn-close" aria-label="Close"></button>
<button type="button" class="btn-close" disabled aria-label="Close"></button>

<!-- White variant for dark backgrounds -->
<div class="bg-dark p-3 d-inline-block rounded">
  <button type="button" class="btn-close btn-close-white" aria-label="Close"></button>
</div>

Live Examples

Basic Close Button

Example 1

Canvas Framework Variants

The Canvas template extends Bootstrap 5 with 1,658+ component variants. Generate any of these using Canvas Builder:

  • Canvas Builder generated close button with custom colours
  • Close Button with interactive states
  • Responsive close button for all screen sizes

Best Practices

Always include aria-label="Close"

The close button renders no visible text, so screen readers have nothing to announce without the aria-label attribute. Bootstrap's own docs require it, and omitting it will fail WCAG 2.1 accessibility audits.

Use btn-close-white on dark surfaces

The default close button uses a dark × icon via CSS mask-image, which is invisible on dark backgrounds. Adding the btn-close-white modifier inverts the icon — use it inside dark alerts, dark modals, or any container with a dark background color.

Pair data-bs-dismiss with the correct parent selector

Bootstrap's dismiss plugin walks up the DOM to find the closest element matching the component type (alert, modal, toast). If your close button is nested inside a custom wrapper between the button and the dismissible parent, the plugin may not find it — keep the nesting shallow or trigger dismissal manually via JavaScript.

Disable the button during async operations

If closing triggers an API call (e.g. marking a notification as read), add the disabled attribute immediately on click to prevent double-submission. Re-enable or remove the element once the request resolves.

FAQ

How does the Bootstrap 5 close button work without an icon font?
Bootstrap 5 dropped icon font dependencies entirely. The btn-close class applies a CSS mask-image using an inline SVG data URI as the mask source, with background-color set to currentColor. This means the × shape is actually the background color showing through a mask cutout — which is why inverting it only requires changing the background color, as btn-close-white does by setting background-color to #fff.
Can I resize or restyle the close button beyond the white variant?
Yes. Since the icon is rendered via background and mask properties, you can override its size using width and height (default is 1em × 1em), change the color by setting color or directly targeting background-color on .btn-close, and adjust opacity via the --bs-btn-close-opacity CSS variable. For a larger hit target on mobile, increase padding rather than the icon size itself — Bootstrap applies 0.25em padding by default which you can override in your stylesheet.
How does Canvas Builder handle Close Button generation?
Canvas Builder automatically includes the btn-close component whenever it generates a dismissible element — alerts, modals, toasts, and offcanvas — and wires up the correct data-bs-dismiss attribute for the parent component type. It applies your brand's foreground color to the button's CSS custom properties so the icon inherits your theme rather than defaulting to Bootstrap's generic dark or white variants.