焦点环

用于为元素和组件添加并修改自定义焦点环样式的工具类。

.focus-ring 辅助类会将焦点状态下的默认 outline 修改为更易于自定义的样式。焦点环由一系列 CSS 变量组成,这些变量继承自 :root 层级,可为任何元素或组件进行修改。

示例

使用 Tab 键进入下方示例即可看到焦点环的效果,或单击下方示例后按 Tab。

HTML
<a href="#" class="d-inline-flex focus-ring py-1 px-2 text-decoration-none border rounded-2">
  自定义焦点环
</a>

变体

使用主题色工具类更改焦点环的颜色。该辅助类会引入给定主题色对应的 --bs-theme-focus-ring CSS 变量。

HTML
<a href="#" class="d-inline-flex focus-ring theme-primary py-1 px-2 text-decoration-none border rounded-2">Primary focus</a>
<a href="#" class="d-inline-flex focus-ring theme-accent py-1 px-2 text-decoration-none border rounded-2">Accent focus</a>
<a href="#" class="d-inline-flex focus-ring theme-success py-1 px-2 text-decoration-none border rounded-2">Success focus</a>
<a href="#" class="d-inline-flex focus-ring theme-danger py-1 px-2 text-decoration-none border rounded-2">Danger focus</a>
<a href="#" class="d-inline-flex focus-ring theme-warning py-1 px-2 text-decoration-none border rounded-2">Warning focus</a>
<a href="#" class="d-inline-flex focus-ring theme-info py-1 px-2 text-decoration-none border rounded-2">Info focus</a>
<a href="#" class="d-inline-flex focus-ring theme-inverse py-1 px-2 text-decoration-none border rounded-2">Inverse focus</a>
<a href="#" class="d-inline-flex focus-ring theme-secondary py-1 px-2 text-decoration-none border rounded-2">Secondary focus</a>

自定义颜色

按需修改 --bs-focus-ring-color CSS 变量以更改默认外观。可使用现有的主题变量或你自己的颜色。使用自己的颜色时,我们建议使用 light-dark() 以支持颜色模式,并使用 color-mix() 确保颜色略有偏差,以便在不同背景上获得更高的对比度。

HTML
<a href="#" class="d-inline-flex focus-ring py-1 px-2 text-decoration-none border rounded-2" style="--bs-focus-ring-color: var(--bs-success-focus-ring)">
  绿色焦点环
</a>