codecamp

Bootstrap4 徽章(Badges)

徽章(Badges)功能的用途主要是用来突显新的或者未读的项,让该项更加显眼。徽章的使用其实很简单,只需要将 .badge 类加上带有指定意义的颜色类 (如 .badge-secondary) 添加到 <span> 元素上即可。

注: 徽章可以根据父元素的大小的变化而变化:

实例:

<!DOCTYPE html>
<html>
<head>
  <title>Bootstrap4 实例</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/4.1.0/css/bootstrap.min.css">
  <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
  <script src="https://cdn.bootcss.com/popper.js/1.12.5/umd/popper.min.js"></script>
  <script src="https://cdn.bootcss.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
  <h2>徽章</h2>
  <h1>测试标题 <span class="badge badge-secondary">New</span></h1>
  <h2>测试标题 <span class="badge badge-secondary">New</span></h2>
  <h3>测试标题 <span class="badge badge-secondary">New</span></h3>
  <h4>测试标题 <span class="badge badge-secondary">New</span></h4>
  <h5>测试标题 <span class="badge badge-secondary">New</span></h5>
  <h6>测试标题 <span class="badge badge-secondary">New</span></h6>
</div>
</body>
</html>


各种颜色类型的徽章

以下列出了所有颜色类型的徽章:

<!DOCTYPE html>
<html>
<head>
  <title>Bootstrap4 实例</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/4.1.0/css/bootstrap.min.css">
  <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
  <script src="https://cdn.bootcss.com/popper.js/1.12.5/umd/popper.min.js"></script>
  <script src="https://cdn.bootcss.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
  <h2>各种颜色类型的徽章</h2>
  <span class="badge badge-primary">主要</span>
  <span class="badge badge-secondary">次要</span>
  <span class="badge badge-success">成功</span>
  <span class="badge badge-danger">危险</span>
  <span class="badge badge-warning">警告</span>
  <span class="badge badge-info">信息</span>
  <span class="badge badge-light">浅色</span>
  <span class="badge badge-dark">深色</span>
</div>
</body>
</html>


药丸形状徽章

使用 .badge-pill 类来设置药丸形状徽章:

<!DOCTYPE html>
<html>
<head>
  <title>Bootstrap4 实例</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/4.1.0/css/bootstrap.min.css">
  <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
  <script src="https://cdn.bootcss.com/popper.js/1.12.5/umd/popper.min.js"></script>
  <script src="https://cdn.bootcss.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
  <h2>药丸形状徽章</h2>
  <span class="badge badge-pill badge-default">默认</span>
  <span class="badge badge-pill badge-primary">主要</span>
  <span class="badge badge-pill badge-success">成功</span>
  <span class="badge badge-pill badge-info">信息</span>
  <span class="badge badge-pill badge-warning">警告</span>
  <span class="badge badge-pill badge-danger">危险</span>
</div>
</body>
</html>


徽章插入到元素内

以下实例将徽章嵌入到按钮内:

<!DOCTYPE html>
<html>
<head>
  <title>Bootstrap4 实例</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/4.1.0/css/bootstrap.min.css">
  <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
  <script src="https://cdn.bootcss.com/popper.js/1.12.5/umd/popper.min.js"></script>
  <script src="https://cdn.bootcss.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
  <h2>徽章嵌入到按钮内</h2>
  <button type="button" class="btn btn-primary">
    Messages <span class="badge badge-light">4</span>
  </button>
  <button type="button" class="btn btn-danger">
    Notifications <span class="badge badge-light">7</span>
  </button>
</div>
</body>
</html>


Bootstrap4 按钮组
Bootstrap4 进度条
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }