"""Offline Playwright Chromium lab for Bootstrap 5 Chapter 04 (grid-basic)."""
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 {
--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 { 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; } }
@media (min-width: 1400px) { .container { max-width: 1320px; } }
.row {
--bs-gutter-x: 1.5rem;
display: flex;
flex-wrap: wrap;
margin-right: calc(-.5 * var(--bs-gutter-x));
margin-left: calc(-.5 * var(--bs-gutter-x));
}
.row > * {
flex-shrink: 0;
width: 100%;
max-width: 100%;
padding-right: calc(var(--bs-gutter-x) * .5);
padding-left: calc(var(--bs-gutter-x) * .5);
}
.col { flex: 1 0 0%; }
.col-4 { flex: 0 0 auto; width: 33.33333333%; }
.col-6 { flex: 0 0 auto; width: 50%; }
.col-8 { flex: 0 0 auto; width: 66.66666667%; }
@media (min-width: 576px) {
.col-sm-3 { flex: 0 0 auto; width: 25%; }
.col-sm-4 { flex: 0 0 auto; width: 33.33333333%; }
.col-sm-8 { flex: 0 0 auto; width: 66.66666667%; }
.col-sm-9 { flex: 0 0 auto; width: 75%; }
}
@media (min-width: 768px) {
.col-md-3 { flex: 0 0 auto; width: 25%; }
.col-md-4 { flex: 0 0 auto; width: 33.33333333%; }
.col-md-6 { flex: 0 0 auto; width: 50%; }
.offset-md-3 { margin-left: 25%; }
.offset-md-4 { margin-left: 33.33333333%; }
}
@media (min-width: 992px) {
.col-lg-4 { flex: 0 0 auto; width: 33.33333333%; }
.col-lg-8 { flex: 0 0 auto; width: 66.66666667%; }
}
@media (min-width: 1200px) {
.col-xl-2 { flex: 0 0 auto; width: 16.66666667%; }
.col-xl-10 { flex: 0 0 auto; width: 83.33333333%; }
}
@media (min-width: 1400px) {
.col-xxl-3 { flex: 0 0 auto; width: 25%; }
.col-xxl-9 { flex: 0 0 auto; width: 75%; }
}
.p-3 { padding: 1rem !important; }
.bg-primary { background-color: #0d6efd !important; }
.bg-dark { background-color: #212529 !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="fixed-c" class="container">
<div id="row-offset-1" class="row">
<div id="off-a1" class="col-md-4">A1</div>
<div id="off-a2" class="col-md-4 offset-md-4">A2</div>
</div>
<div id="row-offset-2" class="row">
<div id="off-b1" class="col-md-3 offset-md-3">B1</div>
<div id="off-b2" class="col-md-3 offset-md-3">B2</div>
</div>
<div id="row-offset-3" class="row">
<div id="off-c1" class="col-md-6 offset-md-3">C1</div>
</div>
</div>
<div class="container-fluid">
<div id="row-auto3" class="row">
<div class="col">短内容</div>
<div class="col" style="height: 80px;">较高内容</div>
<div class="col">短内容</div>
</div>
<div id="row-one-fixed" class="row">
<div class="col">left</div>
<div class="col-6">mid</div>
<div class="col">right</div>
</div>
<div id="row-sm4" class="row">
<div class="col-sm-3">1</div>
<div class="col-sm-3">2</div>
<div class="col-sm-3">3</div>
<div class="col-sm-3">4</div>
</div>
<div id="row-sm-unequal" class="row">
<div class="col-sm-4">4</div>
<div class="col-sm-8">8</div>
</div>
<div id="row-multi" class="row">
<div class="col-sm-3 col-md-6 col-lg-4 col-xl-2 col-xxl-3 p-3 bg-primary text-white">L</div>
<div class="col-sm-9 col-md-6 col-lg-8 col-xl-10 col-xxl-9 p-3 bg-dark text-white">R</div>
</div>
<div id="row-nested" class="row">
<div id="nest-outer8" class="col-8">
outer8
<div class="row">
<div id="nest-inner1" class="col-6">in1</div>
<div id="nest-inner2" class="col-6">in2</div>
</div>
</div>
<div id="nest-outer4" class="col-4">outer4</div>
</div>
</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": 1200, "height": 900})
page.set_content(HTML_DOC)
# Case 1: Flexbox row, 1.5rem gutter, xxl breakpoint & nesting
page.set_viewport_size({"width": 1400, "height": 900})
xxl_w = page.eval_on_selector("#fixed-c", "el => Math.round(el.getBoundingClientRect().width)")
page.set_viewport_size({"width": 1200, "height": 900})
c1 = page.evaluate("""() => {
const row = document.getElementById('row-nested');
const rcs = getComputedStyle(row);
const o8 = document.getElementById('nest-outer8');
const o4 = document.getElementById('nest-outer4');
const i1 = document.getElementById('nest-inner1');
const i2 = document.getElementById('nest-inner2');
return {
row_display: rcs.display,
row_margin_x: [rcs.marginLeft, rcs.marginRight],
col_padding_x: [getComputedStyle(o8).paddingLeft, getComputedStyle(o8).paddingRight],
nested_widths: [
Math.round(o8.getBoundingClientRect().width),
Math.round(o4.getBoundingClientRect().width),
Math.round(i1.getBoundingClientRect().width),
Math.round(i2.getBoundingClientRect().width)
]
};
}""")
c1["xxl_container_w"] = xxl_w
exp1 = {
"row_display": "flex",
"row_margin_x": ["-12px", "-12px"],
"col_padding_x": ["12px", "12px"],
"nested_widths": [800, 400, 400, 400],
"xxl_container_w": 1320,
}
cases.append({
"name": "grid_breakpoints_gutters_and_nesting",
"passed": c1 == exp1,
"expected": exp1,
"actual": c1,
})
# Case 2: Auto equal-width & equal-height .col + single fixed .col-6
c2 = page.evaluate("""() => {
const a3 = [...document.querySelectorAll('#row-auto3 > div')];
const f1 = [...document.querySelectorAll('#row-one-fixed > div')];
return {
auto3_widths: a3.map(e => Math.round(e.getBoundingClientRect().width)),
auto3_heights: a3.map(e => Math.round(e.getBoundingClientRect().height)),
one_fixed_widths: f1.map(e => Math.round(e.getBoundingClientRect().width))
};
}""")
exp2 = {
"auto3_widths": [400, 400, 400],
"auto3_heights": [80, 80, 80],
"one_fixed_widths": [300, 600, 300],
}
cases.append({
"name": "auto_equal_and_single_fixed_col",
"passed": c2 == exp2,
"expected": exp2,
"actual": c2,
})
# Case 3: Responsive .col-sm-3 and .col-sm-4/.col-sm-8 across 375px vs 768px
page.set_viewport_size({"width": 375, "height": 900})
sm_375 = page.evaluate("""() => ({
eq4: [...document.querySelectorAll('#row-sm4 > div')].map(e => Math.round(e.getBoundingClientRect().width)),
uneq2: [...document.querySelectorAll('#row-sm-unequal > div')].map(e => Math.round(e.getBoundingClientRect().width))
})""")
page.set_viewport_size({"width": 768, "height": 900})
sm_768 = page.evaluate("""() => ({
eq4: [...document.querySelectorAll('#row-sm4 > div')].map(e => Math.round(e.getBoundingClientRect().width)),
uneq2: [...document.querySelectorAll('#row-sm-unequal > div')].map(e => Math.round(e.getBoundingClientRect().width))
})""")
c3 = {"375": sm_375, "768": sm_768}
exp3 = {
"375": {"eq4": [375, 375, 375, 375], "uneq2": [375, 375]},
"768": {"eq4": [192, 192, 192, 192], "uneq2": [256, 512]},
}
cases.append({
"name": "responsive_sm_equal_and_unequal",
"passed": c3 == exp3,
"expected": exp3,
"actual": c3,
})
# Case 4: Multi-breakpoint combinations across 6 viewports
c4 = {}
for vw in (375, 576, 768, 992, 1200, 1400):
page.set_viewport_size({"width": vw, "height": 900})
c4[str(vw)] = page.evaluate("""() =>
[...document.querySelectorAll('#row-multi > div')].map(e => Math.round(e.getBoundingClientRect().width))
""")
exp4 = {
"375": [375, 375],
"576": [144, 432],
"768": [384, 384],
"992": [331, 661],
"1200": [200, 1000],
"1400": [350, 1050],
}
cases.append({
"name": "multi_breakpoint_combinations",
"passed": c4 == exp4,
"expected": exp4,
"actual": c4,
})
# Case 5: Offset columns at 992px (container 960px) vs 375px
page.set_viewport_size({"width": 992, "height": 900})
off_992 = page.evaluate("""() => {
const pick = id => {
const el = document.getElementById(id);
return [Math.round(el.getBoundingClientRect().width), Math.round(parseFloat(getComputedStyle(el).marginLeft))];
};
return { a2: pick('off-a2'), b1: pick('off-b1'), c1: pick('off-c1') };
}""")
page.set_viewport_size({"width": 375, "height": 900})
off_375_ml = page.eval_on_selector("#off-a2", "el => getComputedStyle(el).marginLeft")
c5 = {"at_992": off_992, "at_375_margin_left": off_375_ml}
exp5 = {
"at_992": {"a2": [320, 320], "b1": [240, 240], "c1": [480, 240]},
"at_375_margin_left": "0px",
}
cases.append({
"name": "offset_md_columns",
"passed": c5 == exp5,
"expected": exp5,
"actual": c5,
})
browser.close()
print(json.dumps({"cases": cases}, ensure_ascii=False))
if __name__ == "__main__":
main()
本例需在 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
入口文件:projects/bootstrap5-grid-basic-lab/explore-grid-basic.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-grid-basic.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联38章) · 含当前章节或实例 · mbskycom202612a700.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a718.zip