Bootstrap5 Jumbotron
核对 Bootstrap 5 移除专用 .jumbotron 类后的历史演变,以及使用间距、背景、文本色与圆角工具类组合构建超大屏幕展示区的实测效果。
学完这一章,你能够
- 核对 Bootstrap 5 移除专用 .jumbotron 类后的历史演变,以及使用间距、背景、文本色与圆角工具类组合构建超大屏幕展示区的实测效果。
Jumbotron 的演变与 Bootstrap 5 工具类实现
Jumbotron(超大屏幕)最早于 Bootstrap 3 引入,用于以醒目的大尺寸浅灰背景框突出展示首屏核心信息或号召内容。进入 Bootstrap 5 后,官方正式移除了独立的 `.jumbotron` 组件类,转而提倡通过标准 `<div>` 组合间距(如 `mt-4`、`p-5`)、背景色(如 `bg-primary` 或 `bg-light`)、文字色(`text-white` / `text-dark`)及圆角(`rounded` / `rounded-3`)等工具类按需定制,既减少冗余 CSS 又提升配色自由度。
工具类构建的展示区内部可自由嵌套任意 HTML 标签(如 `<h1>`、`<p>`)与其他 Bootstrap 组件(如按钮 `.btn`)。本站离线 Chromium 实测验证:一是 `<div class="mt-4 p-5 bg-primary text-white rounded">` 计算出 `margin-top: 24px`、四向 `padding: 48px`、主色背景 `rgb(13, 110, 253)`、白色文字与 `4px` 圆角;二是单独写旧版 `<div class="jumbotron">` 在 Bootstrap 5 样式下背景透明且内边距 `0px`,证实该类已无内置样式;三是嵌套 `<h1>`、`<p>` 与 `.btn.btn-light` 及浅灰变体 `.bg-light.text-dark.rounded-3`(背景 `rgb(248, 249, 250)`、圆角 `4.8px`)均正常渲染。
离线 Chromium 核对 Bootstrap 5 工具类超大屏幕样式、旧 .jumbotron 失效与浅灰/嵌套组件变体
展开查看收起
"""Offline Playwright Chromium lab for Bootstrap 5 Chapter 09 (jumbotron)."""
import json
from playwright.sync_api import sync_playwright
BS5_CSS = """
*, *::before, *::after { box-sizing: border-box; }
html { font-size: 16px; }
body { margin: 0; font-family: system-ui, sans-serif; color: #212529; }
.mt-4 { margin-top: 1.5rem !important; }
.p-5 { padding: 3rem !important; }
.bg-primary { background-color: #0d6efd !important; }
.bg-light { background-color: #f8f9fa !important; }
.text-white { color: #fff !important; }
.text-dark { color: #212529 !important; }
.rounded { border-radius: 0.25rem !important; }
.rounded-3 { border-radius: 0.3rem !important; }
.btn { display: inline-block; padding: 0.375rem 0.75rem; font-size: 1rem; border-radius: 0.25rem; border: 1px solid transparent; }
.btn-light { color: #000; background-color: #f8f9fa; border-color: #f8f9fa; }
"""
HTML_DOC = f"""<!DOCTYPE html>
<html lang="zh-CN">
<head><meta charset="utf-8"><style>{BS5_CSS}</style></head>
<body>
<div id="bs5-jumbo" class="mt-4 p-5 bg-primary text-white rounded">
<h1>超大屏幕展示区</h1>
<p>在 Bootstrap 5 中通过间距、背景色与圆角工具类自由组合实现。</p>
<button id="jumbo-btn" type="button" class="btn btn-light">了解更多</button>
</div>
<div id="legacy-jumbo" class="jumbotron">旧版 .jumbotron 类在 Bootstrap 5 中已移除</div>
<div id="light-jumbo" class="mt-4 p-5 bg-light text-dark rounded-3">
<h2>经典浅灰风格</h2>
<p>接近 Bootstrap 4 默认浅灰背景框的视觉效果。</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: Bootstrap 5 utility-built jumbotron styles
c1 = page.evaluate("""() => {
const cs = getComputedStyle(document.getElementById('bs5-jumbo'));
return {
margin_top: cs.marginTop,
padding: [cs.paddingTop, cs.paddingRight, cs.paddingBottom, cs.paddingLeft],
bg: cs.backgroundColor,
color: cs.color,
border_radius: cs.borderRadius
};
}""")
exp1 = {
"margin_top": "24px",
"padding": ["48px", "48px", "48px", "48px"],
"bg": "rgb(13, 110, 253)",
"color": "rgb(255, 255, 255)",
"border_radius": "4px",
}
cases.append({
"name": "utility_jumbotron_styles",
"passed": c1 == exp1,
"expected": exp1,
"actual": c1,
})
# Case 2: Legacy .jumbotron has no built-in styles in BS5
c2 = page.evaluate("""() => {
const cs = getComputedStyle(document.getElementById('legacy-jumbo'));
return {
bg: cs.backgroundColor,
padding_top: cs.paddingTop,
border_radius: cs.borderRadius
};
}""")
exp2 = {
"bg": "rgba(0, 0, 0, 0)",
"padding_top": "0px",
"border_radius": "0px",
}
cases.append({
"name": "bs5_no_builtin_jumbotron_class",
"passed": c2 == exp2,
"expected": exp2,
"actual": c2,
})
# Case 3: Nested HTML/Bootstrap components & light gray variant
c3 = page.evaluate("""() => {
const jb = document.getElementById('bs5-jumbo');
const btn = getComputedStyle(document.getElementById('jumbo-btn'));
const lt = getComputedStyle(document.getElementById('light-jumbo'));
return {
nested_tags: [...jb.children].map(el => el.tagName.toLowerCase()),
nested_btn_bg: btn.backgroundColor,
light_variant_bg: lt.backgroundColor,
light_variant_color: lt.color,
light_variant_radius: lt.borderRadius
};
}""")
exp3 = {
"nested_tags": ["h1", "p", "button"],
"nested_btn_bg": "rgb(248, 249, 250)",
"light_variant_bg": "rgb(248, 249, 250)",
"light_variant_color": "rgb(33, 37, 41)",
"light_variant_radius": "4.8px",
}
cases.append({
"name": "jumbotron_nested_elements_and_light_variant",
"passed": c3 == exp3,
"expected": exp3,
"actual": c3,
})
browser.close()
print(json.dumps({"cases": cases}, ensure_ascii=False))
if __name__ == "__main__":
main()
查看独立实例 ↗ 在独立页面查看源码、运行环境与工程包
本地运行
解压后按 README 说明在对应本地环境运行脚本。
本章附件与实例包
常见错误与边界检查
- 实际端口、文档资源与应用配置都应和测试条件一致。
技术规范与官方资料
练习与参考解答
查看参考解答
Bootstrap 5 提供的间距、背景、边框与圆角工具类已足以灵活拼出任意风格的展示区,移除专用类可避免样式僵化与体积冗余;还原浅灰风格可写 <div class="p-5 mb-4 bg-light rounded-3">(BS5.3 亦可用 bg-body-tertiary)。