示例
徽章是微小的静态展示组件,用于为其他元素添加上下文。它们可以作为导航、按钮等的一部分,用于提供计数器或标签。徽章通过使用相对字体大小和 em 单位来缩放以匹配直接父元素的大小。
徽章有三种变体:实心(默认)、柔和和描边。
<span class="badge theme-primary">New</span>
<span class="badge badge-subtle theme-primary">New</span>
<span class="badge badge-outline theme-primary">New</span>标题
徽章会适应父元素的 font-size。
Example heading New
Example heading New
Example heading New
Example heading New
Example heading New
Example heading New
<h1>Example heading <span class="badge">New</span></h1>
<h2>Example heading <span class="badge">New</span></h2>
<h3>Example heading <span class="badge">New</span></h3>
<h4>Example heading <span class="badge">New</span></h4>
<h5>Example heading <span class="badge">New</span></h5>
<h6>Example heading <span class="badge">New</span></h6>按钮
徽章可以作为链接或按钮的一部分,用于提供计数器。
<button type="button" class="btn-solid theme-primary">
Notifications <span class="badge theme-danger">4</span>
</button>请注意,根据使用方式的不同,徽章可能会让屏幕阅读器和类似辅助技术的用户感到困惑。虽然徽章的样式为其用途提供了视觉提示,但这些用户只会看到徽章的内容。根据具体情况,这些徽章可能看起来像是句子、链接或按钮末尾随机附加的单词或数字。
除非上下文清晰(如“Notifications”示例中已理解“4”是通知数量),否则请考虑包含一段视觉上隐藏的额外文本以提供额外的上下文。
定位
使用工具类修改 .badge 并将其定位在链接或按钮的角落。
<button type="button" class="btn-solid theme-primary position-relative">
Inbox
<span class="position-absolute top-0 start-100 translate-middle badge rounded-pill border border-bg theme-danger">
99+
<span class="visually-hidden">unread messages</span>
</span>
</button>你也可以用一些更多的工具类替换 .badge 类,且不带计数,以获得更通用的指示器。
<button type="button" class="btn-solid theme-primary position-relative">
Profile
<span class="position-absolute top-0 start-100 translate-middle p-2 bg-danger border border-bg rounded-circle">
<span class="visually-hidden">New alerts</span>
</span>
</button>变体
使用上下文 .theme-{color} 类为徽章应用语义主题颜色。
<span class="badge theme-primary">Primary</span>
<span class="badge theme-accent">Accent</span>
<span class="badge theme-success">Success</span>
<span class="badge theme-danger">Danger</span>
<span class="badge theme-warning">Warning</span>
<span class="badge theme-info">Info</span>
<span class="badge theme-inverse">Inverse</span>
<span class="badge theme-secondary">Secondary</span><span class="badge badge-subtle theme-primary">Primary</span>
<span class="badge badge-subtle theme-accent">Accent</span>
<span class="badge badge-subtle theme-success">Success</span>
<span class="badge badge-subtle theme-danger">Danger</span>
<span class="badge badge-subtle theme-warning">Warning</span>
<span class="badge badge-subtle theme-info">Info</span>
<span class="badge badge-subtle theme-inverse">Inverse</span>
<span class="badge badge-subtle theme-secondary">Secondary</span><span class="badge badge-outline theme-primary">Primary</span>
<span class="badge badge-outline theme-accent">Accent</span>
<span class="badge badge-outline theme-success">Success</span>
<span class="badge badge-outline theme-danger">Danger</span>
<span class="badge badge-outline theme-warning">Warning</span>
<span class="badge badge-outline theme-info">Info</span>
<span class="badge badge-outline theme-inverse">Inverse</span>
<span class="badge badge-outline theme-secondary">Secondary</span>无障碍提示:使用颜色传达含义
仅使用颜色来增添含义只能提供视觉上的提示,无法传达给屏幕阅读器等辅助技术的用户。请确保含义本身从内容中就能显而易见(例如,具有_足够_颜色对比度的可见文本),或通过其他方式包含,例如使用 .visually-hidden 类隐藏的附加文本。
CSS
变量
Badges use local CSS variables on .badge for real-time customization. Values for the CSS variables are generated from Sass maps unique to each component and applied to the aforementioned class.
$badge-tokens: defaults(
(
--badge-gap: .375em,
--badge-padding-x: .625em,
--badge-padding-y: .25em,
--badge-font-size: clamp(12px, .75em, .75em),
--badge-font-weight: var(--font-weight-semibold),
--badge-color: inherit,
--badge-bg: var(--bg-2),
--badge-border-width: var(--border-width),
--badge-border-color: transparent,
--badge-border-radius: var(--radius-7),
),
$badge-tokens
);