Tailwind CSS Flexbox & Grid
系统核对 Flexbox(basis、方向、换行、伸缩简写、grow/shrink、order)与 CSS Grid(grid-cols、subgrid、跨行列、轨道线定位及对齐分布)并离线实测。
学完这一章,你能够
- 系统核对 Flexbox(basis、方向、换行、伸缩简写、grow/shrink、order)与 CSS Grid(grid-cols、subgrid、跨行列、轨道线定位及对齐分布)并离线实测。
Flexbox 容器、主轴初始尺寸、方向与换行
Tailwind CSS 通过实用类完整覆盖一维弹性布局(Flexbox)与二维网格布局(CSS Grid)。添加 `flex` 即可建立弹性容器,使用 `basis-*`(涵盖固定 rem 刻度、`basis-auto`、`basis-px`、`basis-full` 及 1/2 至 11/12 分数比例)控制弹性项初始主轴尺寸(注:`basis-3/6` 对应 `50%`,即二分之一)。本站离线 Chromium 实测 `400px` 宽 `flex-row` 容器内 `basis-1/4`、`basis-1/4`、`basis-1/2` 三项宽度精确为 `[100, 100, 200]`。
`flex-row`、`flex-row-reverse`、`flex-col`、`flex-col-reverse` 控制主轴排列方向;默认 `flex-nowrap` 将子项压在单行,而 `flex-wrap` 与 `flex-wrap-reverse` 允许超出容器宽度的子项折行。实测 `flex-row-reverse` 使首项左坐标大于末项,`flex-col` 使第二项垂直紧接首项之下;在 `200px` 容器放入三个 `90px` 项目时,`flex-nowrap` 三项同行,`flex-wrap` 将第三项折至下一行。
Flex 伸缩简写、grow/shrink 与视觉排序
伸缩简写类决定剩余空间分配与溢出收缩策略:`flex-1`(`1 1 0%`)忽略原始宽度按比例均分剩余空间,`flex-auto`(`1 1 auto`)基于内容/初始尺寸伸缩,`flex-initial`(`0 1 auto`)只缩不涨,`flex-none` 既不增长也不收缩;也可通过 `grow`/`grow-0` 与 `shrink`/`shrink-0` 单独控制。本站实测 `500px` 容器内 `flex-none w-14`(`56px`)搭配两个 `flex-1` 子项(即便分别标有 `w-64` 与 `w-32`),实测宽度均分为 `[56, 222, 222]`;换作 `flex-initial` 则保持 `[56, 256, 128]`;中间项用 `grow` 填满至 `[56, 388, 56]`;在 `200px` 窄容器中使用 `shrink w-64` 时中间项自动压缩至 `88px`。
`order-1` 至 `order-12`、`order-first`(`-9999`)、`order-last`(`9999`)与 `order-none`(`0`)可在不改动 DOM 顺序的前提下调整 Flexbox 或 Grid 子项视觉次序。实测对三项分别设置 `order-last`、`order-none`、`order-first` 后,计算 `order` 依次为 `'9999'`、`'0'`、`'-9999'`,页面左起坐标反序排布为 `[0, 130, 260]`。
Grid 网格轨道、跨行列、Subgrid 与定位
CSS Grid 通过 `grid` 启用,`grid-cols-{n}`(1–12、`none`、`subgrid`)与 `grid-rows-{n}` 定义列行轨道,`gap-{size}` 设定网格沟槽,`col-span-{n}` 与 `row-span-{n}` 控制单元格跨越多列或多行。实测 `448px` 宽 `grid-cols-4 gap-4` 每列精确宽 `100px`;内部嵌套 `grid-cols-subgrid col-span-3` 且子项声明 `col-start-2` 时,子项完整继承父网格轨道(宽 `100px`、左坐标 `232px`);`332px` 宽 `grid-cols-3 gap-4` 中的 `col-span-2` 实测宽 `216px`(含中间 `16px` 间隙)。
需要精确落点时,可用 `col-start-{n}`、`col-end-{n}`、`row-start-{n}` 与 `row-end-{n}` 指定起止网格线编号(6 列网格共 7 条垂直网格线)。实测 `680px` 宽 `grid-cols-6 gap-4` 中,`col-start-2 col-span-4` 起始于 `116px`、宽 `448px`;`col-start-1 col-end-7` 横跨全部 6 列达到 `680px`。
对齐与空间分布
`justify-content` 系列工具类(`justify-normal`、`start`、`end`、`center`、`between`、`around`、`evenly`、`stretch`)控制主轴空间分布;网格与弹性容器还支持 `justify-items-*`、`justify-self-*`、`items-*`(`align-items`)与 `self-*`(`align-self`)对齐单个或全部单元格。本站实测 `300px` 容器内三个 `50px` 子项在 `justify-start`/`center`/`between` 下的左坐标依次为 `[0, 50, 100]`、`[75, 125, 175]` 与 `[0, 125, 250]`;`200×100` 两列网格应用 `justify-items-center items-center` 后,首个 `40×20` 项在 `100×100` 单元格内相对坐标精准居中于 `[30, 40]`。
Flex 项初始主轴尺寸(Flex Basis)
| Tailwind 类名 | CSS 属性及值 | 说明 |
| basis-0 | flex-basis: 0px; | 设置 flex 项初始主轴尺寸为 0px(0px) |
| basis-1 | flex-basis: 0.25rem; | 设置 flex 项初始主轴尺寸为 0.25rem(4px) |
| basis-2 | flex-basis: 0.5rem; | 设置 flex 项初始主轴尺寸为 0.5rem(8px) |
| basis-3 | flex-basis: 0.75rem; | 设置 flex 项初始主轴尺寸为 0.75rem(12px) |
| basis-4 | flex-basis: 1rem; | 设置 flex 项初始主轴尺寸为 1rem(16px) |
| basis-5 | flex-basis: 1.25rem; | 设置 flex 项初始主轴尺寸为 1.25rem(20px) |
| basis-6 | flex-basis: 1.5rem; | 设置 flex 项初始主轴尺寸为 1.5rem(24px) |
| basis-7 | flex-basis: 1.75rem; | 设置 flex 项初始主轴尺寸为 1.75rem(28px) |
| basis-8 | flex-basis: 2rem; | 设置 flex 项初始主轴尺寸为 2rem(32px) |
| basis-9 | flex-basis: 2.25rem; | 设置 flex 项初始主轴尺寸为 2.25rem(36px) |
| basis-10 | flex-basis: 2.5rem; | 设置 flex 项初始主轴尺寸为 2.5rem(40px) |
| basis-11 | flex-basis: 2.75rem; | 设置 flex 项初始主轴尺寸为 2.75rem(44px) |
| basis-12 | flex-basis: 3rem; | 设置 flex 项初始主轴尺寸为 3rem(48px) |
| basis-14 | flex-basis: 3.5rem; | 设置 flex 项初始主轴尺寸为 3.5rem(56px) |
| basis-16 | flex-basis: 4rem; | 设置 flex 项初始主轴尺寸为 4rem(64px) |
| basis-20 | flex-basis: 5rem; | 设置 flex 项初始主轴尺寸为 5rem(80px) |
| basis-24 | flex-basis: 6rem; | 设置 flex 项初始主轴尺寸为 6rem(96px) |
| basis-28 | flex-basis: 7rem; | 设置 flex 项初始主轴尺寸为 7rem(112px) |
| basis-32 | flex-basis: 8rem; | 设置 flex 项初始主轴尺寸为 8rem(128px) |
| basis-36 | flex-basis: 9rem; | 设置 flex 项初始主轴尺寸为 9rem(144px) |
| basis-40 | flex-basis: 10rem; | 设置 flex 项初始主轴尺寸为 10rem(160px) |
| basis-44 | flex-basis: 11rem; | 设置 flex 项初始主轴尺寸为 11rem(176px) |
| basis-48 | flex-basis: 12rem; | 设置 flex 项初始主轴尺寸为 12rem(192px) |
| basis-52 | flex-basis: 13rem; | 设置 flex 项初始主轴尺寸为 13rem(208px) |
| basis-56 | flex-basis: 14rem; | 设置 flex 项初始主轴尺寸为 14rem(224px) |
| basis-60 | flex-basis: 15rem; | 设置 flex 项初始主轴尺寸为 15rem(240px) |
| basis-64 | flex-basis: 16rem; | 设置 flex 项初始主轴尺寸为 16rem(256px) |
| basis-72 | flex-basis: 18rem; | 设置 flex 项初始主轴尺寸为 18rem(288px) |
| basis-80 | flex-basis: 20rem; | 设置 flex 项初始主轴尺寸为 20rem(320px) |
| basis-96 | flex-basis: 24rem; | 设置 flex 项初始主轴尺寸为 24rem(384px) |
| basis-auto | flex-basis: auto; | 按内容或显式宽高自动确定主轴初始尺寸 |
| basis-px | flex-basis: 1px; | 设置 flex 项初始主轴尺寸为 1px |
| basis-0.5 | flex-basis: 0.125rem; | 设置 flex 项初始主轴尺寸为 0.125rem(2px) |
| basis-1.5 | flex-basis: 0.375rem; | 设置 flex 项初始主轴尺寸为 0.375rem(6px) |
| basis-2.5 | flex-basis: 0.625rem; | 设置 flex 项初始主轴尺寸为 0.625rem(10px) |
| basis-3.5 | flex-basis: 0.875rem; | 设置 flex 项初始主轴尺寸为 0.875rem(14px) |
| basis-1/2 | flex-basis: 50%; | 占容器主轴尺寸的 1/2(50%) |
| basis-1/3 | flex-basis: 33.333333%; | 占容器主轴尺寸的 1/3(约 33.33%) |
| basis-2/3 | flex-basis: 66.666667%; | 占容器主轴尺寸的 2/3(约 66.67%) |
| basis-1/4 | flex-basis: 25%; | 占容器主轴尺寸的 1/4(25%) |
| basis-2/4 | flex-basis: 50%; | 占容器主轴尺寸的 2/4(50%) |
| basis-3/4 | flex-basis: 75%; | 占容器主轴尺寸的 3/4(75%) |
| basis-1/5 | flex-basis: 20%; | 占容器主轴尺寸的 1/5(20%) |
| basis-2/5 | flex-basis: 40%; | 占容器主轴尺寸的 2/5(40%) |
| basis-3/5 | flex-basis: 60%; | 占容器主轴尺寸的 3/5(60%) |
| basis-4/5 | flex-basis: 80%; | 占容器主轴尺寸的 4/5(80%) |
| basis-1/6 | flex-basis: 16.666667%; | 占容器主轴尺寸的 1/6(约 16.67%) |
| basis-2/6 | flex-basis: 33.333333%; | 占容器主轴尺寸的 2/6(约 33.33%) |
| basis-3/6 | flex-basis: 50%; | 占容器主轴尺寸的 3/6(即二分之一,50%) |
| basis-4/6 | flex-basis: 66.666667%; | 占容器主轴尺寸的 4/6(约 66.67%) |
| basis-5/6 | flex-basis: 83.333333%; | 占容器主轴尺寸的 5/6(约 83.33%) |
| basis-1/12 | flex-basis: 8.333333%; | 占容器主轴尺寸的 1/12(约 8.33%) |
| basis-2/12 | flex-basis: 16.666667%; | 占容器主轴尺寸的 2/12(约 16.67%) |
| basis-3/12 | flex-basis: 25%; | 占容器主轴尺寸的 3/12(25%) |
| basis-4/12 | flex-basis: 33.333333%; | 占容器主轴尺寸的 4/12(约 33.33%) |
| basis-5/12 | flex-basis: 41.666667%; | 占容器主轴尺寸的 5/12(约 41.67%) |
| basis-6/12 | flex-basis: 50%; | 占容器主轴尺寸的 6/12(50%) |
| basis-7/12 | flex-basis: 58.333333%; | 占容器主轴尺寸的 7/12(约 58.33%) |
| basis-8/12 | flex-basis: 66.666667%; | 占容器主轴尺寸的 8/12(约 66.67%) |
| basis-9/12 | flex-basis: 75%; | 占容器主轴尺寸的 9/12(75%) |
| basis-10/12 | flex-basis: 83.333333%; | 占容器主轴尺寸的 10/12(约 83.33%) |
| basis-11/12 | flex-basis: 91.666667%; | 占容器主轴尺寸的 11/12(约 91.67%) |
| basis-full | flex-basis: 100%; | 占容器主轴尺寸的 100% |
Flex 伸缩简写工具类(Flex)
| Tailwind 类名 | CSS 属性及值 | 说明 |
| flex-1 | flex: 1 1 0%; | 可增长可收缩且基础尺寸为 0%,忽略原始宽度均分剩余空间 |
| flex-auto | flex: 1 1 auto; | 基于自身初始尺寸按比例增长或收缩以填充空间 |
| flex-initial | flex: 0 1 auto; | 不主动增长但在空间不足时可收缩 |
| flex-none | flex: none; | 等效 0 0 auto,固定不增长也不收缩 |
Flex 增长比例(Flex Grow)
| Tailwind 类名 | CSS 属性及值 | 说明 |
| grow | flex-grow: 1; | 允许弹性项增长占据可用剩余空间 |
| grow-0 | flex-grow: 0; | 禁止弹性项增长 |
Flex 收缩比例(Flex Shrink)
| Tailwind 类名 | CSS 属性及值 | 说明 |
| shrink | flex-shrink: 1; | 允许弹性项在容器空间不足时收缩 |
| shrink-0 | flex-shrink: 0; | 禁止弹性项收缩变形 |
排列顺序工具类(Order)
| Tailwind 类名 | CSS 属性及值 | 说明 |
| order-1 | order: 1; | 设置 flex/grid 项目视觉顺序为 1 |
| order-2 | order: 2; | 设置 flex/grid 项目视觉顺序为 2 |
| order-3 | order: 3; | 设置 flex/grid 项目视觉顺序为 3 |
| order-4 | order: 4; | 设置 flex/grid 项目视觉顺序为 4 |
| order-5 | order: 5; | 设置 flex/grid 项目视觉顺序为 5 |
| order-6 | order: 6; | 设置 flex/grid 项目视觉顺序为 6 |
| order-7 | order: 7; | 设置 flex/grid 项目视觉顺序为 7 |
| order-8 | order: 8; | 设置 flex/grid 项目视觉顺序为 8 |
| order-9 | order: 9; | 设置 flex/grid 项目视觉顺序为 9 |
| order-10 | order: 10; | 设置 flex/grid 项目视觉顺序为 10 |
| order-11 | order: 11; | 设置 flex/grid 项目视觉顺序为 11 |
| order-12 | order: 12; | 设置 flex/grid 项目视觉顺序为 12 |
| order-first | order: -9999; | 将项目排至容器最前端 |
| order-last | order: 9999; | 将项目排至容器最末端 |
| order-none | order: 0; | 恢复默认 DOM 顺序(order: 0) |
网格列模板工具类(Grid Template Columns)
| Tailwind 类名 | CSS 属性及值 | 说明 |
| grid-cols-1 | grid-template-columns: repeat(1, minmax(0, 1fr)); | 创建 1 列等分网格轨道 |
| grid-cols-2 | grid-template-columns: repeat(2, minmax(0, 1fr)); | 创建 2 列等分网格轨道 |
| grid-cols-3 | grid-template-columns: repeat(3, minmax(0, 1fr)); | 创建 3 列等分网格轨道 |
| grid-cols-4 | grid-template-columns: repeat(4, minmax(0, 1fr)); | 创建 4 列等分网格轨道 |
| grid-cols-5 | grid-template-columns: repeat(5, minmax(0, 1fr)); | 创建 5 列等分网格轨道 |
| grid-cols-6 | grid-template-columns: repeat(6, minmax(0, 1fr)); | 创建 6 列等分网格轨道 |
| grid-cols-7 | grid-template-columns: repeat(7, minmax(0, 1fr)); | 创建 7 列等分网格轨道 |
| grid-cols-8 | grid-template-columns: repeat(8, minmax(0, 1fr)); | 创建 8 列等分网格轨道 |
| grid-cols-9 | grid-template-columns: repeat(9, minmax(0, 1fr)); | 创建 9 列等分网格轨道 |
| grid-cols-10 | grid-template-columns: repeat(10, minmax(0, 1fr)); | 创建 10 列等分网格轨道 |
| grid-cols-11 | grid-template-columns: repeat(11, minmax(0, 1fr)); | 创建 11 列等分网格轨道 |
| grid-cols-12 | grid-template-columns: repeat(12, minmax(0, 1fr)); | 创建 12 列等分网格轨道 |
| grid-cols-none | grid-template-columns: none; | 不显式定义网格列轨道 |
| grid-cols-subgrid | grid-template-columns: subgrid; | 启用子网格,直接继承父级网格列轨道线 |
主轴对齐与分布(Justify Content)
| Tailwind 类名 | CSS 属性及值 | 说明 |
| justify-normal | justify-content: normal; | 采用容器默认主轴对齐行为 |
| justify-start | justify-content: flex-start; | 子项向主轴起始端对齐(实测 [0, 50, 100]) |
| justify-end | justify-content: flex-end; | 子项向主轴结束端对齐 |
| justify-center | justify-content: center; | 子项整体在主轴居中(实测 [75, 125, 175]) |
| justify-between | justify-content: space-between; | 两端贴边、中间间距均分(实测 [0, 125, 250]) |
| justify-around | justify-content: space-around; | 每项两侧分配相等空白,首尾空白为项间一半 |
| justify-evenly | justify-content: space-evenly; | 项间与首尾边缘间距完全相等 |
| justify-stretch | justify-content: stretch; | 拉伸自动尺寸轨道以填满容器主轴 |
离线 Chromium 验证 Flexbox 伸缩/排序与 CSS Grid 轨道/Subgrid/对齐几何
展开查看收起
"""Offline Playwright Chromium lab for Tailwind CSS Ch 16: Flexbox & Grid."""
import json
from playwright.sync_api import sync_playwright
CSS = """
*, ::before, ::after { box-sizing: border-box; }
body { margin: 0; }
.flex { display: flex; }
.flex-row { flex-direction: row; }
.flex-row-reverse { flex-direction: row-reverse; }
.flex-col { flex-direction: column; }
.flex-nowrap { flex-wrap: nowrap; }
.flex-wrap { flex-wrap: wrap; }
.basis-1\\/4 { flex-basis: 25%; }
.basis-1\\/2 { flex-basis: 50%; }
.flex-1 { flex: 1 1 0%; }
.flex-auto { flex: 1 1 auto; }
.flex-initial { flex: 0 1 auto; }
.flex-none { flex: none; }
.grow { flex-grow: 1; }
.grow-0 { flex-grow: 0; }
.shrink { flex-shrink: 1; }
.shrink-0 { flex-shrink: 0; }
.w-14 { width: 3.5rem; }
.w-32 { width: 8rem; }
.w-64 { width: 16rem; }
.order-first { order: -9999; }
.order-last { order: 9999; }
.order-none { order: 0; }
.grid { display: grid; }
.grid-cols-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid-cols-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.grid-cols-4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.grid-cols-6 { grid-template-columns: repeat(6, minmax(0, 1fr)); }
.grid-cols-subgrid { grid-template-columns: subgrid; }
.gap-4 { gap: 1rem; }
.col-span-2 { grid-column: span 2 / span 2; }
.col-span-3 { grid-column: span 3 / span 3; }
.col-span-4 { grid-column: span 4 / span 4; }
.col-start-1 { grid-column-start: 1; }
.col-start-2 { grid-column-start: 2; }
.col-end-7 { grid-column-end: 7; }
.justify-start { justify-content: flex-start; }
.justify-center { justify-content: center; }
.justify-end { justify-content: flex-end; }
.justify-between { justify-content: space-between; }
.justify-evenly { justify-content: space-evenly; }
.justify-items-start { justify-items: start; }
.justify-items-center { justify-items: center; }
.items-center { align-items: center; }
"""
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": 900})
page.set_content(f"""
<!doctype html><html><head><style>{CSS}</style></head>
<body>
<div id="basis-row" class="flex flex-row" style="width:400px">
<div id="b1" class="basis-1/4">01</div>
<div id="b2" class="basis-1/4">02</div>
<div id="b3" class="basis-1/2">03</div>
</div>
<div id="rev-row" class="flex flex-row-reverse" style="width:300px">
<div id="r1" style="width:50px">01</div><div id="r2" style="width:50px">02</div><div id="r3" style="width:50px">03</div>
</div>
<div id="col-box" class="flex flex-col" style="width:200px">
<div id="c1" style="height:20px">01</div><div id="c2" style="height:20px">02</div>
</div>
<div class="flex flex-nowrap" style="width:200px">
<div id="nw1" style="width:90px;flex-shrink:0">1</div><div style="width:90px;flex-shrink:0">2</div><div id="nw3" style="width:90px;flex-shrink:0">3</div>
</div>
<div class="flex flex-wrap" style="width:200px">
<div id="w1" style="width:90px">1</div><div style="width:90px">2</div><div id="w3" style="width:90px">3</div>
</div>
<div class="flex" style="width:500px">
<div id="f1a" class="flex-none w-14">01</div>
<div id="f1b" class="flex-1 w-64">02</div>
<div id="f1c" class="flex-1 w-32">03</div>
</div>
<div class="flex" style="width:500px">
<div id="fia" class="flex-none w-14">01</div>
<div id="fib" class="flex-initial w-64">02</div>
<div id="fic" class="flex-initial w-32">03</div>
</div>
<div class="flex" style="width:500px">
<div id="ga" class="flex-none w-14">01</div>
<div id="gb" class="grow">02</div>
<div id="gc" class="flex-none w-14">03</div>
</div>
<div class="flex" style="width:200px">
<div id="sa" class="flex-none w-14">01</div>
<div id="sb" class="shrink w-64">02</div>
<div id="sc" class="flex-none w-14">03</div>
</div>
<div class="flex justify-between" style="width:300px">
<div id="o1" class="order-last" style="width:40px">01</div>
<div id="o2" class="order-none" style="width:40px">02</div>
<div id="o3" class="order-first" style="width:40px">03</div>
</div>
<div class="grid grid-cols-4 gap-4" style="width:448px">
<div id="g4-1">01</div><div>02</div><div>03</div><div>04</div>
<div>05</div>
<div id="subg" class="grid grid-cols-subgrid gap-4 col-span-3">
<div id="subg-item" class="col-start-2">06</div>
</div>
</div>
<div class="grid grid-cols-3 gap-4" style="width:332px">
<div>01</div><div>02</div><div>03</div>
<div id="g3-span2" class="col-span-2">04</div>
<div>05</div>
</div>
<div class="grid grid-cols-6 gap-4" style="width:680px">
<div id="g6-mid" class="col-start-2 col-span-4">01</div>
<div id="g6-full" class="col-start-1 col-end-7">04</div>
</div>
<div class="flex justify-start" style="width:300px"><div id="js1" style="width:50px">1</div><div id="js2" style="width:50px">2</div><div id="js3" style="width:50px">3</div></div>
<div class="flex justify-center" style="width:300px"><div id="jc1" style="width:50px">1</div><div id="jc2" style="width:50px">2</div><div id="jc3" style="width:50px">3</div></div>
<div class="flex justify-between" style="width:300px"><div id="jb1" style="width:50px">1</div><div id="jb2" style="width:50px">2</div><div id="jb3" style="width:50px">3</div></div>
<div class="grid grid-cols-2 justify-items-center items-center" style="width:200px;height:100px">
<div id="ji1" style="width:40px;height:20px">A</div>
<div id="ji2" style="width:40px;height:20px">B</div>
</div>
</body></html>
""")
# Case 1: flex-basis, flex-direction, flex-wrap
act1 = page.evaluate("""() => {
const r = id => document.getElementById(id).getBoundingClientRect();
return {
basis_widths: [Math.round(r('b1').width), Math.round(r('b2').width), Math.round(r('b3').width)],
row_reverse_r1_gt_r3: r('r1').left > r('r3').left,
col_stacked: Math.round(r('c2').top) >= Math.round(r('c1').bottom),
nowrap_same_line: Math.round(r('nw3').top) === Math.round(r('nw1').top),
wrap_next_line: r('w3').top > r('w1').top,
};
}""")
exp1 = {
"basis_widths": [100, 100, 200],
"row_reverse_r1_gt_r3": True,
"col_stacked": True,
"nowrap_same_line": True,
"wrap_next_line": True,
}
cases.append({
"name": "flex_basis_direction_and_wrap",
"passed": act1 == exp1,
"expected": exp1,
"actual": act1,
})
# Case 2: flex-1 vs flex-initial, grow, shrink
act2 = page.evaluate("""() => {
const w = ids => ids.map(id => Math.round(document.getElementById(id).getBoundingClientRect().width));
return {
flex_1: w(['f1a', 'f1b', 'f1c']),
flex_initial: w(['fia', 'fib', 'fic']),
grow_row: w(['ga', 'gb', 'gc']),
shrink_row: w(['sa', 'sb', 'sc']),
};
}""")
exp2 = {
"flex_1": [56, 222, 222],
"flex_initial": [56, 256, 128],
"grow_row": [56, 388, 56],
"shrink_row": [56, 88, 56],
}
cases.append({
"name": "flex_shorthands_grow_and_shrink",
"passed": act2 == exp2,
"expected": exp2,
"actual": act2,
})
# Case 3: order-first, order-none, order-last
act3 = page.evaluate("""() => {
const g = id => getComputedStyle(document.getElementById(id)).order;
const x = id => Math.round(document.getElementById(id).getBoundingClientRect().left);
return {
orders: [g('o1'), g('o2'), g('o3')],
visual_x: [x('o3'), x('o2'), x('o1')],
};
}""")
exp3 = {
"orders": ["9999", "0", "-9999"],
"visual_x": [0, 130, 260],
}
cases.append({
"name": "flex_and_grid_order_control",
"passed": act3 == exp3,
"expected": exp3,
"actual": act3,
})
# Case 4: Grid columns, span, subgrid, and col-start/end
act4 = page.evaluate("""() => {
const r = id => document.getElementById(id).getBoundingClientRect();
const subR = r('subg-item');
const g4_1 = r('g4-1');
return {
g4_col_width: Math.round(g4_1.width),
subgrid_item_width: Math.round(subR.width),
subgrid_item_left: Math.round(subR.left),
g3_span2_width: Math.round(r('g3-span2').width),
g6_mid_left_width: [Math.round(r('g6-mid').left), Math.round(r('g6-mid').width)],
g6_full_width: Math.round(r('g6-full').width),
};
}""")
exp4 = {
"g4_col_width": 100,
"subgrid_item_width": 100,
"subgrid_item_left": 232,
"g3_span2_width": 216,
"g6_mid_left_width": [116, 448],
"g6_full_width": 680,
}
cases.append({
"name": "grid_columns_span_subgrid_and_placement",
"passed": act4 == exp4,
"expected": exp4,
"actual": act4,
})
# Case 5: justify-content, justify-items, align-items
act5 = page.evaluate("""() => {
const xs = ids => ids.map(id => Math.round(document.getElementById(id).getBoundingClientRect().left));
const ji1 = document.getElementById('ji1').getBoundingClientRect();
const p = document.getElementById('ji1').parentElement.getBoundingClientRect();
return {
justify_start: xs(['js1', 'js2', 'js3']),
justify_center: xs(['jc1', 'jc2', 'jc3']),
justify_between: xs(['jb1', 'jb2', 'jb3']),
grid_cell1_rel_xy: [Math.round(ji1.left - p.left), Math.round(ji1.top - p.top)],
};
}""")
exp5 = {
"justify_start": [0, 50, 100],
"justify_center": [75, 125, 175],
"justify_between": [0, 125, 250],
"grid_cell1_rel_xy": [30, 40],
}
cases.append({
"name": "justify_and_align_distribution",
"passed": act5 == exp5,
"expected": exp5,
"actual": act5,
})
browser.close()
print(json.dumps({"cases": cases}, ensure_ascii=False))
if __name__ == "__main__":
main()
查看独立实例 ↗ 在独立页面查看源码、运行环境与工程包
本地运行
解压后按 README 说明在对应本地环境运行脚本。
本章附件与实例包
常见错误与边界检查
- 实际端口、文档资源与应用配置都应和测试条件一致。
技术规范与官方资料
练习与参考解答
查看参考解答
flex-1 编译为 flex: 1 1 0%,将 flex-basis 设为 0% 并允许增长,浏览器忽略 width 属性将剩余 444px 平均分配给两项;flex-initial 编译为 flex: 0 1 auto,flex-grow 为 0 且按 width 计算初始大小,空间充足时不增长也不收缩。