跳至主要內容 跳至文件導覽

標記(Badge)

標記的文件與範例,這是我們用於小型計數和標籤的元件。

範例(Examples)

標記透過使用相對字體大小和 em 單位來縮放以匹配直接父元素的大小。從 v5 開始,標記不再具有連結的焦點或懸停樣式。

標題(Headings)

Example heading New

Example heading New

Example heading New

Example heading New

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

按鈕(Buttons)

標記可以作為連結或按鈕的一部分使用,以提供計數器。

html
<button type="button" class="btn btn-primary">
  Notifications <span class="badge text-bg-secondary">4</span>
</button>

請注意,根據使用方式,標記可能會讓螢幕閱讀器和類似輔助技術的使用者感到困惑。雖然標記的樣式提供了關於其用途的視覺提示,但這些使用者只會看到標記的內容。根據具體情況,這些標記可能看起來像是句子、連結或按鈕末尾的隨機額外文字或數字。

除非情境很清楚(如「Notifications」範例,可以理解「4」是通知的數量),否則請考慮使用視覺隱藏的額外文字來提供額外的情境說明。

定位(Positioned)

使用通用類別來修改 .badge 並將其定位在連結或按鈕的角落。

html
<button type="button" class="btn btn-primary position-relative">
  Inbox
  <span class="position-absolute top-0 start-100 translate-middle badge rounded-pill bg-danger">
    99+
    <span class="visually-hidden">unread messages</span>
  </span>
</button>

您也可以用一些通用類別替換 .badge 類別,不顯示計數,作為更通用的指示器。

html
<button type="button" class="btn btn-primary position-relative">
  Profile
  <span class="position-absolute top-0 start-100 translate-middle p-2 bg-danger border border-light rounded-circle">
    <span class="visually-hidden">New alerts</span>
  </span>
</button>

背景顏色(Background colors)

Added in v5.2.0

使用我們的 .text-bg-{color} 輔助類別來設定具有對比前景 colorbackground-color。以前需要手動配對您選擇的 .text-{color}.bg-{color} 通用類別來設定樣式,如果您願意,仍然可以使用這種方式。

Primary Secondary Success Danger Warning Info Light Dark
html
<span class="badge text-bg-primary">Primary</span>
<span class="badge text-bg-secondary">Secondary</span>
<span class="badge text-bg-success">Success</span>
<span class="badge text-bg-danger">Danger</span>
<span class="badge text-bg-warning">Warning</span>
<span class="badge text-bg-info">Info</span>
<span class="badge text-bg-light">Light</span>
<span class="badge text-bg-dark">Dark</span>

無障礙提示: 僅使用顏色來傳達意義只提供了視覺指示,這不會傳達給使用輔助技術(如螢幕閱讀器)的使用者。請確保意義從內容本身就很明顯(例如,具有_足夠_色彩對比度的可見文字),或透過替代方式包含,例如使用 .visually-hidden 類別隱藏的額外文字。

膠囊樣式(Pill Badges)

使用 .rounded-pill 通用類別讓標記更圓潤,具有更大的 border-radius

Primary Secondary Success Danger Warning Info Light Dark
html
<span class="badge rounded-pill text-bg-primary">Primary</span>
<span class="badge rounded-pill text-bg-secondary">Secondary</span>
<span class="badge rounded-pill text-bg-success">Success</span>
<span class="badge rounded-pill text-bg-danger">Danger</span>
<span class="badge rounded-pill text-bg-warning">Warning</span>
<span class="badge rounded-pill text-bg-info">Info</span>
<span class="badge rounded-pill text-bg-light">Light</span>
<span class="badge rounded-pill text-bg-dark">Dark</span>

CSS

變數(Variables)

Added in v5.2.0

作為 Bootstrap 持續演進的 CSS 變數方法的一部分,標記現在在 .badge 上使用本地 CSS 變數來增強即時自訂功能。CSS 變數的值透過 Sass 設定,因此 Sass 自訂仍然受支援。

--#{$prefix}badge-padding-x: #{$badge-padding-x};
--#{$prefix}badge-padding-y: #{$badge-padding-y};
@include rfs($badge-font-size, --#{$prefix}badge-font-size);
--#{$prefix}badge-font-weight: #{$badge-font-weight};
--#{$prefix}badge-color: #{$badge-color};
--#{$prefix}badge-border-radius: #{$badge-border-radius};

Sass 變數(Sass variables)

$badge-font-size:                   .75em;
$badge-font-weight:                 $font-weight-bold;
$badge-color:                       $white;
$badge-padding-y:                   .35em;
$badge-padding-x:                   .65em;
$badge-border-radius:               var(--#{$prefix}border-radius);