蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

运行 Bootstrap 5 Flex 容器、主轴/交叉轴对齐与响应式断点实验

PYTHONWindows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。explore-flex.py
explore-flex.py268 行
"""Offline Playwright Chromium verification for Bootstrap 5 Flex utilities."""
import json
from playwright.sync_api import sync_playwright

CSS = """
* { box-sizing: border-box; }
body { margin: 0; font-family: system-ui, sans-serif; font-size: 16px; }
.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-0 { order: 0 !important; }
.order-1 { order: 1 !important; }
.order-2 { order: 2 !important; }
.order-3 { order: 3 !important; }
.ms-auto { margin-left: auto !important; }
.me-auto { margin-right: 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; }
.p-2 { padding: 8px !important; }
.p-3 { padding: 16px !important; }
.box { width: 600px; }
.item-fixed { width: 100px; height: 40px; }
@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; }
  .order-md-2 { order: 2 !important; }
  .align-items-md-center { align-items: center !important; }
  .align-self-md-end { align-self: flex-end !important; }
}
"""


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": 900, "height": 800})

        # Case 1: d-flex, d-inline-flex, flex-row, flex-row-reverse, flex-column, flex-column-reverse
        page.set_content(f"""<!DOCTYPE html><html><head><style>{CSS}</style></head><body>
        <div class="box">
          <div id="dflex" class="d-flex"><div class="item-fixed">1</div><div class="item-fixed">2</div><div class="item-fixed">3</div></div>
          <div id="diflex" class="d-inline-flex"><div class="item-fixed">1</div><div class="item-fixed">2</div><div class="item-fixed">3</div></div>
          <div id="frow" class="d-flex flex-row"><div id="r1" class="item-fixed">1</div><div id="r3" class="item-fixed">3</div></div>
          <div id="frowrev" class="d-flex flex-row-reverse"><div id="rr1" class="item-fixed">1</div><div id="rr3" class="item-fixed">3</div></div>
          <div id="fcol" class="d-flex flex-column"><div id="c1" class="item-fixed">1</div><div id="c3" class="item-fixed">3</div></div>
          <div id="fcolrev" class="d-flex flex-column-reverse"><div id="cr1" class="item-fixed">1</div><div id="cr3" class="item-fixed">3</div></div>
        </div></body></html>""")
        act1 = page.evaluate("""() => {
          const cs = id => getComputedStyle(document.getElementById(id));
          const rc = id => document.getElementById(id).getBoundingClientRect();
          return {
            dflex_display: cs('dflex').display,
            dflex_width: Math.round(rc('dflex').width),
            diflex_display: cs('diflex').display,
            diflex_width: Math.round(rc('diflex').width),
            row_r1_left_of_r3: rc('r1').left < rc('r3').left,
            row_rev_rr1_right_of_rr3: rc('rr1').left > rc('rr3').left,
            col_c1_above_c3: rc('c1').top < rc('c3').top,
            col_rev_cr1_below_cr3: rc('cr1').top > rc('cr3').top,
          };
        }""")
        exp1 = {
            "dflex_display": "flex",
            "dflex_width": 600,
            "diflex_display": "inline-flex",
            "diflex_width": 300,
            "row_r1_left_of_r3": True,
            "row_rev_rr1_right_of_rr3": True,
            "col_c1_above_c3": True,
            "col_rev_cr1_below_cr3": True,
        }
        cases.append({"name": "flex_container_and_direction", "expected": exp1, "actual": act1, "passed": act1 == exp1})

        # Case 2: justify-content, flex-fill, flex-grow-1, flex-shrink-0/1
        page.set_content(f"""<!DOCTYPE html><html><head><style>{CSS}</style></head><body>
        <div id="jc_end" class="box d-flex justify-content-end"><div id="je1" class="item-fixed">1</div></div>
        <div id="jc_center" class="box d-flex justify-content-center"><div id="jct1" class="item-fixed">1</div></div>
        <div id="jc_between" class="box d-flex justify-content-between"><div id="jb1" class="item-fixed">1</div><div id="jb2" class="item-fixed">2</div></div>
        <div id="jc_around" class="box d-flex justify-content-around"><div id="ja1" class="item-fixed">1</div><div id="ja2" class="item-fixed">2</div></div>
        <div id="fill_box" class="box d-flex"><div id="f1" class="flex-fill">X</div><div id="f2" class="flex-fill">X</div><div id="f3" class="flex-fill">X</div></div>
        <div id="grow_box" class="box d-flex"><div id="g1" class="item-fixed">1</div><div id="g2" class="item-fixed">2</div><div id="g3" class="flex-grow-1">3</div></div>
        <div id="shrink_box" style="width:200px" class="d-flex"><div id="s0" style="width:150px" class="flex-shrink-0">NoShrink</div><div id="s1" style="width:150px" class="flex-shrink-1">Shrink</div></div>
        </body></html>""")
        act2 = page.evaluate("""() => {
          const rc = id => document.getElementById(id).getBoundingClientRect();
          return {
            end_x: Math.round(rc('je1').left),
            center_x: Math.round(rc('jct1').left),
            between_gap: Math.round(rc('jb2').left - rc('jb1').right),
            around_left_margin: Math.round(rc('ja1').left),
            fill_widths: [Math.round(rc('f1').width), Math.round(rc('f2').width), Math.round(rc('f3').width)],
            grow_g3_width: Math.round(rc('g3').width),
            shrink_widths: [Math.round(rc('s0').width), Math.round(rc('s1').width)],
          };
        }""")
        exp2 = {
            "end_x": 500,
            "center_x": 250,
            "between_gap": 400,
            "around_left_margin": 100,
            "fill_widths": [200, 200, 200],
            "grow_g3_width": 400,
            "shrink_widths": [150, 50],
        }
        cases.append({"name": "flex_justify_fill_grow_shrink", "expected": exp2, "actual": act2, "passed": act2 == exp2})

        # Case 3: order and ms-auto / me-auto
        page.set_content(f"""<!DOCTYPE html><html><head><style>{CSS}</style></head><body>
        <div id="ord_box" class="box d-flex">
          <div id="o1" class="item-fixed order-3">Item 1</div>
          <div id="o2" class="item-fixed order-2">Item 2</div>
          <div id="o3" class="item-fixed order-1">Item 3</div>
        </div>
        <div id="ms_box" class="box d-flex">
          <div id="ms1" class="item-fixed ms-auto">Item 1</div>
          <div id="ms2" class="item-fixed">Item 2</div>
        </div>
        <div id="me_box" class="box d-flex">
          <div id="me1" class="item-fixed me-auto">Item 1</div>
          <div id="me2" class="item-fixed">Item 2</div>
        </div>
        </body></html>""")
        act3 = page.evaluate("""() => {
          const rc = id => document.getElementById(id).getBoundingClientRect();
          return {
            order_x: [Math.round(rc('o3').left), Math.round(rc('o2').left), Math.round(rc('o1').left)],
            ms_auto_x: [Math.round(rc('ms1').left), Math.round(rc('ms2').left)],
            me_auto_x: [Math.round(rc('me1').left), Math.round(rc('me2').left)],
          };
        }""")
        exp3 = {
            "order_x": [0, 100, 200],
            "ms_auto_x": [400, 500],
            "me_auto_x": [0, 500],
        }
        cases.append({"name": "flex_order_and_auto_margins", "expected": exp3, "actual": act3, "passed": act3 == exp3})

        # Case 4: flex-wrap, align-content, align-items, align-self
        page.set_content(f"""<!DOCTYPE html><html><head><style>{CSS}</style></head><body>
        <div id="wrap_box" style="width:250px;height:160px" class="d-flex flex-wrap align-content-center">
          <div id="w1" class="item-fixed">1</div><div id="w2" class="item-fixed">2</div><div id="w3" class="item-fixed">3</div>
        </div>
        <div id="wrev_box" style="width:250px;height:160px" class="d-flex flex-wrap-reverse">
          <div id="wr1" class="item-fixed">1</div><div id="wr2" class="item-fixed">2</div><div id="wr3" class="item-fixed">3</div>
        </div>
        <div id="ai_box" style="width:400px;height:150px" class="d-flex align-items-center">
          <div id="ai1" class="item-fixed">1</div>
          <div id="ai2" class="item-fixed align-self-start">2</div>
          <div id="ai3" class="item-fixed align-self-end">3</div>
          <div id="ai4" style="width:100px" class="align-self-stretch">4</div>
        </div>
        </body></html>""")
        act4 = page.evaluate("""() => {
          const rc = id => document.getElementById(id).getBoundingClientRect();
          const baseW = rc('wrap_box').top;
          const baseAI = rc('ai_box').top;
          return {
            wrap_second_row: rc('w3').top > rc('w1').top,
            wrap_center_offset_y: Math.round(rc('w1').top - baseW),
            wrap_reverse_w3_above_w1: rc('wr3').top < rc('wr1').top,
            ai_center_offset_y: Math.round(rc('ai1').top - baseAI),
            self_start_offset_y: Math.round(rc('ai2').top - baseAI),
            self_end_offset_y: Math.round(rc('ai3').top - baseAI),
            self_stretch_height: Math.round(rc('ai4').height),
          };
        }""")
        exp4 = {
            "wrap_second_row": True,
            "wrap_center_offset_y": 40,
            "wrap_reverse_w3_above_w1": True,
            "ai_center_offset_y": 55,
            "self_start_offset_y": 0,
            "self_end_offset_y": 110,
            "self_stretch_height": 150,
        }
        cases.append({"name": "flex_wrap_and_align", "expected": exp4, "actual": act4, "passed": act4 == exp4})

        # Case 5: Responsive md breakpoints (500px vs 900px)
        html_resp = f"""<!DOCTYPE html><html><head><style>{CSS}</style></head><body>
        <div id="resp" style="height:120px" class="d-md-flex flex-column flex-md-row justify-content-md-between align-items-md-center">
          <div id="rm1" class="item-fixed order-md-2">First</div>
          <div id="rm2" class="item-fixed order-md-1 align-self-md-end">Second</div>
        </div></body></html>"""
        page.set_viewport_size({"width": 500, "height": 600})
        page.set_content(html_resp)
        narrow = page.evaluate("""() => {
          const cs = getComputedStyle(document.getElementById('resp'));
          return { display: cs.display, direction: cs.flexDirection };
        }""")
        page.set_viewport_size({"width": 900, "height": 600})
        wide = page.evaluate("""() => {
          const resp = document.getElementById('resp');
          const cs = getComputedStyle(resp);
          const base = resp.getBoundingClientRect().top;
          const r1 = document.getElementById('rm1').getBoundingClientRect();
          const r2 = document.getElementById('rm2').getBoundingClientRect();
          return {
            display: cs.display,
            direction: cs.flexDirection,
            justify: cs.justifyContent,
            alignItems: cs.alignItems,
            rm1_offset_y: Math.round(r1.top - base),
            rm2_offset_y: Math.round(r2.top - base),
            rm2_left_of_rm1: r2.left < r1.left,
          };
        }""")
        act5 = {"narrow": narrow, "wide": wide}
        exp5 = {
            "narrow": {"display": "block", "direction": "column"},
            "wide": {
                "display": "flex",
                "direction": "row",
                "justify": "space-between",
                "alignItems": "center",
                "rm1_offset_y": 40,
                "rm2_offset_y": 80,
                "rm2_left_of_rm1": True,
            },
        }
        cases.append({"name": "flex_responsive_breakpoints", "expected": exp5, "actual": act5, "passed": act5 == exp5})

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


if __name__ == "__main__":
    main()
本例需在 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。

入口文件:projects/bootstrap5-flex-lab/explore-flex.py

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 explore-flex.py,或下载右侧课程工程包。
  2. 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:Bootstrap5

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

下载课程关联工程包:Bootstrap5

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

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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