Bootstrap5 容器
核对 Bootstrap 5 六档断点(含新增 xxl ≥1400px)下固定容器、全宽容器、间距/边框/颜色修饰类及五类响应式容器的完整尺寸矩阵。
学完这一章,你能够
- 核对 Bootstrap 5 六档断点(含新增 xxl ≥1400px)下固定容器、全宽容器、间距/边框/颜色修饰类及五类响应式容器的完整尺寸矩阵。
容器类型与六档断点固定宽度 .container
Bootstrap 5 使用容器包裹网格与内容区域,核心提供 `.container`(默认响应式固定宽度容器,随视口断点阶梯式缩放)和 `.container-fluid`(始终占据 `100%` 视口宽度的全屏流式容器)两类。
`.container` 的 `max-width` 随屏幕断点分级递增;需要特别注意 Bootstrap 5 新增了 `≥1400px` 的超级大屏幕(`xxl`)档位(最大宽 `1320px`),而 Bootstrap 4 最高只到 `≥1200px`(`1140px`)。本站通过离线 Chromium 在 6 档标准视口实测 `.container` 渲染宽度:`375px → 375px`、`576px → 540px`、`768px → 720px`、`992px → 960px`、`1200px → 1140px`、`1400px → 1320px`,与官方断点表完全一致。
100% 宽度容器、内边距及边框颜色修饰
`.container-fluid` 在任意屏幕尺寸下宽度均保持 `100%`。本站跨 `375px` 至 `1400px` 六档视口实测,其渲染宽度恒等于视口宽度(`375 / 576 / 768 / 992 / 1200 / 1400px`),且默认水平左右内边距各为 `12px`(`0.75rem`,即 `--bs-gutter-x` 的一半)、上下内边距为 `0px`。
容器默认不含上下内边距,可按需叠加间距、边框与主题色工具类(详见后续工具类章节)。本站实测:`.container.pt-5` 仅将顶部内边距增至 `48px`(底部仍 `0px`、左侧仍 `12px`);`.container.p-5.my-5.border` 四周内边距 `48px`、上下外边距各 `48px`、边框 `1px`;`.bg-dark.text-white` 背景 `rgb(33, 37, 41)` 配白字,`.bg-primary.text-white` 背景 `rgb(13, 110, 253)` 配白字。
响应式容器 .container-{breakpoint}
Bootstrap 5 提供 `.container-sm`、`.container-md`、`.container-lg`、`.container-xl` 与 `.container-xxl` 五类响应式容器:在低于对应断点时均保持 `100%` 全宽,一旦视口达到或超过该断点即切换为对应档位的固定 `max-width`。本站跨 6 档视口一次性实测五个容器的完整像素矩阵,全部精准命中下表数值。
Bootstrap 5 .container 六档断点 max-width 对照表
| 属性 | 超小 (<576px) | 小屏 (≥576px) | 中屏 (≥768px) | 大屏 (≥992px) | 特大 (≥1200px) | 超大 (≥1400px) |
|---|---|---|---|---|---|---|
| max-width | 100% | 540px | 720px | 960px | 1140px | 1320px |
Bootstrap 5 响应式容器类名宽度矩阵
| 容器类 | 超小 (<576px) | 小屏 (≥576px) | 中屏 (≥768px) | 大屏 (≥992px) | 特大 (≥1200px) | 超大 (≥1400px) |
|---|---|---|---|---|---|---|
| .container-sm | 100% | 540px | 720px | 960px | 1140px | 1320px |
| .container-md | 100% | 100% | 720px | 960px | 1140px | 1320px |
| .container-lg | 100% | 100% | 100% | 960px | 1140px | 1320px |
| .container-xl | 100% | 100% | 100% | 100% | 1140px | 1320px |
| .container-xxl | 100% | 100% | 100% | 100% | 100% | 1320px |
离线 Chromium 跨 6 档视口实测 .container、.container-fluid、间距颜色及 5 类响应式容器
"""Offline Playwright Chromium lab for Bootstrap 5 Chapter 03 (container)."""
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; }
.container, .container-fluid, .container-sm, .container-md, .container-lg, .container-xl, .container-xxl {
--bs-gutter-x: 1.5rem;
width: 100%;
padding-right: calc(var(--bs-gutter-x) * .5);
padding-left: calc(var(--bs-gutter-x) * .5);
margin-right: auto;
margin-left: auto;
}
@media (min-width: 576px) { .container, .container-sm { max-width: 540px; } }
@media (min-width: 768px) { .container, .container-sm, .container-md { max-width: 720px; } }
@media (min-width: 992px) { .container, .container-sm, .container-md, .container-lg { max-width: 960px; } }
@media (min-width: 1200px) { .container, .container-sm, .container-md, .container-lg, .container-xl { max-width: 1140px; } }
@media (min-width: 1400px) { .container, .container-sm, .container-md, .container-lg, .container-xl, .container-xxl { max-width: 1320px; } }
.pt-5 { padding-top: 3rem !important; }
.p-5 { padding: 3rem !important; }
.my-5 { margin-top: 3rem !important; margin-bottom: 3rem !important; }
.border { border: 1px solid #dee2e6 !important; }
.bg-dark { background-color: #212529 !important; }
.bg-primary { background-color: #0d6efd !important; }
.text-white { color: #fff !important; }
"""
HTML_DOC = f"""<!DOCTYPE html>
<html lang="zh-CN">
<head><meta charset="utf-8"><style>{BS5_CSS}</style></head>
<body>
<div id="c-default" class="container">default</div>
<div id="c-fluid" class="container-fluid">fluid</div>
<div id="c-pt" class="container pt-5">pt-5</div>
<div id="c-border" class="container p-5 my-5 border">border</div>
<div id="c-dark" class="container p-5 my-5 bg-dark text-white">dark</div>
<div id="c-primary" class="container p-5 my-5 bg-primary text-white">primary</div>
<div id="c-sm" class="container-sm">sm</div>
<div id="c-md" class="container-md">md</div>
<div id="c-lg" class="container-lg">lg</div>
<div id="c-xl" class="container-xl">xl</div>
<div id="c-xxl" class="container-xxl">xxl</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 = []
viewports = [375, 576, 768, 992, 1200, 1400]
with sync_playwright() as p:
browser = launch_browser(p)
page = browser.new_page(viewport={"width": 1200, "height": 900})
page.set_content(HTML_DOC)
# Collect widths across all 6 Bootstrap 5 viewports in one pass
widths_by_vw = {}
for vw in viewports:
page.set_viewport_size({"width": vw, "height": 900})
widths_by_vw[vw] = page.evaluate("""() => {
const ids = ['c-default', 'c-fluid', 'c-sm', 'c-md', 'c-lg', 'c-xl', 'c-xxl'];
const out = {};
for (const id of ids) {
out[id] = Math.round(document.getElementById(id).getBoundingClientRect().width);
}
return out;
}""")
# Case 1: .container across 6 breakpoints (including new xxl >= 1400px)
actual1 = {str(vw): widths_by_vw[vw]["c-default"] for vw in viewports}
exp1 = {"375": 375, "576": 540, "768": 720, "992": 960, "1200": 1140, "1400": 1320}
cases.append({
"name": "fixed_container_six_breakpoints",
"passed": actual1 == exp1,
"expected": exp1,
"actual": actual1,
})
# Case 2: .container-fluid 100% width & default horizontal padding
fluid_pad = page.evaluate("""() => {
const cs = getComputedStyle(document.getElementById('c-fluid'));
return { pt: cs.paddingTop, pb: cs.paddingBottom, pl: cs.paddingLeft, pr: cs.paddingRight };
}""")
actual2 = {
"widths": {str(vw): widths_by_vw[vw]["c-fluid"] for vw in viewports},
"default_padding": fluid_pad,
}
exp2 = {
"widths": {"375": 375, "576": 576, "768": 768, "992": 992, "1200": 1200, "1400": 1400},
"default_padding": {"pt": "0px", "pb": "0px", "pl": "12px", "pr": "12px"},
}
cases.append({
"name": "fluid_container_full_width",
"passed": actual2 == exp2,
"expected": exp2,
"actual": actual2,
})
# Case 3: Container padding, margin, border, and color utilities
actual3 = page.evaluate("""() => {
const pt = getComputedStyle(document.getElementById('c-pt'));
const bd = getComputedStyle(document.getElementById('c-border'));
const dk = getComputedStyle(document.getElementById('c-dark'));
const pr = getComputedStyle(document.getElementById('c-primary'));
return {
pt5: { pt: pt.paddingTop, pb: pt.paddingBottom, pl: pt.paddingLeft },
border_box: { p: bd.paddingTop, my: [bd.marginTop, bd.marginBottom], border: bd.borderTopWidth },
dark_box: { bg: dk.backgroundColor, color: dk.color },
primary_box: { bg: pr.backgroundColor, color: pr.color }
};
}""")
exp3 = {
"pt5": {"pt": "48px", "pb": "0px", "pl": "12px"},
"border_box": {"p": "48px", "my": ["48px", "48px"], "border": "1px"},
"dark_box": {"bg": "rgb(33, 37, 41)", "color": "rgb(255, 255, 255)"},
"primary_box": {"bg": "rgb(13, 110, 253)", "color": "rgb(255, 255, 255)"},
}
cases.append({
"name": "container_padding_border_colors",
"passed": actual3 == exp3,
"expected": exp3,
"actual": actual3,
})
# Case 4: Responsive containers .container-sm..xxl matrix
actual4 = {
cls: [widths_by_vw[vw][cid] for vw in viewports]
for cls, cid in [
(".container-sm", "c-sm"),
(".container-md", "c-md"),
(".container-lg", "c-lg"),
(".container-xl", "c-xl"),
(".container-xxl", "c-xxl"),
]
}
exp4 = {
".container-sm": [375, 540, 720, 960, 1140, 1320],
".container-md": [375, 576, 720, 960, 1140, 1320],
".container-lg": [375, 576, 768, 960, 1140, 1320],
".container-xl": [375, 576, 768, 992, 1140, 1320],
".container-xxl": [375, 576, 768, 992, 1200, 1320],
}
cases.append({
"name": "responsive_container_matrix",
"passed": actual4 == exp4,
"expected": exp4,
"actual": actual4,
})
browser.close()
print(json.dumps({"cases": cases}, ensure_ascii=False))
if __name__ == "__main__":
main()
- 核对 .container 六档宽度 [375,540,720,960,1140,1320]、.container-fluid 全宽与 12px 左右内边距、pt-5/p-5/my-5/border/bg-* 样式及 5×6 响应式容器矩阵。
所需环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
当前源码 SHA256:57c51982c074184830ef8772ddc633132f55885cffd90e2b8947f4e2de52407d
本地运行
解压后按 README 说明在对应本地环境运行脚本。
所需环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
ZIP SHA256:8dcd1629c7478d6c28f08f309c37a8cdcfadb09b301587e95350c0da9dcd9f2e
常见错误与边界
- 实际端口、文档资源与应用配置都应和测试条件一致。
练习与解答
在 1000px 视口宽度下,.container、.container-lg 与 .container-xl 的实际渲染宽度分别是多少?
展开参考解答
1000px 处于 lg(≥992px 且 <1200px)区间:.container 与 .container-lg 均为 960px,而 .container-xl 尚未达到 1200px 断点,因此仍保持 100% 宽度(1000px)。