主题

将三种主题工具类之一与我们的主题颜色类结合使用,即可像组件变体那样,为任何元素设置 color 和 background-color 搭配。

ClassStyles
.theme-contrast--bs-theme-fg: var(--bs-theme-contrast);
--bs-theme-fg-emphasis: var(--bs-theme-contrast);
.theme-subtlebackground-color: var(--bs-theme-bg-subtle);
color: var(--bs-theme-fg);
.theme-muted--bs-theme-fg: var(--bs-theme-fg-emphasis);
.theme-borderborder: var(--bs-border-width) solid var(--bs-theme-border);

概述

取用我们的任意主题颜色类,并添加 .theme-contrast、.theme-subtle 或 .theme-muted,即可设置预定义的 color 和 background-color 搭配。然后可选地添加 .theme-border 以设置预定义的边框颜色。有关主题颜色工作原理的更多信息,请参阅“自定义”部分中的主题文档。

对比

对比样式将 bg 主题令牌与 contrast 文本颜色搭配,通过 .theme-contrast 实现最高可读性。这非常适合具有纯色背景的高强调元素。

.theme-primary .theme-contrast
.theme-accent .theme-contrast
.theme-success .theme-contrast
.theme-danger .theme-contrast
.theme-warning .theme-contrast
.theme-info .theme-contrast
.theme-inverse .theme-contrast
.theme-secondary .theme-contrast
HTML
<div class="p-2 theme-primary theme-contrast">.theme-primary .theme-contrast</div>
<div class="p-2 theme-accent theme-contrast">.theme-accent .theme-contrast</div>
<div class="p-2 theme-success theme-contrast">.theme-success .theme-contrast</div>
<div class="p-2 theme-danger theme-contrast">.theme-danger .theme-contrast</div>
<div class="p-2 theme-warning theme-contrast">.theme-warning .theme-contrast</div>
<div class="p-2 theme-info theme-contrast">.theme-info .theme-contrast</div>
<div class="p-2 theme-inverse theme-contrast">.theme-inverse .theme-contrast</div>
<div class="p-2 theme-secondary theme-contrast">.theme-secondary .theme-contrast</div>

嵌套链接会继承对比前景色,因此它们在纯色背景上依然清晰可读:

Theme Primary with a nested link inside.
Theme Accent with a nested link inside.
Theme Success with a nested link inside.
Theme Danger with a nested link inside.
Theme Warning with a nested link inside.
Theme Info with a nested link inside.
Theme Inverse with a nested link inside.
Theme Secondary with a nested link inside.
HTML
<div class="p-3 theme-primary theme-contrast">Theme Primary with a <a class="underline-opacity-50 hover:underline-opacity-100" href="#">nested link</a> inside.</div>
<div class="p-3 theme-accent theme-contrast">Theme Accent with a <a class="underline-opacity-50 hover:underline-opacity-100" href="#">nested link</a> inside.</div>
<div class="p-3 theme-success theme-contrast">Theme Success with a <a class="underline-opacity-50 hover:underline-opacity-100" href="#">nested link</a> inside.</div>
<div class="p-3 theme-danger theme-contrast">Theme Danger with a <a class="underline-opacity-50 hover:underline-opacity-100" href="#">nested link</a> inside.</div>
<div class="p-3 theme-warning theme-contrast">Theme Warning with a <a class="underline-opacity-50 hover:underline-opacity-100" href="#">nested link</a> inside.</div>
<div class="p-3 theme-info theme-contrast">Theme Info with a <a class="underline-opacity-50 hover:underline-opacity-100" href="#">nested link</a> inside.</div>
<div class="p-3 theme-inverse theme-contrast">Theme Inverse with a <a class="underline-opacity-50 hover:underline-opacity-100" href="#">nested link</a> inside.</div>
<div class="p-3 theme-secondary theme-contrast">Theme Secondary with a <a class="underline-opacity-50 hover:underline-opacity-100" href="#">nested link</a> inside.</div>

柔和

柔和样式将 bg-subtle 主题令牌与 text 文本颜色搭配,以获得对比度更低、更柔和的外观。此组合会自动适应当前的颜色模式。

