小码哥的IT人生

Bootstrap 5 徽章

Bootstrap 2023-08-10 09:26:52小码哥的IT人生shichen

Bootstrap 5 徽章

徽章

徽章(Badges)用于向内容添加附加信息:

<span> 元素中使用 .badge 类和上下文类(如 .bg-secondary)来创建矩形徽章。请注意,徽章会缩放以匹配父元素的大小(如果有):

示例代码:

<h1>Example heading <span class="badge bg-secondary">New</span></h1>
<h2>Example heading <span class="badge bg-secondary">New</span></h2>
<h3>Example heading <span class="badge bg-secondary">New</span></h3>
<h4>Example heading <span class="badge bg-secondary">New</span></h4>
<h5>Example heading <span class="badge bg-secondary">New</span></h5>
<h6>Example heading <span class="badge bg-secondary">New</span></h6>

 

完整实例【亲自试一试】:

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap 实例</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/css/bootstrap.min.css" rel="stylesheet">
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>
<div class="container mt-3">
  <h2>徽章</h2>
  <h1>Example heading <span class="badge bg-secondary">New</span></h1>
  <h2>Example heading <span class="badge bg-secondary">New</span></h2>
  <h3>Example heading <span class="badge bg-secondary">New</span></h3>
  <h4>Example heading <span class="badge bg-secondary">New</span></h4>
  <h5>Example heading <span class="badge bg-secondary">New</span></h5>
  <h6>Example heading <span class="badge bg-secondary">New</span></h6>
</div>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

上下文徽章

请使用任意上下文类 (.bg-*) 来更改徽章的颜色:

示例代码:

<span class="badge bg-primary">主要</span>
<span class="badge bg-secondary">次要</span>
<span class="badge bg-success">成功</span>
<span class="badge bg-danger">危险</span>
<span class="badge bg-warning">警告</span>
<span class="badge bg-info">信息</span>
<span class="badge bg-light">浅色</span>
<span class="badge bg-dark">深色</span>

 

完整实例【亲自试一试】:

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap 实例</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/css/bootstrap.min.css" rel="stylesheet">
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>
<div class="container mt-3">
  <h2>上下文徽章</h2>
  <span class="badge bg-primary">主要</span>
  <span class="badge bg-secondary">次要</span>
  <span class="badge bg-success">成功</span>
  <span class="badge bg-danger">危险</span>
  <span class="badge bg-warning">警告</span>
  <span class="badge bg-info">信息</span>
  <span class="badge bg-light">浅色</span>
  <span class="badge bg-dark">深色</span>
</div>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

胶囊徽章

使用 .rounded-pill 类使徽章更圆润:

示例代码:

<span class="badge rounded-pill bg-primary">主要</span>
<span class="badge rounded-pill bg-secondary">次要</span>
<span class="badge rounded-pill bg-success">成功</span>
<span class="badge rounded-pill bg-danger">危险</span>
<span class="badge rounded-pill bg-warning">警告</span>
<span class="badge rounded-pill bg-info">信息</span>
<span class="badge rounded-pill bg-light">浅色</span>
<span class="badge rounded-pill bg-dark">深色</span>

 

完整实例【亲自试一试】:

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap 实例</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/css/bootstrap.min.css" rel="stylesheet">
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>
<div class="container mt-3">
  <h2>胶囊徽章</h2>
  <span class="badge rounded-pill bg-primary">主要</span>
  <span class="badge rounded-pill bg-secondary">次要</span>
  <span class="badge rounded-pill bg-success">成功</span>
  <span class="badge rounded-pill bg-danger">危险</span>
  <span class="badge rounded-pill bg-warning">警告</span>
  <span class="badge rounded-pill bg-info">信息</span>
  <span class="badge rounded-pill bg-light">浅色</span>
  <span class="badge rounded-pill bg-dark">深色</span>
</div>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

元素内的徽章

在按钮内使用徽章的例子:

示例代码:

<button type="button" class="btn btn-primary">
  消息 <span class="badge bg-danger">4</span>
</button>

 

完整实例【亲自试一试】:

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap 实例</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/css/bootstrap.min.css" rel="stylesheet">
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>
<div class="container mt-3">
  <h2>按钮内的徽章</h2>
  <button type="button" class="btn btn-primary">
    消息 <span class="badge bg-danger">4</span>
  </button>
  <button type="button" class="btn btn-danger">
    通知 <span class="badge bg-dark">7</span>
  </button>
</div>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

版权所有 © 小码哥的IT人生
Copyright © phpcodeweb All Rights Reserved
ICP备案号:苏ICP备17019232号-2  

苏公网安备 32030202000762号

© 2021-2024