蓝色天空.知识 · 实践 · 创造
我的收藏
Bootstrap4 教程目录 35章
Bootstrap4 / LEARN BY DOING

Bootstrap4 Jumbotron

核对 Bootstrap 4 基础圆角巨幕 .jumbotron 的响应式内边距与嵌套组件承载能力,以及全宽无圆角 .jumbotron-fluid 配合内层容器的布局实测。

学完这一章,你能够

  • 核对 Bootstrap 4 基础圆角巨幕 .jumbotron 的响应式内边距与嵌套组件承载能力,以及全宽无圆角 .jumbotron-fluid 配合内层容器的布局实测。

基础巨幕 .jumbotron 样式与内容承载

Jumbotron(巨幕/超大屏幕)是用于突出呈现网站核心简介、主标题或行动号召的浅灰背景展示区块。在 `<div>` 上添加 `.jumbotron` 即可创建,内部既可以放置 `<h1>`、`<p>`、`<hr>` 等原生 HTML 标签,也可以直接嵌套按钮等 Bootstrap 组件。本站离线 Chromium 实测显示:置于 `.container` 内的 `.jumbotron` 背景为 `rgb(233, 236, 239)`(`#e9ecef`)、圆角 `4.8px`(`0.3rem`)、下外边距 `32px`(`2rem`),在 `992px` 视口下内容宽度为 `930px`;其内边距具备响应式切换——在 `375px` 小屏为 `32px 16px`(`2rem 1rem`),在 `768px` 及 `≥576px` 宽屏自动扩充为 `64px 32px`(`4rem 2rem`),内部标题、段落、分隔线与主色按钮按纵坐标严格顺序排列。

全宽无圆角巨幕 .jumbotron-fluid

当需要横贯整个视口宽度且去除边角弧度的通栏巨幕时,可同时挂载 `.jumbotron.jumbotron-fluid`,并在其内部再嵌套一个 `.container` 或 `.container-fluid` 来控制正文边界。本站在 `1200px` 视口实测确认:外层 `.jumbotron.jumbotron-fluid` 宽度撑满 `1200px`、`border-radius` 归零为 `0px`、左右内边距为 `0px`,而内层 `.container` 将正文稳定约束在 `1140px` 居中区域(左偏移 `30px`)。

离线验证 Bootstrap 4 基础圆角巨幕与全宽无圆角 .jumbotron-fluid

含完整源码 · 136 行
展开查看收起
python
"""Offline Playwright Chromium lab for Bootstrap 4 Ch 08 (jumbotron)."""
import json
from playwright.sync_api import sync_playwright

BS4_JUMBO_CSS = """
*, *::before, *::after { box-sizing: border-box; }
html { font-size: 16px; }
body { margin: 0; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 1rem; line-height: 1.5; color: #212529; background: #fff; }
h1 { margin-top: 0; margin-bottom: 0.5rem; font-size: 2.5rem; font-weight: 500; line-height: 1.2; }
p { margin-top: 0; margin-bottom: 1rem; }
hr { margin: 1.5rem 0; border: 0; border-top: 1px solid rgba(0, 0, 0, 0.1); }
.container { width: 100%; padding-right: 15px; padding-left: 15px; margin-right: auto; margin-left: auto; }
@media (min-width: 576px) { .container { max-width: 540px; } }
@media (min-width: 768px) { .container { max-width: 720px; } }
@media (min-width: 992px) { .container { max-width: 960px; } }
@media (min-width: 1200px) { .container { max-width: 1140px; } }
.jumbotron { padding: 2rem 1rem; margin-bottom: 2rem; background-color: #e9ecef; border-radius: 0.3rem; }
@media (min-width: 576px) { .jumbotron { padding: 4rem 2rem; } }
.jumbotron-fluid { padding-right: 0; padding-left: 0; border-radius: 0; }
.btn { display: inline-block; padding: 0.375rem 0.75rem; font-size: 1rem; line-height: 1.5; border-radius: 0.25rem; text-decoration: none; border: 1px solid transparent; }
.btn-primary { color: #fff; background-color: #007bff; border-color: #007bff; }
"""

HTML_PAGE = f"""<!DOCTYPE html>
<html lang="zh-CN">
<head><meta charset="utf-8"><style>{BS4_JUMBO_CSS}</style></head>
<body>
  <div class="container">
    <div id="jb-basic" class="jumbotron">
      <h1 id="jb-h1">Bootstrap4 巨幕展示区</h1>
      <p id="jb-p">用于突出呈现页面核心导语与关键操作入口。</p>
      <hr id="jb-hr">
      <a id="jb-btn" class="btn btn-primary" href="#">了解更多</a>
    </div>
  </div>
  <div id="jb-fluid" class="jumbotron jumbotron-fluid">
    <div id="jb-fluid-inner" class="container">
      <h1>全宽无圆角巨幕</h1>
      <p>背景铺满整个视口宽度,内部正文仍居中对齐。</p>
    </div>
  </div>
</body>
</html>
"""