.theme-primary .theme-subtle
.theme-accent .theme-subtle
.theme-success .theme-subtle
.theme-danger .theme-subtle
.theme-warning .theme-subtle
.theme-info .theme-subtle
.theme-inverse .theme-subtle
.theme-secondary .theme-subtle
HTML
<div class="p-2 theme-primary theme-subtle">.theme-primary .theme-subtle</div>
<div class="p-2 theme-accent theme-subtle">.theme-accent .theme-subtle</div>
<div class="p-2 theme-success theme-subtle">.theme-success .theme-subtle</div>
<div class="p-2 theme-danger theme-subtle">.theme-danger .theme-subtle</div>
<div class="p-2 theme-warning theme-subtle">.theme-warning .theme-subtle</div>
<div class="p-2 theme-info theme-subtle">.theme-info .theme-subtle</div>
<div class="p-2 theme-inverse theme-subtle">.theme-inverse .theme-subtle</div>
<div class="p-2 theme-secondary theme-subtle">.theme-secondary .theme-subtle</div>

带边框的效果:

.theme-primary .theme-subtle .theme-border
.theme-accent .theme-subtle .theme-border
.theme-success .theme-subtle .theme-border
.theme-danger .theme-subtle .theme-border
.theme-warning .theme-subtle .theme-border
.theme-info .theme-subtle .theme-border
.theme-inverse .theme-subtle .theme-border
.theme-secondary .theme-subtle .theme-border
HTML
<div class="p-2 theme-primary theme-subtle theme-border">.theme-primary .theme-subtle .theme-border</div>
<div class="p-2 theme-accent theme-subtle theme-border">.theme-accent .theme-subtle .theme-border</div>
<div class="p-2 theme-success theme-subtle theme-border">.theme-success .theme-subtle .theme-border</div>
<div class="p-2 theme-danger theme-subtle theme-border">.theme-danger .theme-subtle .theme-border</div>
<div class="p-2 theme-warning theme-subtle theme-border">.theme-warning .theme-subtle .theme-border</div>
<div class="p-2 theme-info theme-subtle theme-border">.theme-info .theme-subtle .theme-border</div>
<div class="p-2 theme-inverse theme-subtle theme-border">.theme-inverse .theme-subtle .theme-border</div>
<div class="p-2 theme-secondary theme-subtle theme-border">.theme-secondary .theme-subtle .theme-border</div>

嵌套链接会继承柔和前景色,因此它们在柔和背景上依然清晰可读:

Theme Primary with a nested link inside.
Theme Accent with a nested link inside.
Theme Success with a nested link inside.
Theme Danger with a nested link inside.
Theme Warning with a nested link inside.
Theme Info with a nested link inside.
Theme Inverse with a nested link inside.
Theme Secondary with a nested link inside.
HTML
<div class="p-3 theme-primary theme-subtle">Theme Primary with a <a class="underline-opacity-50 hover:underline-opacity-100" href="#">nested link</a> inside.</div>
<div class="p-3 theme-accent theme-subtle">Theme Accent with a <a class="underline-opacity-50 hover:underline-opacity-100" href="#">nested link</a> inside.</div>
<div class="p-3 theme-success theme-subtle">Theme Success with a <a class="underline-opacity-50 hover:underline-opacity-100" href="#">nested link</a> inside.</div>
<div class="p-3 theme-danger theme-subtle">Theme Danger with a <a class="underline-opacity-50 hover:underline-opacity-100" href="#">nested link</a> inside.</div>
<div class="p-3 theme-warning theme-subtle">Theme Warning with a <a class="underline-opacity-50 hover:underline-opacity-100" href="#">nested link</a> inside.</div>
<div class="p-3 theme-info theme-subtle">Theme Info with a <a class="underline-opacity-50 hover:underline-opacity-100" href="#">nested link</a> inside.</div>
<div class="p-3 theme-inverse theme-subtle">Theme Inverse with a <a class="underline-opacity-50 hover:underline-opacity-100" href="#">nested link</a> inside.</div>
<div class="p-3 theme-secondary theme-subtle">Theme Secondary with a <a class="underline-opacity-50 hover:underline-opacity-100" href="#">nested link</a> inside.</div>

弱化

