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

运行边框圆角、响应式浮动、居中宽高、正负间距与显隐阴影工具类实测

PYTHONWindows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。explore-utilities.py
explore-utilities.py384 行
"""Offline Playwright Chromium verification for Bootstrap 4 Chapter 32: Utilities."""
import json
import platform
from playwright.sync_api import sync_playwright

BS4_UTILITIES_CSS = """
* { box-sizing: border-box; }
html { font-size: 16px; }
body { margin: 0; padding: 0; font-family: Arial, sans-serif; }
.swatch { display: inline-block; width: 60px; height: 60px; background: #f5f5f5; }
.border { border: 1px solid #dee2e6 !important; }
.border-0 { border: 0 !important; }
.border-top-0 { border-top: 0 !important; }
.border-right-0 { border-right: 0 !important; }
.border-bottom-0 { border-bottom: 0 !important; }
.border-left-0 { border-left: 0 !important; }
.border-primary { border-color: #007bff !important; }
.border-secondary { border-color: #6c757d !important; }
.border-success { border-color: #28a745 !important; }
.border-danger { border-color: #dc3545 !important; }
.border-warning { border-color: #ffc107 !important; }
.border-info { border-color: #17a2b8 !important; }
.border-light { border-color: #f8f9fa !important; }
.border-dark { border-color: #343a40 !important; }
.border-white { border-color: #ffffff !important; }
.rounded { border-radius: .25rem !important; }
.rounded-top { border-top-left-radius: .25rem !important; border-top-right-radius: .25rem !important; }
.rounded-right { border-top-right-radius: .25rem !important; border-bottom-right-radius: .25rem !important; }
.rounded-bottom { border-bottom-right-radius: .25rem !important; border-bottom-left-radius: .25rem !important; }
.rounded-left { border-top-left-radius: .25rem !important; border-bottom-left-radius: .25rem !important; }
.rounded-circle { border-radius: 50% !important; }
.rounded-0 { border-radius: 0 !important; }
.float-left { float: left !important; }
.float-right { float: right !important; }
.float-none { float: none !important; }
.clearfix::after { display: block; clear: both; content: ""; }
.mx-auto { margin-right: auto !important; margin-left: auto !important; }
.w-25 { width: 25% !important; }
.w-50 { width: 50% !important; }
.w-75 { width: 75% !important; }
.w-100 { width: 100% !important; }
.mw-100 { max-width: 100% !important; }
.h-25 { height: 25% !important; }
.h-50 { height: 50% !important; }
.h-75 { height: 75% !important; }
.h-100 { height: 100% !important; }
.mh-100 { max-height: 100% !important; }
.m-0 { margin: 0 !important; }
.mt-0 { margin-top: 0 !important; }
.ml-1 { margin-left: .25rem !important; }
.mt-2 { margin-top: .5rem !important; }
.mb-3 { margin-bottom: 1rem !important; }
.mr-4 { margin-right: 1.5rem !important; }
.my-5 { margin-top: 3rem !important; margin-bottom: 3rem !important; }
.p-0 { padding: 0 !important; }
.pt-1 { padding-top: .25rem !important; }
.px-2 { padding-left: .5rem !important; padding-right: .5rem !important; }
.p-3 { padding: 1rem !important; }
.pb-4 { padding-bottom: 1.5rem !important; }
.py-5 { padding-top: 3rem !important; padding-bottom: 3rem !important; }
.mt-n1 { margin-top: -.25rem !important; }
.mt-n2 { margin-top: -.5rem !important; }
.mt-n3 { margin-top: -1rem !important; }
.mt-n4 { margin-top: -1.5rem !important; }
.mt-n5 { margin-top: -3rem !important; }
.shadow-sm { box-shadow: 0 .125rem .25rem rgba(0,0,0,.075) !important; }
.shadow { box-shadow: 0 .5rem 1rem rgba(0,0,0,.15) !important; }
.shadow-lg { box-shadow: 0 1rem 3rem rgba(0,0,0,.175) !important; }
.shadow-none { box-shadow: none !important; }
.align-top { vertical-align: top !important; }
.align-middle { vertical-align: middle !important; }
.align-bottom { vertical-align: bottom !important; }
.d-none { display: none !important; }
.d-inline { display: inline !important; }
.d-block { display: block !important; }
@media (min-width: 576px) { .float-sm-right { float: right !important; } }
@media (min-width: 768px) {
  .float-md-right { float: right !important; }
  .mt-md-4 { margin-top: 1.5rem !important; }
  .d-md-block { display: block !important; }
}
@media (min-width: 992px) { .float-lg-right { float: right !important; } }
@media (min-width: 1200px) { .float-xl-right { float: right !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)
        try:
            page = browser.new_page(viewport={"width": 1000, "height": 600})

            # Case 1: Borders, border colors, and rounded utilities
            html1 = f"""<!doctype html><html><head><style>{BS4_UTILITIES_CSS}</style></head><body>
  <span id="b_all" class="swatch border"></span>
  <span id="b_0" class="swatch border border-0"></span>
  <span id="b_top0" class="swatch border border-top-0"></span>
  <span id="b_right0" class="swatch border border-right-0"></span>
  <span id="b_bottom0" class="swatch border border-bottom-0"></span>
  <span id="b_left0" class="swatch border border-left-0"></span>
  <span id="c_primary" class="swatch border border-primary"></span>
  <span id="c_success" class="swatch border border-success"></span>
  <span id="c_danger" class="swatch border border-danger"></span>
  <span id="c_dark" class="swatch border border-dark"></span>
  <span id="r_all" class="swatch rounded"></span>
  <span id="r_top" class="swatch rounded-top"></span>
  <span id="r_circle" class="swatch rounded-circle"></span>
  <span id="r_0" class="swatch rounded rounded-0"></span>
</body></html>"""
            page.set_content(html1)
            act1 = page.evaluate("""() => {
              const cs = id => getComputedStyle(document.getElementById(id));
              return {
                border_all: cs('b_all').borderTopWidth,
                border_0: cs('b_0').borderTopWidth,
                border_top_0: [cs('b_top0').borderTopWidth, cs('b_top0').borderBottomWidth],
                border_left_0: [cs('b_left0').borderLeftWidth, cs('b_left0').borderRightWidth],
                color_primary: cs('c_primary').borderTopColor,
                color_success: cs('c_success').borderTopColor,
                color_danger: cs('c_danger').borderTopColor,
                color_dark: cs('c_dark').borderTopColor,
                rounded: cs('r_all').borderRadius,
                rounded_top: [cs('r_top').borderTopLeftRadius, cs('r_top').borderBottomLeftRadius],
                rounded_circle: cs('r_circle').borderRadius,
                rounded_0: cs('r_0').borderRadius
              };
            }""")
            exp1 = {
                "border_all": "1px",
                "border_0": "0px",
                "border_top_0": ["0px", "1px"],
                "border_left_0": ["0px", "1px"],
                "color_primary": "rgb(0, 123, 255)",
                "color_success": "rgb(40, 167, 69)",
                "color_danger": "rgb(220, 53, 69)",
                "color_dark": "rgb(52, 58, 64)",
                "rounded": "4px",
                "rounded_top": ["4px", "0px"],
                "rounded_circle": "50%",
                "rounded_0": "0px",
            }
            cases.append(dict(
                name="borders_colors_and_rounded",
                inputs=dict(border_classes=["border", "border-0", "border-top-0", "border-left-0"], colors=["primary", "success", "danger", "dark"], rounded=["rounded", "rounded-top", "rounded-circle", "rounded-0"]),
                prerequisites="根字号 16px,按 Bootstrap 4 默认边框宽度 1px 与圆角 0.25rem(4px) 规则渲染",
                action="读取各边框移除类、语义边框色与圆角类的实际计算样式。",
                expected=exp1,
                actual=act1,
                measurements=dict(swatch_px=60),
                passed=(act1 == exp1),
            ))

            # Case 2: Float, clearfix, and responsive floats across 5 breakpoints
            html2 = f"""<!doctype html><html><head><style>{BS4_UTILITIES_CSS}</style></head><body>
  <div id="no_clear"><span class="float-left" style="height:40px">L</span><span class="float-right" style="height:40px">R</span></div>
  <div id="with_clear" class="clearfix"><span id="fl" class="float-left" style="height:40px">L</span><span id="fr" class="float-right" style="height:40px">R</span></div>
  <div id="fsm" class="float-sm-right">sm</div>
  <div id="fmd" class="float-md-right">md</div>
  <div id="flg" class="float-lg-right">lg</div>
  <div id="fxl" class="float-xl-right">xl</div>
  <div id="fnone" class="float-none">none</div>
</body></html>"""
            page.set_content(html2)
            clear_check = page.evaluate("""() => ({
              no_clear_h: Math.round(document.getElementById('no_clear').getBoundingClientRect().height),
              with_clear_h: Math.round(document.getElementById('with_clear').getBoundingClientRect().height),
              fl: getComputedStyle(document.getElementById('fl')).float,
              fr: getComputedStyle(document.getElementById('fr')).float
            })""")
            bp_floats = {}
            for vw in (500, 600, 800, 1024, 1280):
                page.set_viewport_size({"width": vw, "height": 500})
                bp_floats[str(vw)] = page.evaluate("""() => ({
                  sm: getComputedStyle(document.getElementById('fsm')).float,
                  md: getComputedStyle(document.getElementById('fmd')).float,
                  lg: getComputedStyle(document.getElementById('flg')).float,
                  xl: getComputedStyle(document.getElementById('fxl')).float,
                  none: getComputedStyle(document.getElementById('fnone')).float
                })""")
            act2 = {"clearfix": clear_check, "breakpoints": bp_floats}
            exp2 = {
                "clearfix": {"no_clear_h": 0, "with_clear_h": 40, "fl": "left", "fr": "right"},
                "breakpoints": {
                    "500": {"sm": "none", "md": "none", "lg": "none", "xl": "none", "none": "none"},
                    "600": {"sm": "right", "md": "none", "lg": "none", "xl": "none", "none": "none"},
                    "800": {"sm": "right", "md": "right", "lg": "none", "xl": "none", "none": "none"},
                    "1024": {"sm": "right", "md": "right", "lg": "right", "xl": "none", "none": "none"},
                    "1280": {"sm": "right", "md": "right", "lg": "right", "xl": "right", "none": "none"},
                },
            }
            cases.append(dict(
                name="float_clearfix_and_responsive_float",
                inputs=dict(viewports=[500, 600, 800, 1024, 1280]),
                prerequisites="父容器分别省略与添加 .clearfix,响应式浮动按 sm(576)/md(768)/lg(992)/xl(1200) 断点配置",
                action="核对无 clearfix 时的父容器高度塌陷与五个视口宽度下各 .float-*-right 类的计算 float 值。",
                expected=exp2,
                actual=act2,
                measurements=dict(tested_breakpoints=5),
                passed=(act2 == exp2),
            ))

            # Case 3: mx-auto centering, width w-*/mw-100, and height h-*/mh-100
            page.set_viewport_size({"width": 800, "height": 600})
            html3 = f"""<!doctype html><html><head><style>{BS4_UTILITIES_CSS}</style></head><body>
  <div id="box" style="width:400px;position:relative">
    <div id="centered" class="mx-auto" style="width:150px">居中显示</div>
    <div id="w25" class="w-25">25%</div>
    <div id="w50" class="w-50">50%</div>
    <div id="w75" class="w-75">75%</div>
    <div id="w100" class="w-100">100%</div>
    <div id="mw100" class="mw-100" style="width:600px">mw</div>
  </div>
  <div id="hbox" style="width:400px;height:200px">
    <div id="h25" class="h-25">25%</div>
    <div id="h50" class="h-50">50%</div>
    <div id="h75" class="h-75">75%</div>
    <div id="h100" class="h-100">100%</div>
    <div id="mh100" class="mh-100" style="height:500px">mh</div>
  </div>
</body></html>"""
            page.set_content(html3)
            act3 = page.evaluate("""() => {
              const r = id => document.getElementById(id).getBoundingClientRect();
              const cs = id => getComputedStyle(document.getElementById(id));
              return {
                mx_auto_left: Math.round(r('centered').left),
                mx_auto_margins: [cs('centered').marginLeft, cs('centered').marginRight],
                widths: {
                  w25: Math.round(r('w25').width),
                  w50: Math.round(r('w50').width),
                  w75: Math.round(r('w75').width),
                  w100: Math.round(r('w100').width),
                  mw100: Math.round(r('mw100').width)
                },
                heights: {
                  h25: Math.round(r('h25').height),
                  h50: Math.round(r('h50').height),
                  h75: Math.round(r('h75').height),
                  h100: Math.round(r('h100').height),
                  mh100: Math.round(r('mh100').height)
                }
              };
            }""")
            exp3 = {
                "mx_auto_left": 125,
                "mx_auto_margins": ["125px", "125px"],
                "widths": {"w25": 100, "w50": 200, "w75": 300, "w100": 400, "mw100": 400},
                "heights": {"h25": 50, "h50": 100, "h75": 150, "h100": 200, "mh100": 200},
            }
            cases.append(dict(
                name="mx_auto_width_and_height",
                inputs=dict(parent_width=400, parent_height=200, centered_width=150),
                prerequisites="父容器宽 400px、高 200px,测试 .mx-auto、.w-*、.mw-100、.h-* 与 .mh-100",
                action="测量居中块左右外边距、百分比宽高以及 .mw-100/.mh-100 对超限尺寸的上限约束。",
                expected=exp3,
                actual=act3,
                measurements=dict(mw_overflow_input=600, mh_overflow_input=500),
                passed=(act3 == exp3),
            ))

            # Case 4: Spacing scale (0..5), negative margins (n1..n5), and responsive spacing
            html4 = f"""<!doctype html><html><head><style>{BS4_UTILITIES_CSS}</style></head><body>
  <div id="s0" class="mt-0 p-0"></div>
  <div id="s1" class="ml-1 pt-1"></div>
  <div id="s2" class="mt-2 px-2"></div>
  <div id="s3" class="mb-3 p-3"></div>
  <div id="s4" class="mr-4 pb-4"></div>
  <div id="s5" class="my-5 py-5"></div>
  <div id="n1" class="mt-n1"></div>
  <div id="n2" class="mt-n2"></div>
  <div id="n3" class="mt-n3"></div>
  <div id="n4" class="mt-n4"></div>
  <div id="n5" class="mt-n5"></div>
  <div id="resp" class="mt-2 mt-md-4"></div>
