Bootstrap5 颜色
核对 Bootstrap 5 11 类语义文本颜色、50% 透明度文本、链接着色与悬停变暗,以及 8 类背景颜色与 .text-bg-* 组合辅助类的实测色值。
学完这一章,你能够
- 核对 Bootstrap 5 11 类语义文本颜色、50% 透明度文本、链接着色与悬停变暗,以及 8 类背景颜色与 .text-bg-* 组合辅助类的实测色值。
语义文本颜色与半透明文本类
Bootstrap 5 基于 `--bs-*-rgb` 与 `--bs-text-opacity` CSS 变量体系提供 11 个语义文本颜色类:`.text-muted`(柔和灰)、`.text-primary`(主要蓝)、`.text-success`(成功绿)、`.text-info`(信息青)、`.text-warning`(警告黄)、`.text-danger`(危险红)、`.text-secondary`(次要灰)、`.text-dark`(深灰)、`.text-body`(默认正文色)、`.text-light`(浅灰)及 `.text-white`(纯白,在浅色背景上需配深色底)。本站离线实测其计算 RGB 值依次为 `rgb(108, 117, 125)`、`rgb(13, 110, 253)`、`rgb(25, 135, 84)`、`rgb(13, 202, 240)`、`rgb(255, 193, 7)`、`rgb(220, 53, 69)`、`rgb(108, 117, 125)`、`rgb(33, 37, 41)`、`rgb(33, 37, 41)`、`rgb(248, 249, 250)` 与 `rgb(255, 255, 255)`。
需要半透明弱化文字时,可使用 `.text-black-50`(白底上 50% 不透明黑字)与 `.text-white-50`(常与 `.bg-dark` 搭配实现深底半透明白字)。本站实测两者计算色值分别为 `rgba(0, 0, 0, 0.5)` 与 `rgba(255, 255, 255, 0.5)`(配深色底 `rgb(33, 37, 41)`)。
在链接中应用文本颜色与悬停交互
上述 `.text-*` 类可直接加在 `<a>` 标签上改变链接文字颜色;而在 Bootstrap 5 中,若需要带内置 `:hover` / `:focus` 加深反馈的语义链接,官方更推荐使用 `.link-*` 系列(如 `.link-primary`)。本站离线实测:`<a class="text-danger">` 呈现 `rgb(220, 53, 69)`,而对 `.link-primary` 执行真实鼠标悬停后,前景色由默认 `rgb(13, 110, 253)` 变深为 `rgb(10, 88, 202)`。
背景颜色类与前景色搭配原则
背景色工具类包含 `.bg-primary`、`.bg-success`、`.bg-info`、`.bg-warning`、`.bg-danger`、`.bg-secondary`、`.bg-dark` 与 `.bg-light` 八种。需要牢记:`.bg-*` 只修改 `background-color`,不会自动调整文字颜色,因此在深色背景上必须同步搭配 `.text-white`(或在浅灰背景上搭配 `.text-dark`)以保证对比度;Bootstrap 5.2 起另新增了 `.text-bg-*` 复合类自动配对高对比前景色。本站实测单写 `.bg-primary` 时文字仍保持深灰 `rgb(33, 37, 41)`,叠加 `.text-white` 或直接使用 `.text-bg-primary` 后文字即切换为纯白 `rgb(255, 255, 255)`,而 `.text-bg-warning` 自动配黑字 `rgb(0, 0, 0)`。
离线 Chromium 实测 Bootstrap 5 语义文本色、50% 透明文本、链接悬停及背景色配对规则
"""Offline Playwright Chromium lab for Bootstrap 5 Chapter 06 (colors)."""
import json
from playwright.sync_api import sync_playwright
BS5_CSS = """
:root {
--bs-primary-rgb: 13, 110, 253;
--bs-secondary-rgb: 108, 117, 125;
--bs-success-rgb: 25, 135, 84;
--bs-info-rgb: 13, 202, 240;
--bs-warning-rgb: 255, 193, 7;
--bs-danger-rgb: 220, 53, 69;
--bs-light-rgb: 248, 249, 250;
--bs-dark-rgb: 33, 37, 41;
--bs-white-rgb: 255, 255, 255;
--bs-body-color-rgb: 33, 37, 41;
--bs-text-opacity: 1;
--bs-bg-opacity: 1;
}
body { margin: 0; color: #212529; font-family: system-ui, sans-serif; }
.text-muted { color: #6c757d !important; }
.text-primary { color: rgba(var(--bs-primary-rgb), var(--bs-text-opacity)) !important; }
.text-secondary { color: rgba(var(--bs-secondary-rgb), var(--bs-text-opacity)) !important; }
.text-success { color: rgba(var(--bs-success-rgb), var(--bs-text-opacity)) !important; }
.text-info { color: rgba(var(--bs-info-rgb), var(--bs-text-opacity)) !important; }
.text-warning { color: rgba(var(--bs-warning-rgb), var(--bs-text-opacity)) !important; }
.text-danger { color: rgba(var(--bs-danger-rgb), var(--bs-text-opacity)) !important; }
.text-light { color: rgba(var(--bs-light-rgb), var(--bs-text-opacity)) !important; }
.text-dark { color: rgba(var(--bs-dark-rgb), var(--bs-text-opacity)) !important; }
.text-body { color: rgba(var(--bs-body-color-rgb), var(--bs-text-opacity)) !important; }
.text-white { color: rgba(var(--bs-white-rgb), var(--bs-text-opacity)) !important; }
.text-black-50 { color: rgba(0, 0, 0, 0.5) !important; }
.text-white-50 { color: rgba(255, 255, 255, 0.5) !important; }
.link-primary { color: #0d6efd; }
.link-primary:hover { color: #0a58ca; }
.bg-primary { background-color: rgba(var(--bs-primary-rgb), var(--bs-bg-opacity)) !important; }
.bg-secondary { background-color: rgba(var(--bs-secondary-rgb), var(--bs-bg-opacity)) !important; }
.bg-success { background-color: rgba(var(--bs-success-rgb), var(--bs-bg-opacity)) !important; }
.bg-info { background-color: rgba(var(--bs-info-rgb), var(--bs-bg-opacity)) !important; }
.bg-warning { background-color: rgba(var(--bs-warning-rgb), var(--bs-bg-opacity)) !important; }
.bg-danger { background-color: rgba(var(--bs-danger-rgb), var(--bs-bg-opacity)) !important; }
.bg-light { background-color: rgba(var(--bs-light-rgb), var(--bs-bg-opacity)) !important; }
.bg-dark { background-color: rgba(var(--bs-dark-rgb), var(--bs-bg-opacity)) !important; }
.text-bg-primary { color: #fff !important; background-color: RGBA(13, 110, 253, var(--bs-bg-opacity, 1)) !important; }
.text-bg-warning { color: #000 !important; background-color: RGBA(255, 193, 7, var(--bs-bg-opacity, 1)) !important; }
.text-bg-dark { color: #fff !important; background-color: RGBA(33, 37, 41, var(--bs-bg-opacity, 1)) !important; }
"""
HTML_DOC = f"""<!DOCTYPE html>
<html lang="zh-CN">
<head><meta charset="utf-8"><style>{BS5_CSS}</style></head>
<body>
<div id="text-list">
<p data-k="muted" class="text-muted">muted</p>
<p data-k="primary" class="text-primary">primary</p>
<p data-k="success" class="text-success">success</p>
<p data-k="info" class="text-info">info</p>
<p data-k="warning" class="text-warning">warning</p>
<p data-k="danger" class="text-danger">danger</p>
<p data-k="secondary" class="text-secondary">secondary</p>
<p data-k="dark" class="text-dark">dark</p>
<p data-k="body" class="text-body">body</p>
<p data-k="light" class="text-light">light</p>
<p data-k="white" class="text-white">white</p>
</div>
<p id="op-black" class="text-black-50">black 50</p>
<p id="op-white" class="text-white-50 bg-dark">white 50</p>
<a id="a-danger" href="#d" class="text-danger">danger link</a>
<a id="a-link-pri" href="#p" class="link-primary">bs5 link-primary</a>
<div id="bg-list">
<p id="bg-only-primary" class="bg-primary">bg only</p>
<p id="bg-pair-primary" class="bg-primary text-white">bg + text-white</p>
<p id="bg-pair-light" class="bg-light text-dark">bg-light + text-dark</p>
<p id="bg-success" class="bg-success text-white">success</p>
<p id="bg-info" class="bg-info text-white">info</p>
<p id="bg-warning" class="bg-warning text-white">warning</p>
<p id="bg-danger" class="bg-danger text-white">danger</p>
<p id="bg-secondary" class="bg-secondary text-white">secondary</p>
<p id="bg-dark" class="bg-dark text-white">dark</p>
<p id="tbg-primary" class="text-bg-primary">text-bg-primary</p>
<p id="tbg-warning" class="text-bg-warning">text-bg-warning</p>
</div>
</body>
</html>
"""
def launch_browser(pw):
try:
return pw.chromium.launch(headless=True)
except Exception:
return pw.chromium.launch(headless=True, channel="chrome")
def main():
cases = []
with sync_playwright() as p:
browser = launch_browser(p)
page = browser.new_page(viewport={"width": 1000, "height": 800})
page.set_content(HTML_DOC)
# Case 1: All 11 semantic text color classes
c1 = page.evaluate("""() => {
const out = {};
for (const el of document.querySelectorAll('#text-list p')) {
out[el.dataset.k] = getComputedStyle(el).color;
}
return out;
}""")
exp1 = {
"muted": "rgb(108, 117, 125)",
"primary": "rgb(13, 110, 253)",
"success": "rgb(25, 135, 84)",
"info": "rgb(13, 202, 240)",
"warning": "rgb(255, 193, 7)",
"danger": "rgb(220, 53, 69)",
"secondary": "rgb(108, 117, 125)",
"dark": "rgb(33, 37, 41)",
"body": "rgb(33, 37, 41)",
"light": "rgb(248, 249, 250)",
"white": "rgb(255, 255, 255)",
}
cases.append({
"name": "semantic_text_colors",
"passed": c1 == exp1,
"expected": exp1,
"actual": c1,
})
# Case 2: 50% opacity text classes
c2 = page.evaluate("""() => ({
black_50: getComputedStyle(document.getElementById('op-black')).color,
white_50_color: getComputedStyle(document.getElementById('op-white')).color,
white_50_bg: getComputedStyle(document.getElementById('op-white')).backgroundColor
})""")
exp2 = {
"black_50": "rgba(0, 0, 0, 0.5)",
"white_50_color": "rgba(255, 255, 255, 0.5)",
"white_50_bg": "rgb(33, 37, 41)",
}
cases.append({
"name": "text_opacity_50",
"passed": c2 == exp2,
"expected": exp2,
"actual": c2,
})
# Case 3: Colored links & .link-primary hover darkening
danger_col = page.eval_on_selector("#a-danger", "el => getComputedStyle(el).color")
link_before = page.eval_on_selector("#a-link-pri", "el => getComputedStyle(el).color")
page.hover("#a-link-pri")
link_hover = page.eval_on_selector("#a-link-pri", "el => getComputedStyle(el).color")
c3 = {
"text_danger_link": danger_col,
"link_primary_default": link_before,
"link_primary_hover": link_hover,
}
exp3 = {
"text_danger_link": "rgb(220, 53, 69)",
"link_primary_default": "rgb(13, 110, 253)",
"link_primary_hover": "rgb(10, 88, 202)",
}
cases.append({
"name": "link_text_colors_and_hover",
"passed": c3 == exp3,
"expected": exp3,
"actual": c3,
})
# Case 4: Background colors do not alter text color vs .text-bg-* helper
c4 = page.evaluate("""() => {
const pick = id => {
const cs = getComputedStyle(document.getElementById(id));
return { bg: cs.backgroundColor, color: cs.color };
};
return {
bg_only_primary: pick('bg-only-primary'),
bg_pair_primary: pick('bg-pair-primary'),
bg_pair_light: pick('bg-pair-light'),
bg_danger: pick('bg-danger'),
bg_dark: pick('bg-dark'),
text_bg_primary: pick('tbg-primary'),
text_bg_warning: pick('tbg-warning')
};
}""")
exp4 = {
"bg_only_primary": {"bg": "rgb(13, 110, 253)", "color": "rgb(33, 37, 41)"},
"bg_pair_primary": {"bg": "rgb(13, 110, 253)", "color": "rgb(255, 255, 255)"},
"bg_pair_light": {"bg": "rgb(248, 249, 250)", "color": "rgb(33, 37, 41)"},
"bg_danger": {"bg": "rgb(220, 53, 69)", "color": "rgb(255, 255, 255)"},
"bg_dark": {"bg": "rgb(33, 37, 41)", "color": "rgb(255, 255, 255)"},
"text_bg_primary": {"bg": "rgb(13, 110, 253)", "color": "rgb(255, 255, 255)"},
"text_bg_warning": {"bg": "rgb(255, 193, 7)", "color": "rgb(0, 0, 0)"},
}
cases.append({
"name": "background_colors_and_text_bg",
"passed": c4 == exp4,
"expected": exp4,
"actual": c4,
})
browser.close()
print(json.dumps({"cases": cases}, ensure_ascii=False))
if __name__ == "__main__":
main()
- 核对 11 类 .text-* 色值、.text-black-50/.text-white-50 透明度、.link-primary 悬停变深至 rgb(10, 88, 202),以及单写 .bg-primary 不变字色与 .text-bg-* 自动配对。
所需环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
当前源码 SHA256:08d68fd112152ea5aae87d6fc0b4ec95be680f1f9a31dcdb68c4ababe080a1a3
本地运行
解压后按 README 说明在对应本地环境运行脚本。
所需环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
ZIP SHA256:bc5664dc389652f130e5b38997d96cec00ff8d0d30d4ef3b48e53b12d99ef650
常见错误与边界
- 实际端口、文档资源与应用配置都应和测试条件一致。
练习与解答
为什么在 Bootstrap 5 中只给元素添加 .bg-dark 往往会导致文字难以辨认?有哪些规范的解决方式?
展开参考解答
.bg-dark 只设置深色背景而不改变文字颜色,默认正文色(#212529)与深底几乎无对比度;应搭配 .text-white(或 .text-light),在 Bootstrap 5.2+ 中也可直接使用 .text-bg-dark 一步设置背景与高对比前景色。