def launch_browser(p):
    try:
        return p.chromium.launch(headless=True)
    except Exception:
        return p.chromium.launch(headless=True, channel="chrome")


def main():
    cases = []
    with sync_playwright() as p:
        browser = launch_browser(p)
        page = browser.new_page(viewport={"width": 992, "height": 900})
        page.set_content(HTML_PAGE)

        # Case 1: basic_jumbotron_styles
        res1 = page.evaluate("""() => {
            const el = document.getElementById('jb-basic');
            const s = getComputedStyle(el);
            const r = el.getBoundingClientRect();
            return {
                bg: s.backgroundColor,
                radius: s.borderRadius,
                margin_bottom: s.marginBottom,
                padding: s.paddingTop + ' ' + s.paddingRight,
                width_at_992: Math.round(r.width),
            };
        }""")
        expected_1 = {
            "bg": "rgb(233, 236, 239)",
            "radius": "4.8px",
            "margin_bottom": "32px",
            "padding": "64px 32px",
            "width_at_992": 930,
        }
        cases.append(dict(name="basic_jumbotron_styles", passed=(res1 == expected_1), expected=expected_1, actual=res1))

        # Case 2: jumbotron_responsive_padding
        page.set_viewport_size({"width": 375, "height": 900})
        p375 = page.evaluate("() => { const s = getComputedStyle(document.getElementById('jb-basic')); return s.paddingTop + ' ' + s.paddingRight; }")
        page.set_viewport_size({"width": 768, "height": 900})
        p768 = page.evaluate("() => { const s = getComputedStyle(document.getElementById('jb-basic')); return s.paddingTop + ' ' + s.paddingRight; }")
        actual_2 = {"padding_375": p375, "padding_768": p768}
        expected_2 = {"padding_375": "32px 16px", "padding_768": "64px 32px"}
        cases.append(dict(name="jumbotron_responsive_padding", passed=(actual_2 == expected_2), expected=expected_2, actual=actual_2))

        # Case 3: jumbotron_fluid_full_bleed
        page.set_viewport_size({"width": 1200, "height": 900})
        res3 = page.evaluate("""() => {
            const fl = document.getElementById('jb-fluid');
            const inner = document.getElementById('jb-fluid-inner');
            const s = getComputedStyle(fl);
            const r = fl.getBoundingClientRect();
            const ir = inner.getBoundingClientRect();
            return {
                fluid_width: Math.round(r.width),
                fluid_radius: s.borderRadius,
                fluid_padding_x: [s.paddingLeft, s.paddingRight],
                inner_container_width: Math.round(ir.width),
                inner_left_offset: Math.round(ir.left),
            };
        }""")
        expected_3 = {
            "fluid_width": 1200,
            "fluid_radius": "0px",
            "fluid_padding_x": ["0px", "0px"],
            "inner_container_width": 1140,
            "inner_left_offset": 30,
        }
        cases.append(dict(name="jumbotron_fluid_full_bleed", passed=(res3 == expected_3), expected=expected_3, actual=res3))

        # Case 4: jumbotron_nested_elements
        res4 = page.evaluate("""() => {
            const tops = ['jb-h1', 'jb-p', 'jb-hr', 'jb-btn'].map(id => Math.round(document.getElementById(id).getBoundingClientRect().top));
            const btnBg = getComputedStyle(document.getElementById('jb-btn')).backgroundColor;
            return {
                strictly_stacked: tops[0] < tops[1] && tops[1] < tops[2] && tops[2] < tops[3],
                nested_btn_bg: btnBg,
            };
        }""")
        expected_4 = {"strictly_stacked": True, "nested_btn_bg": "rgb(0, 123, 255)"}
        cases.append(dict(name="jumbotron_nested_elements", passed=(res4 == expected_4), expected=expected_4, actual=res4))

        browser.close()

    print(json.dumps({"cases": cases}, ensure_ascii=False, indent=2))


if __name__ == "__main__":
    main()
核对 .jumbotron 背景色/圆角/响应式内边距与嵌套组件排布,以及 .jumbotron-fluid 在 1200px 视口下 1200px 通宽、0px 圆角与内层 1140px 居中容器。

查看独立实例 ↗ 在独立页面查看源码、运行环境与工程包

本地运行

解压后按 README 说明在对应本地环境运行脚本。

下载本章实验包 (mbskycom202612a682.zip) ↓ 查看运行说明 ↗

本章附件与实例包

下载课程关联工程包:Bootstrap4

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a682.zip

下载课程关联工程包:Bootstrap4

课程关联工程包(登记关联35章) · 含当前章节或实例 · mbskycom202612a664.zip

常见错误与边界检查

  • 实际端口、文档资源与应用配置都应和测试条件一致。

技术规范与官方资料

练习与参考解答

查看参考解答

.jumbotron-fluid 移除了圆角与左右内边距并让灰底横跨整个视口宽度;内嵌 .container 可在保留全屏背景的同时,让内部标题与正文与页面其余主内容保持统一的固定最大宽度和左右对齐边距。

SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习