"""Offline Playwright Chromium verification for Bootstrap 4 Chapter 33: Flex."""
import json
import platform
from playwright.sync_api import sync_playwright
BS4_FLEX_CSS = """
* { box-sizing: border-box; }
body { margin: 0; padding: 0; font-family: Arial, sans-serif; font-size: 16px; }
.d-block { display: block !important; }
.d-flex { display: flex !important; }
.d-inline-flex { display: inline-flex !important; }
.flex-row { flex-direction: row !important; }
.flex-row-reverse { flex-direction: row-reverse !important; }
.flex-column { flex-direction: column !important; }
.flex-column-reverse { flex-direction: column-reverse !important; }
.justify-content-start { justify-content: flex-start !important; }
.justify-content-end { justify-content: flex-end !important; }
.justify-content-center { justify-content: center !important; }
.justify-content-between { justify-content: space-between !important; }
.justify-content-around { justify-content: space-around !important; }
.flex-fill { flex: 1 1 auto !important; }
.flex-grow-0 { flex-grow: 0 !important; }
.flex-grow-1 { flex-grow: 1 !important; }
.flex-shrink-0 { flex-shrink: 0 !important; }
.flex-shrink-1 { flex-shrink: 1 !important; }
.order-1 { order: 1 !important; }
.order-2 { order: 2 !important; }
.order-3 { order: 3 !important; }
.mr-auto { margin-right: auto !important; }
.ml-auto { margin-left: auto !important; }
.flex-nowrap { flex-wrap: nowrap !important; }
.flex-wrap { flex-wrap: wrap !important; }
.flex-wrap-reverse { flex-wrap: wrap-reverse !important; }
.align-content-start { align-content: flex-start !important; }
.align-content-end { align-content: flex-end !important; }
.align-content-center { align-content: center !important; }
.align-content-between { align-content: space-between !important; }
.align-content-around { align-content: space-around !important; }
.align-content-stretch { align-content: stretch !important; }
.align-items-start { align-items: flex-start !important; }
.align-items-end { align-items: flex-end !important; }
.align-items-center { align-items: center !important; }
.align-items-baseline { align-items: baseline !important; }
.align-items-stretch { align-items: stretch !important; }
.align-self-start { align-self: flex-start !important; }
.align-self-end { align-self: flex-end !important; }
.align-self-center { align-self: center !important; }
.align-self-baseline { align-self: baseline !important; }
.align-self-stretch { align-self: stretch !important; }
@media (min-width: 768px) {
.d-md-flex { display: flex !important; }
.flex-md-row { flex-direction: row !important; }
.justify-content-md-between { justify-content: space-between !important; }
.order-md-1 { order: 1 !important; }
.flex-md-fill { flex: 1 1 auto !important; }
.align-self-md-end { align-self: flex-end !important; }
}
@media (min-width: 992px) {
.align-items-lg-center { align-items: center !important; }
}
.box100 { width: 100px; height: 40px; }
"""
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)
try:
page = browser.new_page(viewport={"width": 800, "height": 700})
# Case 1: d-flex vs d-inline-flex and 4 flex-direction variants
html1 = f"""<!doctype html><html><head><style>{BS4_FLEX_CSS}</style></head><body>
<div style="width:600px">
<div id="df" class="d-flex"><div class="box100">1</div><div class="box100">2</div><div class="box100">3</div></div>
<div id="dif" class="d-inline-flex"><div class="box100">1</div><div class="box100">2</div><div class="box100">3</div></div>
<div id="row" class="d-flex flex-row"><div id="r1" class="box100">1</div><div id="r2" class="box100">2</div><div id="r3" class="box100">3</div></div>
<div id="rowr" class="d-flex flex-row-reverse"><div id="rr1" class="box100">1</div><div id="rr2" class="box100">2</div><div id="rr3" class="box100">3</div></div>
<div id="col" class="d-flex flex-column"><div id="c1" class="box100">1</div><div id="c2" class="box100">2</div><div id="c3" class="box100">3</div></div>
<div id="colr" class="d-flex flex-column-reverse"><div id="cr1" class="box100">1</div><div id="cr2" class="box100">2</div><div id="cr3" class="box100">3</div></div>
</div>
</body></html>"""
page.set_content(html1)
act1 = page.evaluate("""() => {
const r = id => document.getElementById(id).getBoundingClientRect();
const cs = id => getComputedStyle(document.getElementById(id));
return {
d_flex: [cs('df').display, Math.round(r('df').width)],
d_inline_flex: [cs('dif').display, Math.round(r('dif').width)],
row_x: [Math.round(r('r1').left), Math.round(r('r2').left), Math.round(r('r3').left)],
row_rev_x: [Math.round(r('rr1').left), Math.round(r('rr2').left), Math.round(r('rr3').left)],
col_order_top_to_bottom: r('c1').top < r('c2').top && r('c2').top < r('c3').top,
col_rev_order_bottom_to_top: r('cr3').top < r('cr2').top && r('cr2').top < r('cr1').top
};
}""")
exp1 = {
"d_flex": ["flex", 600],
"d_inline_flex": ["inline-flex", 300],
"row_x": [0, 100, 200],
"row_rev_x": [500, 400, 300],
"col_order_top_to_bottom": True,
"col_rev_order_bottom_to_top": True,
}
cases.append(dict(
name="flex_container_and_direction",
inputs=dict(parent_width=600, item_width=100, item_count=3),
prerequisites="父容器宽 600px,三个子项各宽 100px",
action="比较 .d-flex 与 .d-inline-flex 的容器宽度,并实测四个方向类下子项的水平/垂直坐标顺序。",
expected=exp1,
actual=act1,
measurements=dict(directions=["row", "row-reverse", "column", "column-reverse"]),
passed=(act1 == exp1),
))
# Case 2: justify-content-*, flex-fill, flex-grow-1, flex-shrink-0/1
html2 = f"""<!doctype html><html><head><style>{BS4_FLEX_CSS}</style></head><body>
<div id="js_start" class="d-flex justify-content-start" style="width:600px"><div class="box100">1</div><div class="box100">2</div><div class="box100">3</div></div>
<div id="js_end" class="d-flex justify-content-end" style="width:600px"><div class="box100">1</div><div class="box100">2</div><div class="box100">3</div></div>
<div id="js_center" class="d-flex justify-content-center" style="width:600px"><div class="box100">1</div><div class="box100">2</div><div class="box100">3</div></div>
<div id="js_between" class="d-flex justify-content-between" style="width:600px"><div class="box100">1</div><div class="box100">2</div><div class="box100">3</div></div>
<div id="js_around" class="d-flex justify-content-around" style="width:600px"><div class="box100">1</div><div class="box100">2</div><div class="box100">3</div></div>
<div id="fill_row" class="d-flex" style="width:600px"><div class="flex-fill">Item</div><div class="flex-fill">Item</div><div class="flex-fill">Item</div></div>
<div id="grow_row" class="d-flex" style="width:600px"><div class="box100">1</div><div class="box100">2</div><div class="flex-grow-1" style="height:40px">3</div></div>
<div id="shrink_row" class="d-flex" style="width:300px"><div class="flex-shrink-0" style="width:200px;height:40px">S0</div><div class="flex-shrink-1" style="width:200px;height:40px">S1</div></div>
</body></html>"""
page.set_content(html2)
act2 = page.evaluate("""() => {
const xs = id => Array.from(document.getElementById(id).children).map(el => Math.round(el.getBoundingClientRect().left));
const ws = id => Array.from(document.getElementById(id).children).map(el => Math.round(el.getBoundingClientRect().width));
return {
start_x: xs('js_start'),
end_x: xs('js_end'),
center_x: xs('js_center'),
between_x: xs('js_between'),
around_x: xs('js_around'),
fill_w: ws('fill_row'),
grow_w: ws('grow_row'),
shrink_w: ws('shrink_row')
};
}""")
exp2 = {
"start_x": [0, 100, 200],
"end_x": [300, 400, 500],
"center_x": [150, 250, 350],
"between_x": [0, 250, 500],
"around_x": [50, 250, 450],
"fill_w": [200, 200, 200],
"grow_w": [100, 100, 400],
"shrink_w": [200, 100],
}
cases.append(dict(
name="justify_content_fill_and_grow",
inputs=dict(main_width=600, shrink_container_width=300),
prerequisites="主轴容器宽 600px,收缩测试容器宽 300px(两个 200px 子项)",
action="测量五种 justify-content-* 水平定位、.flex-fill 等宽分配、.flex-grow-1 剩余空间填充与 .flex-shrink-0/1 收缩宽度。",
expected=exp2,
actual=act2,
measurements=dict(justify_modes=5),
passed=(act2 == exp2),
))
# Case 3: order-* and mr-auto / ml-auto
html3 = f"""<!doctype html><html><head><style>{BS4_FLEX_CSS}</style></head><body>
<div id="ord" class="d-flex" style="width:600px">
<div id="o1" class="box100 order-3">Item 1</div>
<div id="o2" class="box100 order-2">Item 2</div>
<div id="o3" class="box100 order-1">Item 3</div>
</div>
<div id="mra" class="d-flex" style="width:600px">
<div class="box100 mr-auto">1</div><div class="box100">2</div><div class="box100">3</div>
</div>
<div id="mla" class="d-flex" style="width:600px">
<div class="box100">1</div><div class="box100">2</div><div class="box100 ml-auto">3</div>
</div>
</body></html>"""
page.set_content(html3)
act3 = page.evaluate("""() => {
const xs = id => Array.from(document.getElementById(id).children).map(el => Math.round(el.getBoundingClientRect().left));
return {
order_x: xs('ord'),
mr_auto_x: xs('mra'),
ml_auto_x: xs('mla')
};
}""")
exp3 = {"order_x": [200, 100, 0], "mr_auto_x": [0, 400, 500], "ml_auto_x": [0, 100, 500]}
cases.append(dict(
name="order_and_auto_margins",
inputs=dict(container_width=600, item_width=100),
prerequisites="三子项按 DOM 顺序分别应用 .order-3/.order-2/.order-1;另两组分别施加 .mr-auto 与 .ml-auto",
action="测量重排后子项左坐标及自动外边距吸收剩余空间后的推挤位置。",
expected=exp3,
actual=act3,
measurements=dict(auto_margin_absorbed_px=300),
passed=(act3 == exp3),
))
# Case 4: flex-wrap, align-content, align-items, align-self
html4 = f"""<!doctype html><html><head><style>{BS4_FLEX_CSS}</style></head><body>
<div id="nowrap" class="d-flex flex-nowrap" style="width:300px;height:120px"><div class="box100">1</div><div class="box100">2</div><div class="box100">3</div><div class="box100">4</div></div>
<div id="wrap_end" class="d-flex flex-wrap align-content-end" style="width:300px;height:200px"><div class="box100">1</div><div class="box100">2</div><div class="box100">3</div><div class="box100">4</div></div>
<div id="wrap_rev" class="d-flex flex-wrap-reverse align-content-start" style="width:300px;height:200px"><div class="box100">1</div><div class="box100">2</div><div class="box100">3</div><div class="box100">4</div></div>
<div id="ai_center" class="d-flex align-items-center" style="width:300px;height:120px"><div class="box100">1</div></div>
<div id="ai_base" class="d-flex align-items-baseline" style="width:300px;height:120px"><div class="box100">1</div></div>
<div id="as_box" class="d-flex" style="width:300px;height:150px">
<div id="as1" style="width:80px">1</div>
<div id="as2" class="align-self-start" style="width:80px;height:40px">2</div>
<div id="as3" style="width:80px">3</div>
</div>
</body></html>"""
page.set_content(html4)
act4 = page.evaluate("""() => {
const relTops = id => {
const p = document.getElementById(id);
const pt = p.getBoundingClientRect().top;
return Array.from(p.children).map(el => Math.round(el.getBoundingClientRect().top - pt));
};
const cs = id => getComputedStyle(document.getElementById(id));
return {
nowrap_rows: new Set(relTops('nowrap')).size,
wrap_end_rel_tops: relTops('wrap_end'),
wrap_rev_row1_below_row2: relTops('wrap_rev')[0] > relTops('wrap_rev')[3],
align_items_center_rel_top: relTops('ai_center')[0],
align_items_baseline: cs('ai_base').alignItems,
align_self_heights: [
Math.round(document.getElementById('as1').getBoundingClientRect().height),
Math.round(document.getElementById('as2').getBoundingClientRect().height),
Math.round(document.getElementById('as3').getBoundingClientRect().height)
]
};
}""")
exp4 = {
"nowrap_rows": 1,
"wrap_end_rel_tops": [120, 120, 120, 160],
"wrap_rev_row1_below_row2": True,
"align_items_center_rel_top": 40,
"align_items_baseline": "baseline",
"align_self_heights": [150, 40, 150],
}
cases.append(dict(
name="wrap_align_content_items_and_self",
inputs=dict(wrap_width=300, wrap_height=200, align_self_container_height=150),
prerequisites="300px 容器放四个 100x40px 子项测试换行与多行堆叠;150px 容器测试单项 align-self-start 覆盖默认 stretch",
action="核对 nowrap/wrap/wrap-reverse 行数与行序、align-content-end 多行贴底、align-items-center/baseline 与 align-self-start 高度差异。",
expected=exp4,
actual=act4,
measurements=dict(note="CSS align-items supports baseline rather than around"),
passed=(act4 == exp4),
))
# Case 5: Responsive flex utility classes across 500px / 800px / 1024px
html5 = f"""<!doctype html><html><head><style>{BS4_FLEX_CSS}</style></head><body>
<div id="rbox" class="d-block d-md-flex flex-column flex-md-row justify-content-start justify-content-md-between align-items-start align-items-lg-center" style="height:120px">
<div id="ri1" class="order-2 order-md-1 flex-md-fill">1</div>
<div id="ri2" class="align-self-start align-self-md-end">2</div>
</div>
</body></html>"""
page.set_content(html5)
snap = {}
for vw in (500, 800, 1024):
page.set_viewport_size({"width": vw, "height": 500})
snap[str(vw)] = page.evaluate("""() => {
const norm = v => v.replace(/^flow-/, 'flex-');
const rb = getComputedStyle(document.getElementById('rbox'));
const i1 = getComputedStyle(document.getElementById('ri1'));
const i2 = getComputedStyle(document.getElementById('ri2'));
return {
display: rb.display,
direction: rb.flexDirection,
justify: norm(rb.justifyContent),
alignItems: norm(rb.alignItems),
i1_order: i1.order,
i2_self: norm(i2.alignSelf)
};
}""")
exp5 = {
"500": {"display": "block", "direction": "column", "justify": "flex-start", "alignItems": "flex-start", "i1_order": "2", "i2_self": "flex-start"},
"800": {"display": "flex", "direction": "row", "justify": "space-between", "alignItems": "flex-start", "i1_order": "1", "i2_self": "flex-end"},
"1024": {"display": "flex", "direction": "row", "justify": "space-between", "alignItems": "center", "i1_order": "1", "i2_self": "flex-end"},
}
cases.append(dict(
name="responsive_flex_breakpoints",
inputs=dict(viewports=[500, 800, 1024]),
prerequisites="容器组合 .d-block.d-md-flex、.flex-column.flex-md-row、.justify-content-md-between、.align-items-lg-center 及子项响应类",
action="在 500px、800px、1024px 三个视口宽度下采样计算样式,验证响应式 flex 断点接管。",
expected=exp5,
actual=snap,
measurements=dict(breakpoints_checked=["xs<576", "md>=768", "lg>=992"]),
passed=(snap == exp5),
))
finally:
browser.close()
print(json.dumps(dict(version=platform.python_version(), system=platform.system(), cases=cases), ensure_ascii=False))
if __name__ == "__main__":
main()
本例需在 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
入口文件:projects/bootstrap4-flex-lab/explore-flex.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-flex.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a674.zip
课程关联工程包(登记关联35章) · 含当前章节或实例 · mbskycom202612a664.zip