弱化样式将 bg-muted 主题令牌与 text-emphasis 文本颜色搭配,以获得对比度更低、更弱化的外观。与柔和样式一样,此组合会自动适应当前的颜色模式。

.theme-primary .theme-muted
.theme-accent .theme-muted
.theme-success .theme-muted
.theme-danger .theme-muted
.theme-warning .theme-muted
.theme-info .theme-muted
.theme-inverse .theme-muted
.theme-secondary .theme-muted
HTML
<div class="p-2 theme-primary theme-muted">.theme-primary .theme-muted</div>
<div class="p-2 theme-accent theme-muted">.theme-accent .theme-muted</div>
<div class="p-2 theme-success theme-muted">.theme-success .theme-muted</div>
<div class="p-2 theme-danger theme-muted">.theme-danger .theme-muted</div>
<div class="p-2 theme-warning theme-muted">.theme-warning .theme-muted</div>
<div class="p-2 theme-info theme-muted">.theme-info .theme-muted</div>
<div class="p-2 theme-inverse theme-muted">.theme-inverse .theme-muted</div>
<div class="p-2 theme-secondary theme-muted">.theme-secondary .theme-muted</div>

带边框的效果:

.theme-primary .theme-muted .theme-border
.theme-accent .theme-muted .theme-border
.theme-success .theme-muted .theme-border
.theme-danger .theme-muted .theme-border
.theme-warning .theme-muted .theme-border
.theme-info .theme-muted .theme-border
.theme-inverse .theme-muted .theme-border
.theme-secondary .theme-muted .theme-border
HTML
<div class="p-2 theme-primary theme-muted theme-border">.theme-primary .theme-muted .theme-border</div>
<div class="p-2 theme-accent theme-muted theme-border">.theme-accent .theme-muted .theme-border</div>
<div class="p-2 theme-success theme-muted theme-border">.theme-success .theme-muted .theme-border</div>
<div class="p-2 theme-danger theme-muted theme-border">.theme-danger .theme-muted .theme-border</div>
<div class="p-2 theme-warning theme-muted theme-border">.theme-warning .theme-muted .theme-border</div>
<div class="p-2 theme-info theme-muted theme-border">.theme-info .theme-muted .theme-border</div>
<div class="p-2 theme-inverse theme-muted theme-border">.theme-inverse .theme-muted .theme-border</div>
<div class="p-2 theme-secondary theme-muted theme-border">.theme-secondary .theme-muted .theme-border</div>

嵌套链接会继承弱化前景色,因此它们在弱化背景上依然清晰可读:

Theme Primary with a nested link inside.
Theme Accent with a nested link inside.
Theme Success with a nested link inside.
Theme Danger with a nested link inside.
Theme Warning with a nested link inside.
Theme Info with a nested link inside.
Theme Inverse with a nested link inside.
Theme Secondary with a nested link inside.
HTML
<div class="p-3 theme-primary theme-muted">Theme Primary with a <a class="underline-opacity-50 hover:underline-opacity-100" href="#">nested link</a> inside.</div>
<div class="p-3 theme-accent theme-muted">Theme Accent with a <a class="underline-opacity-50 hover:underline-opacity-100" href="#">nested link</a> inside.</div>
<div class="p-3 theme-success theme-muted">Theme Success with a <a class="underline-opacity-50 hover:underline-opacity-100" href="#">nested link</a> inside.</div>
<div class="p-3 theme-danger theme-muted">Theme Danger with a <a class="underline-opacity-50 hover:underline-opacity-100" href="#">nested link</a> inside.</div>
<div class="p-3 theme-warning theme-muted">Theme Warning with a <a class="underline-opacity-50 hover:underline-opacity-100" href="#">nested link</a> inside.</div>
<div class="p-3 theme-info theme-muted">Theme Info with a <a class="underline-opacity-50 hover:underline-opacity-100" href="#">nested link</a> inside.</div>
<div class="p-3 theme-inverse theme-muted">Theme Inverse with a <a class="underline-opacity-50 hover:underline-opacity-100" href="#">nested link</a> inside.</div>
<div class="p-3 theme-secondary theme-muted">Theme Secondary with a <a class="underline-opacity-50 hover:underline-opacity-100" href="#">nested link</a> inside.</div>

对比

以下是每种主题颜色下三种样式的并排对比,包括嵌套链接:

Primary with a link
Primary with a link
Primary with a link
Accent with a link
Accent with a link
Accent with a link
Success with a link
Success with a link
Success with a link
Danger with a link
Danger with a link
Danger with a link
Warning with a link
Warning with a link
Warning with a link
Info with a link
Info with a link
Info with a link
Inverse with a link
Inverse with a link
Inverse with a link
Secondary with a link
Secondary with a link
Secondary with a link
HTML
<div class="d-flex gap-2 mb-2">
  <div class="theme-primary theme-contrast p-3 flex-fill text-center rounded">Primary with a <a class="underline-opacity-50 hover:underline-opacity-100" href="#">link</a></div>
  <div class="theme-primary theme-subtle p-3 flex-fill text-center rounded">Primary with a <a class="underline-opacity-50 hover:underline-opacity-100" href="#">link</a></div>
  <div class="theme-primary theme-muted p-3 flex-fill text-center rounded">Primary with a <a class="underline-opacity-50 hover:underline-opacity-100" href="#">link</a></div>
</div>
<div class="d-flex gap-2 mb-2">
  <div class="theme-accent theme-contrast p-3 flex-fill text-center rounded">Accent with a <a class="underline-opacity-50 hover:underline-opacity-100" href="#">link</a></div>
  <div class="theme-accent theme-subtle p-3 flex-fill text-center rounded">Accent with a <a class="underline-opacity-50 hover:underline-opacity-100" href="#">link</a></div>
  <div class="theme-accent theme-muted p-3 flex-fill text-center rounded">Accent with a <a class="underline-opacity-50 hover:underline-opacity-100" href="#">link</a></div>
</div>
<div class="d-flex gap-2 mb-2">
  <div class="theme-success theme-contrast p-3 flex-fill text-center rounded">Success with a <a class="underline-opacity-50 hover:underline-opacity-100" href="#">link</a></div>
  <div class="theme-success theme-subtle p-3 flex-fill text-center rounded">Success with a <a class="underline-opacity-50 hover:underline-opacity-100" href="#">link</a></div>
  <div class="theme-success theme-muted p-3 flex-fill text-center rounded">Success with a <a class="underline-opacity-50 hover:underline-opacity-100" href="#">link</a></div>
</div>
<div class="d-flex gap-2 mb-2">
  <div class="theme-danger theme-contrast p-3 flex-fill text-center rounded">Danger with a <a class="underline-opacity-50 hover:underline-opacity-100" href="#">link</a></div>
  <div class="theme-danger theme-subtle p-3 flex-fill text-center rounded">Danger with a <a class="underline-opacity-50 hover:underline-opacity-100" href="#">link</a></div>
  <div class="theme-danger theme-muted p-3 flex-fill text-center rounded">Danger with a <a class="underline-opacity-50 hover:underline-opacity-100" href="#">link</a></div>
</div>
<div class="d-flex gap-2 mb-2">
  <div class="theme-warning theme-contrast p-3 flex-fill text-center rounded">Warning with a <a class="underline-opacity-50 hover:underline-opacity-100" href="#">link</a></div>
  <div class="theme-warning theme-subtle p-3 flex-fill text-center rounded">Warning with a <a class="underline-opacity-50 hover:underline-opacity-100" href="#">link</a></div>
  <div class="theme-warning theme-muted p-3 flex-fill text-center rounded">Warning with a <a class="underline-opacity-50 hover:underline-opacity-100" href="#">link</a></div>
</div>
<div class="d-flex gap-2 mb-2">
  <div class="theme-info theme-contrast p-3 flex-fill text-center rounded">Info with a <a class="underline-opacity-50 hover:underline-opacity-100" href="#">link</a></div>
  <div class="theme-info theme-subtle p-3 flex-fill text-center rounded">Info with a <a class="underline-opacity-50 hover:underline-opacity-100" href="#">link</a></div>
  <div class="theme-info theme-muted p-3 flex-fill text-center rounded">Info with a <a class="underline-opacity-50 hover:underline-opacity-100" href="#">link</a></div>
</div>
<div class="d-flex gap-2 mb-2">
  <div class="theme-inverse theme-contrast p-3 flex-fill text-center rounded">Inverse with a <a class="underline-opacity-50 hover:underline-opacity-100" href="#">link</a></div>
  <div class="theme-inverse theme-subtle p-3 flex-fill text-center rounded">Inverse with a <a class="underline-opacity-50 hover:underline-opacity-100" href="#">link</a></div>
  <div class="theme-inverse theme-muted p-3 flex-fill text-center rounded">Inverse with a <a class="underline-opacity-50 hover:underline-opacity-100" href="#">link</a></div>
</div>
<div class="d-flex gap-2 mb-2">
  <div class="theme-secondary theme-contrast p-3 flex-fill text-center rounded">Secondary with a <a class="underline-opacity-50 hover:underline-opacity-100" href="#">link</a></div>
  <div class="theme-secondary theme-subtle p-3 flex-fill text-center rounded">Secondary with a <a class="underline-opacity-50 hover:underline-opacity-100" href="#">link</a></div>
  <div class="theme-secondary theme-muted p-3 flex-fill text-center rounded">Secondary with a <a class="underline-opacity-50 hover:underline-opacity-100" href="#">link</a></div>
</div>

配合组件

主题工具类与支持主题化的组件配合得非常好。将主题颜色应用于容器,并在各个元素上使用主题样式类。

Contrast badge Subtle badge Muted badge
Contrast badge Subtle badge Muted badge
HTML
<div class="theme-primary">
  <div class="d-flex gap-2 mb-3">
    <span class="badge theme-contrast">Contrast badge</span>
    <span class="badge theme-subtle">Subtle badge</span>
    <span class="badge theme-muted">Muted badge</span>
  </div>
</div>
<div class="theme-danger">
  <div class="d-flex gap-2 mb-3">
    <span class="badge theme-contrast">Contrast badge</span>
    <span class="badge theme-subtle">Subtle badge</span>
    <span class="badge theme-muted">Muted badge</span>
  </div>
</div>

你也可以通过为子元素应用不同的 .theme-{color} 类,在同一容器内混合使用主题颜色:

Success
Warning
Danger
HTML
<div class="d-flex gap-2">
  <div class="theme-success theme-contrast p-3 rounded">Success</div>
  <div class="theme-warning theme-contrast p-3 rounded">Warning</div>
  <div class="theme-danger theme-contrast p-3 rounded">Danger</div>
</div>

重置

当嵌套子树应放弃继承的主题并恢复组件默认值时,请使用 .theme-reset。详情参见主题工具类。

Primary badge Default badge
HTML
<div class="theme-primary p-3 rounded">
  <span class="badge theme-contrast">Primary badge</span>
  <span class="badge theme-reset">Default badge</span>
</div>

CSS

生成的 CSS

主题样式类会生成以下 CSS:

CSS
          .theme-contrast {
  --theme-fg: var(--theme-contrast);
  --theme-fg-emphasis: var(--theme-contrast);
  background-color: var(--theme-bg);
  color: var(--theme-contrast);
}

.theme-subtle {
  background-color: var(--theme-bg-subtle);
  color: var(--theme-fg);
}

.theme-muted {
  --theme-fg: var(--theme-fg-emphasis);
  background-color: var(--theme-bg-muted);
  color: var(--theme-fg-emphasis);
}
        

Sass 工具类 API

主题工具类在我们的工具类 API 中于 scss/_utilities.scss 内声明。了解如何使用工具类 API。

// Theme style utilities - pair with .theme-{color} to apply coordinated bg + text colors
"theme-contrast": (
  property: (
    "background-color": var(--theme-bg),
    "color": var(--theme-contrast)
  ),
  class: theme-contrast,
  values: (null: null)
),
"theme-subtle": (
  property: (
    "background-color": var(--theme-bg-subtle),
    "color": var(--theme-fg)
  ),
  class: theme-subtle,
  values: (null: null)
),
"theme-muted": (
  property: (
    "background-color": var(--theme-bg-muted),
    "color": var(--theme-fg-emphasis)
  ),
  class: theme-muted,
  values: (null: null)
),
"theme-border": (
  property: border,
  class: theme-border,
  values: (null: var(--border-width) solid var(--theme-border))
),