</body></html>"""
            page.set_content(html4)
            page.set_viewport_size({"width": 500, "height": 500})
            resp_500 = page.eval_on_selector("#resp", "el => getComputedStyle(el).marginTop")
            page.set_viewport_size({"width": 800, "height": 500})
            act4 = page.evaluate("""() => {
              const cs = id => getComputedStyle(document.getElementById(id));
              return {
                scale_px: {
                  mt0: cs('s0').marginTop,
                  ml1: cs('s1').marginLeft,
                  px2: [cs('s2').paddingLeft, cs('s2').paddingRight],
                  p3: cs('s3').paddingTop,
                  pb4: cs('s4').paddingBottom,
                  my5: [cs('s5').marginTop, cs('s5').marginBottom]
                },
                neg_margins: {
                  n1: cs('n1').marginTop,
                  n2: cs('n2').marginTop,
                  n3: cs('n3').marginTop,
                  n4: cs('n4').marginTop,
                  n5: cs('n5').marginTop
                },
                resp_800: cs('resp').marginTop
              };
            }""")
            act4["resp_500"] = resp_500
            exp4 = {
                "scale_px": {
                    "mt0": "0px",
                    "ml1": "4px",
                    "px2": ["8px", "8px"],
                    "p3": "16px",
                    "pb4": "24px",
                    "my5": ["48px", "48px"],
                },
                "neg_margins": {"n1": "-4px", "n2": "-8px", "n3": "-16px", "n4": "-24px", "n5": "-48px"},
                "resp_500": "8px",
                "resp_800": "24px",
            }
            cases.append(dict(
                name="spacing_scale_and_negative_margins",
                inputs=dict(spacer_rem=1.0, root_font_px=16, viewports=[500, 800]),
                prerequisites="$spacer = 1rem = 16px,核对 0-5 阶梯、n1-n5 负外边距与响应式断点间距",
                action="读取正向间距、负外边距及 .mt-2.mt-md-4 在 500px/800px 视口的计算像素值。",
                expected=exp4,
                actual=act4,
                measurements=dict(ratios=[0, 0.25, 0.5, 1, 1.5, 3]),
                passed=(act4 == exp4),
            ))

            # Case 5: Form spacing example + shadow, vertical-align, and responsive display
            html5 = f"""<!doctype html><html><head><style>{BS4_UTILITIES_CSS}</style></head><body>
  <div id="fcenter" class="mx-auto" style="width:200px">元素设置居中</div>
  <form><label id="flabel" class="mt-2 d-block">设置 margin-top:</label></form>
  <div id="sh_sm" class="shadow-sm">sm</div>
  <div id="sh_none" class="shadow shadow-none">none</div>
  <span id="va_mid" class="align-middle">mid</span>
  <div id="d_resp" class="d-none d-md-block">md only</div>
</body></html>"""
            page.set_content(html5)
            page.set_viewport_size({"width": 500, "height": 500})
            d_500 = page.eval_on_selector("#d_resp", "el => getComputedStyle(el).display")
            page.set_viewport_size({"width": 800, "height": 500})
            act5 = page.evaluate("""() => {
              const cs = id => getComputedStyle(document.getElementById(id));
              return {
                label_mt: cs('flabel').marginTop,
                shadow_sm_has_value: cs('sh_sm').boxShadow !== 'none',
                shadow_none: cs('sh_none').boxShadow,
                vertical_align: cs('va_mid').verticalAlign,
                d_800: cs('d_resp').display
              };
            }""")
            act5["d_500"] = d_500
            exp5 = {
                "label_mt": "8px",
                "shadow_sm_has_value": True,
                "shadow_none": "none",
                "vertical_align": "middle",
                "d_500": "none",
                "d_800": "block",
            }
            cases.append(dict(
                name="utility_form_shadow_align_display",
                inputs=dict(viewports=[500, 800]),
                prerequisites="表单标签应用 .mt-2,并测试常用配套工具类 .shadow-*、.align-middle 与 .d-none.d-md-block",
                action="验证表单外边距、阴影开关、垂直对齐以及 500px/800px 下的响应式显隐切换。",
                expected=exp5,
                actual=act5,
                measurements=dict(center_box_width=200),
                passed=(act5 == 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-utilities-lab/explore-utilities.py

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

下载课程关联工程包:Bootstrap4

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

下载课程关联工程包:Bootstrap4

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

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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