Close Button

This chapter will discuss about Bootstrap close button. Close button is used for dismissing content like modals, alerts, and popovers.

Basic example

  • Use .btn-close for creating a close button. Default styling is customizable.
  • Change Sass variables to change background-image and add text for screen readers using aria-label.
https://www.tutorialspoint.com/bootstrap/examples/closebutton_basic_example.php

Example

You can edit and try running this code using Edit & Run option.

<!DOCTYPE html><html lang="en"><head><title>Bootstrap - Close Button</title><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"><script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script></head><body><div class="container mt-2">
    Close Notification
    &lt;button type="button" class="btn-close" aria-label="Close"&gt;&lt;/button&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

Disabled state

  • Disable close buttons with disabled attribute. Bootstrap also applies pointer-events: none; and user-select: none; to prevent triggering of hover and active states.
  • The opacity of disabled close buttons is changed.

Example

You can edit and try running this code using Edit & Run option.

<!DOCTYPE html><html lang="en"><head><title>Bootstrap - Close Button</title><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"><script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script></head><body><div class="container mt-2">
    Close Notification
    &lt;button type="button" class="btn-close" disabled aria-label="Close"&gt;&lt;/button&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

Dark variant

Take note! The .btn-close-white class is deprecated as of v5.3.0. Use data-bs-theme="dark" for the close button color mode.

To invert the close button, add data-bs-theme="dark" to .btn-close class or to it's parent elements. The filter property is used to invert the background-image.https://www.tutorialspoint.com/bootstrap/examples/closebutton_dark_variant.php

Example

You can edit and try running this code using Edit & Run option.

<!DOCTYPE html><html lang="en"><head><title>Bootstrap - Close Button</title><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"><script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script></head><body>
    Close Notification
    &lt;button type="button" class="btn-close" data-bs-theme="dark" aria-label="Close"&gt;&lt;/button&gt;&lt;button type="button" class="btn-close" data-bs-theme="dark" disabled aria-label="Close"&gt;&lt;/button&gt;&lt;/body&gt;&lt;/html&gt;</pre>

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *