Bootstrap 4 Flex
系统核对 Bootstrap 4 弹性盒子容器、主轴方向、主轴对齐、等宽与伸缩、排序与自动外边距、多行包裹与交叉轴对齐,以及完整响应式 flex 工具类对照表与离线 Chromium 实测。
学完这一章,你能够
- 系统核对 Bootstrap 4 弹性盒子容器、主轴方向、主轴对齐、等宽与伸缩、排序与自动外边距、多行包裹与交叉轴对齐,以及完整响应式 flex 工具类对照表与离线 Chromium 实测。
弹性容器与浏览器兼容性
Bootstrap 4 与 Bootstrap 3 在布局体系上的根本差异在于全面采用 CSS3 弹性盒子(Flexbox)取代传统浮动布局,借助丰富的 `flex` 工具类即可灵活应对现代响应式排版。需要注意的是,IE9 及更低版本不支持 Flexbox;如果项目必须兼容 IE8–IE9,应选择 Bootstrap 3。
创建块级弹性容器使用 `.d-flex`(`display: flex`),创建与行内文本同排展示的行内弹性容器则使用 `.d-inline-flex`(`display: inline-flex`)。本站离线 Chromium 在 `600px` 父容器中放置三个 `100px` 子项实测:`.d-flex` 容器占满整行宽 `600px`,而 `.d-inline-flex` 容器恰好收缩包裹内容为 `300px`。
主轴方向与主轴内容排列
水平主轴由 `.flex-row`(默认,从左到右)与 `.flex-row-reverse`(从右到左反向排列并贴右对齐)控制;垂直主轴由 `.flex-column`(从上到下)与 `.flex-column-reverse`(从下到上翻转)控制。本站实测 `600px` 容器内三个 `100px` 子项:`.flex-row` 左坐标为 `[0, 100, 200]`,`.flex-row-reverse` 翻转为 `[500, 400, 300]`,而 `.flex-column-reverse` 将第 3 项置顶、第 1 项置底。
主轴对齐通过 `.justify-content-*` 控制,取值涵盖 `start`(默认左对齐)、`end`(右对齐)、`center`(居中)、`between`(两端贴边、中间等距)与 `around`(每项两侧分配相等空间)。本站实测 `600px` 容器内三个 `100px` 子项的左坐标依次为:`start` `[0, 100, 200]`、`end` `[300, 400, 500]`、`center` `[150, 250, 350]`、`between` `[0, 250, 500]`、`around` `[50, 250, 450]`。
等宽、扩展收缩、排序与自动外边距
空间分配方面:`.flex-fill`(`flex: 1 1 auto !important`)使各弹性子项共同撑满剩余空间(当各项固有内容宽度一致时呈严格等宽,本站实测三个同宽项在 `600px` 容器内各占 `200px`);`.flex-grow-1` 让指定子项吸收全部剩余空间(前两项保持 `100px`,第三项扩展至 `400px`);收缩控制使用 `.flex-shrink-0`(禁止压缩)与 `.flex-shrink-1`(允许压缩),本站实测 `300px` 容器放入两个 `200px` 子项时,设了 `.flex-shrink-0` 的保持 `200px`,而 `.flex-shrink-1` 压缩为 `100px`。
视觉顺序由 `.order-0` 至 `.order-12` 控制,数值越小越靠前(例如 DOM 中依次放 `.order-3`、`.order-2`、`.order-1`,本站实测三项左坐标变为 `[200, 100, 0]`,即第 3 项排在最左)。在弹性容器内叠加 `.mr-auto`(`margin-right: auto !important`)或 `.ml-auto`(`margin-left: auto !important`)可把剩余空间全部吸进指定一侧:本站实测 `600px` 容器中第 1 项加 `.mr-auto` 后三项左坐标为 `[0, 400, 500]`,第 3 项加 `.ml-auto` 后三项左坐标为 `[0, 100, 500]`。
多行包裹、多行堆叠与交叉轴对齐
换行策略由 `.flex-nowrap`(默认单行不换行)、`.flex-wrap`(自动换行)与 `.flex-wrap-reverse`(反向换行)控制。多行容器的交叉轴行间堆叠通过 `.align-content-*` 调整,包括 `start`、`end`、`center`、`between`、`around` 与 `stretch`——需要特别注意,该系列类仅在发生多行换行时生效,单行容器下无效。本站实测 `300px × 200px` 容器放四个 `100×40px` 子项:`.flex-nowrap` 仅 `1` 行,`.flex-wrap.align-content-end` 将两行贴底于相对顶部 `[120, 120, 120, 160]`,`.flex-wrap-reverse` 使首行位于次行下方。
单行内全部子项的交叉轴对齐使用 `.align-items-*`,有效取值为 `start`、`end`、`center`、`baseline` 与 `stretch`(默认;需注意 CSS 与 Bootstrap 4 中并无 `align-items-around`,对应基线对齐应写 `.align-items-baseline`)。若仅覆盖单个子项的交叉轴对齐,则在该子项上添加 `.align-self-start`/`end`/`center`/`baseline`/`stretch`。本站实测 `150px` 高容器内,默认拉伸的两项高度均为 `150px`,而添加 `.align-self-start` 的第 2 项保持自身 `40px` 高度且贴顶显示。
所有核心 Flex 工具类均支持按断点加缀(`*` 取 `sm`、`md`、`lg`、`xl`)构建响应式布局。本站离线 Chromium 在 `500px`、`800px`、`1024px` 三个视口实测 `.d-block.d-md-flex`、`.flex-column.flex-md-row`、`.justify-content-md-between`、`.order-2.order-md-1`、`.align-self-md-end` 与 `.align-items-lg-center`:随视口越过 `768px` 与 `992px` 阈值,布局依次由块级竖排平滑切换为水平两端对齐、重排序并最终垂直居中。完整响应式类名汇总如下表。
Bootstrap 4 响应式 Flex 工具类速查表(* 代表 sm / md / lg / xl)
| 响应式类名 | 作用说明 |
|---|---|
| 【分组】弹性容器 | 按断点控制元素是否启用块级或行内弹性容器 |
| `.d-*-flex` | 在指定断点及以上创建块级弹性容器(`display: flex`) |
| `.d-*-inline-flex` | 在指定断点及以上创建行内弹性容器(`display: inline-flex`) |
| 【分组】主轴方向 | 按断点切换水平/垂直与正向/反向主轴 |
| `.flex-*-row` | 在指定断点及以上设置水平从左到右排列 |
| `.flex-*-row-reverse` | 在指定断点及以上设置水平反向(右对齐)排列 |
| `.flex-*-column` | 在指定断点及以上设置垂直从上到下排列 |
| `.flex-*-column-reverse` | 在指定断点及以上设置垂直反向排列 |
| 【分组】子项排序 | 按断点动态重排子项视觉顺序而不改 DOM |
| `.order-*-0` 至 `.order-*-12` | 在指定断点及以上设置子项 `order` 权重(0–12,越小越靠前) |
| 【分组】主轴对齐(justify-content) | 按断点控制弹性子项沿主轴的分布方式 |
| `.justify-content-*-start` | 在指定断点及以上向主轴起点对齐 |
| `.justify-content-*-end` | 在指定断点及以上向主轴终点对齐 |
| `.justify-content-*-center` | 在指定断点及以上在主轴方向居中对齐 |
| `.justify-content-*-between` | 在指定断点及以上两端对齐且项间等距 |
| `.justify-content-*-around` | 在指定断点及以上为每项两侧分配相等间距 |
| 【分组】填充剩余空间 | 按断点为子项启用 `flex: 1 1 auto` |
| `.flex-*-fill` | 在指定断点及以上按内容基础共同填充可用主轴宽度 |
| 【分组】扩展因子(flex-grow) | 按断点允许或禁止子项扩张占用剩余空间 |
| `.flex-*-grow-0` | 在指定断点及以上禁止子项扩展(`flex-grow: 0`) |
| `.flex-*-grow-1` | 在指定断点及以上允许子项吸收剩余空间(`flex-grow: 1`) |
| 【分组】收缩因子(flex-shrink) | 按断点允许或禁止子项在空间不足时收缩 |
| `.flex-*-shrink-0` | 在指定断点及以上禁止子项收缩(`flex-shrink: 0`) |
| `.flex-*-shrink-1` | 在指定断点及以上允许子项按需收缩(`flex-shrink: 1`) |
| 【分组】换行包裹(flex-wrap) | 按断点控制单行显示或多行折行 |
| `.flex-*-nowrap` | 在指定断点及以上保持单行不换行 |
| `.flex-*-wrap` | 在指定断点及以上允许子项折成多行 |
| `.flex-*-wrap-reverse` | 在指定断点及以上反向折行排列 |
| 【分组】多行交叉轴堆叠(align-content) | 仅多行生效,按断点控制行组在交叉轴的分布 |
| `.align-content-*-start` | 在指定断点及以上将多行堆叠在交叉轴起点 |
| `.align-content-*-end` | 在指定断点及以上将多行堆叠在交叉轴终点 |
| `.align-content-*-center` | 在指定断点及以上将多行居中堆叠 |
| `.align-content-*-around` | 在指定断点及以上在各行周围均匀分配空间 |
| `.align-content-*-stretch` | 在指定断点及以上拉伸各行填满交叉轴高度 |
| 【分组】子项交叉轴对齐(align-items) | 按断点统一设置容器内子项的交叉轴对齐方式 |
| `.align-items-*-start` | 在指定断点及以上使子项向交叉轴起点对齐 |
| `.align-items-*-end` | 在指定断点及以上使子项向交叉轴终点对齐 |
| `.align-items-*-center` | 在指定断点及以上使子项在交叉轴居中 |
| `.align-items-*-baseline` | 在指定断点及以上按首行文字基线对齐 |
| `.align-items-*-stretch` | 在指定断点及以上拉伸子项填满交叉轴 |
| 【分组】单项覆盖对齐(align-self) | 按断点覆盖单个子项自身的交叉轴对齐 |
| `.align-self-*-start` | 在指定断点及以上使当前子项贴交叉轴起点 |
| `.align-self-*-end` | 在指定断点及以上使当前子项贴交叉轴终点 |
| `.align-self-*-center` | 在指定断点及以上使当前子项交叉轴居中 |
| `.align-self-*-baseline` | 在指定断点及以上使当前子项按基线对齐 |
| `.align-self-*-stretch` | 在指定断点及以上拉伸当前子项高度 |
运行弹性容器方向、主轴对齐与伸缩、排序与自动外边距、多行包裹及响应式断点实测
"""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()
- 核对 .d-flex(600px)/.d-inline-flex(300px)、四方向坐标翻转、五种 justify-content-* 精确坐标、.flex-fill(200px 等宽)/.flex-grow-1(400px)/.flex-shrink-0/1(200/100px)、order 重排与 mr/ml-auto、wrap/align-content/align-items/align-self 及 500/800/1024px 断点切换。
所需环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
当前源码 SHA256:80f2f32712929977057b356c1a705451acb27f2482c3d2a566bd21a6ffb1c811
本地运行
解压后按 README 说明在对应本地环境运行脚本。
所需环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
ZIP SHA256:492ea8556ef5e32702380460909ef12c4a33899d2a107b36546d8a838f4adda3
常见错误与边界
- 实际端口、文档资源与应用配置都应和测试条件一致。
练习与解答
为什么 `.align-content-*` 在只有一行的弹性容器上看不出效果?另外给三个文本长度差异很大的子项加上 `.flex-fill` 时,它们一定会严格等宽吗?
展开参考解答
`align-content` 控制的是多条弹性行(flex lines)之间的交叉轴空间分配,单行容器只有一条行轨因此不生效,单行对齐应改用 `align-items`;`.flex-fill` 对应 `flex: 1 1 auto !important`,基准尺寸为 `auto`(固有内容宽度),它把剩余空间均分给各子项,只有内容固有宽度相近时才严格等宽,若需忽略内容长度强制等宽应使用 `flex-basis: 0`(如栅格 `.col`)。