Bootstrap4 小工具
系统核对 Bootstrap 4 边框与圆角、浮动与响应式浮动、水平居中、百分比宽高、正负内外边距语法与全方向速查表,并附离线 Chromium 计算样式实测。
学完这一章,你能够
- 系统核对 Bootstrap 4 边框与圆角、浮动与响应式浮动、水平居中、百分比宽高、正负内外边距语法与全方向速查表,并附离线 Chromium 计算样式实测。
边框、边框颜色与圆角工具类
Bootstrap 4 提供了一整套原子化的实用工具类(Utilities),无需手写自定义 CSS 即可快速组合常见视觉与布局样式。边框方面,`.border` 为元素添加四边 `1px solid #dee2e6` 边框,`.border-0` 移除全部边框,`.border-top-0`、`.border-right-0`、`.border-bottom-0`、`.border-left-0` 分别仅清除指定单侧边框。
边框颜色可通过九种主题语义类调整:`.border-primary`、`secondary`、`success`、`danger`、`warning`、`info`、`light`、`dark` 与 `white`;圆角则由 `.rounded`(四角 `0.25rem`)、单侧圆角 `.rounded-top`/`right`/`bottom`/`left`、正圆 `.rounded-circle`(`50%`)以及清除圆角 `.rounded-0` 控制。本站离线 Chromium 实测(根字号 `16px`):`.border` 顶宽 `1px`、`.border-top-0` 顶宽 `0px` 且底宽保持 `1px`,`.border-primary`/`success`/`danger`/`dark` 精确输出 `rgb(0, 123, 255)`、`rgb(40, 167, 69)`、`rgb(220, 53, 69)` 与 `rgb(52, 58, 64)`,`.rounded` 为 `4px`、`.rounded-top` 左上 `4px` 而左下 `0px`、`.rounded-circle` 为 `50%`、`.rounded-0` 为 `0px`。
浮动、清除浮动与响应式浮动
元素浮动使用 `.float-left`(左浮动)、`.float-right`(右浮动)与 `.float-none`(取消浮动),并在父容器上添加 `.clearfix` 通过伪元素 `::after { display: block; clear: both; content: ""; }` 防止高度塌陷。本站实测同组 `40px` 高的左右浮动子元素:未加 `.clearfix` 时父容器高度塌陷为 `0px`,加上 `.clearfix` 后恢复为 `40px`。
响应式浮动按 `.float-{breakpoint}-{left|right|none}` 规则随屏幕断点生效(`*` 取 `sm`、`md`、`lg`、`xl`)。本站在 `500px`、`600px`、`800px`、`1024px`、`1280px` 五个视口实测 `.float-sm-right` 至 `.float-xl-right`:在 `500px` 全部保持 `none`,达到 `600px`(`≥576px`)、`800px`(`≥768px`)、`1024px`(`≥992px`)与 `1280px`(`≥1200px`)后对应类依次切换为 `right`,而 `.float-none` 全程为 `none`。
水平居中与百分比宽高尺寸
定宽块级元素可通过 `.mx-auto`(左右外边距均为 `auto !important`)在父容器中水平居中。本站在 `400px` 父容器内放入宽 `150px` 的 `.mx-auto` 元素,实测其左坐标与左右外边距均为精确的 `125px`。
相对父容器的尺寸工具类包含宽度 `.w-25`、`.w-50`、`.w-75`、`.w-100`、`.mw-100`(`max-width: 100%`)以及高度 `.h-25`、`.h-50`、`.h-75`、`.h-100`、`.mh-100`(`max-height: 100%`,使用百分比高度要求父元素具备明确高度)。本站在 `400px × 200px` 父容器中实测:`.w-25..100` 依次为 `100px`、`200px`、`300px`、`400px`,设了 `width: 600px` 的 `.mw-100` 被约束为 `400px`;`.h-25..100` 依次为 `50px`、`100px`、`150px`、`200px`,设了 `height: 500px` 的 `.mh-100` 被约束为 `200px`。
间距命名语法、负外边距与配套工具类
Bootstrap 4 的外边距与内边距统一采用 `{property}{sides}-{size}`(适用全尺寸含 `xs < 576px`)与 `{property}{sides}-{breakpoint}-{size}`(适用 `sm ≥ 576px`、`md ≥ 768px`、`lg ≥ 992px`、`xl ≥ 1200px`)两种语法。其中 `property` 仅取两个字母:`m` 表示 `margin`,`p` 表示 `padding`。
`sides` 控制生效方向:`t` 对应 top、`b` 对应 bottom、`l` 对应 left、`r` 对应 right、`x` 同时设置 left 与 right、`y` 同时设置 top 与 bottom,省略方向(blank)则同时作用于上下左右四个方向。
`size` 以基准变量 `$spacer`(默认 `1rem`,根字号 `16px` 时为 `16px`)按阶梯缩放:`0` 为 `0`,`1` 为 `$spacer * 0.25`(`4px`),`2` 为 `$spacer * 0.5`(`8px`),`3` 为 `$spacer`(`16px`),`4` 为 `$spacer * 1.5`(`24px`),`5` 为 `$spacer * 3`(`48px`),`auto` 将外边距设为 `auto`。
Bootstrap 4.2+ 还支持负外边距(仅适用于 `margin`,不适用于 `padding`),在数字前加 `n`:`n1` 至 `n5` 分别对应 `-0.25rem`(`-4px`)、`-0.5rem`(`-8px`)、`-1rem`(`-16px`)、`-1.5rem`(`-24px`)与 `-3rem`(`-48px`)。
源码层面所有间距类都附带 `!important`(如 `.mt-0`、`.ml-1`、`.px-2`、`.p-3`),因此表单标签等组件上直接叠加 `.mt-2` 即可稳定生效。本站离线 Chromium 实测验证了 `0..5` 全阶梯(`0px / 4px / 8px / 16px / 24px / 48px`)、`n1..n5`(`-4px` 至 `-48px`)、响应式 `.mt-2.mt-md-4`(`500px` 时 `8px`,`800px` 时 `24px`),以及常见配套工具类 `.shadow-sm`/`.shadow-none`、`.align-middle` 和 `.d-none.d-md-block`(`500px` 隐藏、`800px` 块级显示)。下方速查表列出全部 14 组方向间距类。
Bootstrap 4 间距工具类速查表
| 类名模式 | 作用属性 | 方向与响应说明 |
|---|---|---|
| `.m-#` / `.m-*-#` | 四边 `margin` | 同时设置上下左右外边距,`#` 取 `0-5`/`n1-n5`/`auto`,`*` 取断点 |
| `.mt-#` / `.mt-*-#` | `margin-top` | 仅设置顶部外边距,支持断点覆盖(如 `.mt-2.mt-md-4`) |
| `.mb-#` / `.mb-*-#` | `margin-bottom` | 仅设置底部外边距,常用于分隔段落与卡片模块 |
| `.ml-#` / `.ml-*-#` | `margin-left` | 仅设置左侧外边距,配合 `auto` 可在 Flex 中推向右侧 |
| `.mr-#` / `.mr-*-#` | `margin-right` | 仅设置右侧外边距,常用于图文间距与右推布局 |
| `.mx-#` / `.mx-*-#` | `margin-left` 与 `margin-right` | 同时设置水平两侧外边距,`.mx-auto` 用于定宽块居中 |
| `.my-#` / `.my-*-#` | `margin-top` 与 `margin-bottom` | 同时设置垂直上下外边距,保持区块纵向节奏统一 |
| `.p-#` / `.p-*-#` | 四边 `padding` | 同时设置上下左右内边距,`#` 仅支持非负值 `0-5` |
| `.pt-#` / `.pt-*-#` | `padding-top` | 仅设置顶部内边距,常用于避开固定顶栏或增加标题留白 |
| `.pb-#` / `.pb-*-#` | `padding-bottom` | 仅设置底部内边距,支持响应式断点调整留白密度 |
| `.pl-#` / `.pl-*-#` | `padding-left` | 仅设置左侧内边距,常用于列表缩进与正文留边 |
| `.pr-#` / `.pr-*-#` | `padding-right` | 仅设置右侧内边距,支持断点差异化排版 |
| `.py-#` / `.py-*-#` | `padding-top` 与 `padding-bottom` | 同时设置垂直上下内边距,常用于导航栏与条幅区块 |
| `.px-#` / `.px-*-#` | `padding-left` 与 `padding-right` | 同时设置水平左右内边距,控制按钮与容器横向呼吸感 |
运行边框圆角、响应式浮动、居中宽高、正负间距与显隐阴影工具类实测
"""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()
- 核对 1px/0px 边框与 4 种主题 RGB 色、4px/50%/0px 圆角、clearfix 修复 0px→40px 塌陷与 5 断点浮动切换、150px 块 mx-auto 左右 125px、w-*/h-* 与 mw-100/mh-100 上限约束、0..5 与 n1..n5 全阶梯像素值及 500px/800px 响应式切换。
所需环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
当前源码 SHA256:54efccafed9271229e83216bfdbb9c3f0e60cdbeb9d181b381e4e7e9efe8b88f
本地运行
解压后按 README 说明在对应本地环境运行脚本。
所需环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
ZIP SHA256:fd582e58da19d792224f4c85e0061c544ae98b8ce5f2110ff82a95c879df5498
常见错误与边界
- 实际端口、文档资源与应用配置都应和测试条件一致。
练习与解答
在 Bootstrap 4 中同时给元素写 `.mt-2` 与 `.mt-md-4` 时,两个类都带 `!important`,为什么视口达到 768px 后能正确变成 24px 而不是继续停留在 8px?
展开参考解答
Bootstrap 4 编译生成的 CSS 严格按移动优先顺序排列,所有无断点基础类(如 `.mt-2`)位于前面,各 `@media (min-width: ...)` 响应式断点类(`sm` → `md` → `lg` → `xl`)依次排在后面;在选择器权重和 `!important` 相同时,后声明的 `.mt-md-4` 按级联顺序覆盖 `.mt-2`。