標籤(Label)與徽章(Badge)是兩個長得很像、用途也相近的小組件,都是用來傳達補充信息的。本文把二者的使用方式、配色方案與背後的 CSS 實現一次講透。
很多 Web 頁面都會在某個位置掛一枚小標籤,用來提示額外信息。最典型的場景莫過於導航欄新增了一個入口,旁邊綴上一個「new」小標,告訴使用者這是剛上線的功能。

Bootstrap 把這種效果抽離成了獨立組件,用 .label 類名實現高亮顯示。作為獨立組件,它在不同形態的源碼中對應不同文件:
LESS 版本:源文件 label.less
Sass 版本:源文件 _label.scss
編譯後版本:bootstrap.css 第 4261 行~第 4327 行
寫法非常直觀,在行內元素(如 span)上加 label 與配色類名即可:
<h3>Example heading <span class="label label-default">New</span></h3>

核心樣式只有寥寥數行,關鍵是縮小的字號、內邊距與圓角:
.label {
display: inline;
padding: .2em .6em .3em;
font-size: 75%;
font-weight: bold;
line-height: 1;
color: #fff;
text-align: center;
white-space: nowrap;
vertical-align: baseline;
border-radius: .25em;
}
如果標籤是用 a 元素製作的,為了讓 hover 狀態更美觀,需要去掉默認下劃線:
.label[href]:hover, .label[href]:focus {
color: #fff;
text-decoration: none;
cursor: pointer;
}
當標籤內沒有內容時,可以借助 CSS3 的 :empty 偽類把它隱藏起來,避免出現一個空的小色塊:
.label:empty { display: none; }
和按鈕類似,label 也提供了一整套語義化配色:
label-default:預設標籤,深灰色
label-primary:主要標籤,深藍色
label-success:成功標籤,綠色
label-info:信息標籤,淺藍色
label-warning:警告標籤,橙色
label-danger:錯誤標籤,紅色
<span class="label label-default">默認標簽</span> <span class="label label-primary">主要標簽</span> <span class="label label-success">成功標簽</span> <span class="label label-info">信息標簽</span> <span class="label label-warning">警告標簽</span> <span class="label label-danger">錯誤標簽</span>

配色本質上就是替換背景色,並為 a 元素準備一個稍深的 hover 色:
.label-default { background-color: #999; }
.label-default[href]:hover, .label-default[href]:focus { background-color: #808080; }
.label-primary { background-color: #428bca; }
.label-primary[href]:hover, .label-primary[href]:focus { background-color: #3071a9; }
.label-success { background-color: #5cb85c; }
.label-success[href]:hover, .label-success[href]:focus { background-color: #449d44; }
.label-info { background-color: #5bc0de; }
.label-info[href]:hover, .label-info[href]:focus { background-color: #31b0d5; }
.label-warning { background-color: #f0ad4e; }
.label-warning[href]:hover, .label-warning[href]:focus { background-color: #ec971f; }
.label-danger { background-color: #d9534f; }
.label-danger[href]:hover, .label-danger[href]:focus { background-color: #c9302c; }
從視覺效果上看,徽章和標籤幾乎是孿生兄弟,同樣用於提示信息。它最經典的用武之地是未讀消息計數——登入 Twitter 之後,若還有沒看的私信,菜單旁會掛一個數字提醒你。

Bootstrap 把這種效果稱為徽章,用 .badge 類名實現。對應文件:
LESS 版本:源文件 badges.less
Sass 版本:源文件 _badges.scss
編譯後版本:bootstrap.css 第 4328 行~第 4366 行
用法和標籤幾乎一致,用 span 標籤加上 badge 類:
<a href="#">Inbox <span class="badge">42</span></a>
運行效果:

與標籤的方角不同,徽章被設置成了膠囊形(border-radius 給到 10px),並加上了灰色背景:
.badge {
display: inline-block;
min-width: 10px;
padding: 3px 7px;
font-size: 12px;
font-weight: bold;
line-height: 1;
color: #fff;
text-align: center;
white-space: nowrap;
vertical-align: baseline;
background-color: #999;
border-radius: 10px;
}
同樣用 :empty 偽類處理空內容的情況:
.badge:empty { display: none; }
徽章最自然的搭檔是導航條與膠囊形菜單:
<div class="navbar navbar-default" role="navigation"> <div class="navbar-header"> <a href="##" class="navbar-brand">慕課網</a> </div> <ul class="nav navbar-nav"> <li class="active"><a href="##">網站首頁</a></li> <li><a href="##">系列教程</a></li> <li><a href="##">名師介紹</a></li> <li><a href="##">成功案例<span class="badge">23</span></a></li> <li><a href="##">關於我們</a></li> </ul> </div>
運行效果:

<ul class="nav nav-pills"> <li class="active"><a href="#">Home <span class="badge">42</span></a></li> <li><a href="#">Profile</a></li> <li><a href="#">Messages <span class="badge">3</span></a></li> </ul>
運行效果:

相關樣式可查閱 bootstrap.css 第 4345 行~第 4366 行。
需要特別注意的是,徽章組件並不像標籤那樣內建六種配色,預設只有一種灰色。如果需要多色徽章,可以自己修改 badges.less 或 _badges.scss 快速擴充。
選擇上也很清楚:需要傳達「狀態語義」(成功、警告、危險)時用標籤,需要傳達「數量」時用徽章。兩者都記住 :empty 自動隱藏這個細節,就能避免不少邊界情況下的視覺瑕疵。