Bootstrap 简明教程
Bootstrap - Close Button
本章将讨论 Bootstrap 关闭按钮。关闭按钮用于关闭内容,例如模态窗口、警报和弹出层。
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.
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/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>
<div class="container mt-2">
Close Notification
<button type="button" class="btn-close" aria-label="Close"></button>
</div>
</body>
</html>
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/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>
<div class="container mt-2">
Close Notification
<button type="button" class="btn-close" disabled aria-label="Close"></button>
</div>
</body>
</html>
Dark variant
在 data-bs-theme="dark" 中加入 .btn-close 类或其父元素以反转关闭按钮。 filter 属性用于反转 background-image 。
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.
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/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>
Close Notification
<button type="button" class="btn-close" data-bs-theme="dark" aria-label="Close"></button>
<button type="button" class="btn-close" data-bs-theme="dark" disabled aria-label="Close"></button>
</body>
</html>