Tailwind CSS 布局类
系统核对 Tailwind CSS 12 组布局工具类(宽高比、容器、多栏、分页断行、box-border/box-content 盒模型、display、浮动清除、堆叠隔离与 object-fit)并附实测几何数据。
学完这一章,你能够
- 系统核对 Tailwind CSS 12 组布局工具类(宽高比、容器、多栏、分页断行、box-border/box-content 盒模型、display、浮动清除、堆叠隔离与 object-fit)并附实测几何数据。
宽高比、响应式容器与多列布局
Tailwind CSS 布局工具类覆盖从比例锁定到流式排版的全链路控制。宽高比工具类(`aspect-auto`、`aspect-square`、`aspect-video`)对应 CSS `aspect-ratio`,可在不拉伸图片或视频的前提下固定容器比例(注意编写 16:9 比例应使用 `aspect-video`,而非无效占位类 `aspect-ratio`)。本站离线 Chromium 实测:`200px` 宽的 `aspect-square` 计算高度精确为 `200px`,`320px` 宽的 `aspect-video` 计算高度精确为 `180px`。
`container` 类默认 `width: 100%`,并在命中各响应式断点(`sm: 640px`、`md: 768px`、`lg: 1024px`、`xl: 1280px`、`2xl: 1536px`)时将 `max-width` 锁定为当前断点宽度;常搭配 `mx-auto` 实现水平居中,或以 `lg:container` 仅在大屏生效。实测 `container mx-auto` 在 `500px` 视口下宽 `500px`,在 `800px` 视口下宽 `768px`,在 `1400px` 视口下宽 `1280px` 且左侧自动居中留白 `60px`。
多列布局通过 `columns-{n}`(1–12 列)或 `columns-{size}`(按理想列宽如 `columns-3xs` 至 `columns-5xl` 自动分栏)控制 CSS `columns` 属性,并可配合列间距(推荐 `gap-x-*`,示例亦展示了 `space-x-*`)。实测 `.columns-3` 计算 `columnCount` 为 `'3'`,`.columns-sm` 计算 `columnWidth` 为 `'384px'`。
分页分列控制与片段装饰断行
在打印分页或多栏排版场景中,`break-after-*`、`break-before-*` 与 `break-inside-*` 分别控制元素之后、之前及内部是否允许分列或分页(例如 `auto` 由浏览器自动判定、`avoid` 尽量避免断开、`column` 强制分列)。在 `columns-2` 两栏容器中为第二段添加 `break-after-column`,即可强制其后段落另起新列。实测 `break-after-column`、`break-before-page`、`break-inside-avoid` 在 Chromium 中的计算值分别为 `'column'`、`'page'` 与 `'avoid'`。
当行内元素跨越多行、多列或多页被拆分为多个片段时,`box-decoration-break` 决定背景渐变与边框的绘制策略:`box-decoration-clone` 让每个片段独立渲染完整边框、内边距与背景;`box-decoration-slice` 则将整个元素视为连续整体后再在断行处切割。本站实测两者的计算值分别为 `'clone'` 与 `'slice'`。
盒模型控制:box-border 与 box-content 实测勘误
Tailwind 通过 `box-border`(`box-sizing: border-box`)与 `box-content`(`box-sizing: content-box`)切换盒模型计算方式。使用 `box-border` 时,声明的宽高已包含内边距与边框:例如指定 `100px × 100px` 且带 `2px` 边框与 `4px` 内边距时,总渲染尺寸仍严格保持 `100px × 100px`(内部内容区收缩为 `88px × 88px`,部分中文资料误写作 112px,需特别甄别)。本站离线 Chromium 实测验证:`h-32 w-32 p-4 border-4`(`128px` + 四周 `16px` 内边距 + `4px` 边框)搭配 `box-border` 时,外框实测精确为 `128×128`(与私有留证示意图 `box-border` 标注一致);`100px` 案例实测亦为 `100×100`。
使用 `box-content` 时,声明的宽高仅指内容区,内边距与边框向外叠加。因此 `100px × 100px` 加 `2px` 边框与 `4px` 内边距的实际渲染尺寸为 `112px × 112px`;同理,`box-content h-32 w-32 p-4 border-4` 在 Chromium 中实测总渲染尺寸扩大至 `168×168`(`128 + 16×2 + 4×2`),直观印证了盒模型外扩差异。
显示类型 display、浮动清除、堆叠隔离与 object-fit
`display` 工具类涵盖块级与行内(`block`、`inline-block`、`inline`)、弹性与网格(`flex`、`inline-flex`、`grid`、`inline-grid`)、流根 BFC(`flow-root`)、移除自身盒子的 `contents`、`list-item`、隐藏元素 `hidden`(`display: none`),以及完整 CSS 表格显示家族(`table`、`table-header-group`、`table-row-group`、`table-row`、`table-cell` 等)。本站实测上述各类计算 `display` 均与规范一致,且 `table` + `table-cell` 组合使单元格自动水平对齐于同一行。
浮动与清除工具类同时提供物理方向(`float-left`/`right`/`none`、`clear-left`/`right`/`both`/`none`)和书写方向感知的逻辑属性(`float-start`/`end` 对应 `inline-start`/`inline-end`,`clear-start`/`end` 同理)。本站实测 `dir="rtl"` 容器中 `float-start` 计算值为 `'inline-start'` 且几何右边界紧贴父容器右缘;带有 `clear-left` 的段落顶部坐标严格位于左侧浮动块底部之下。
`isolate`(`isolation: isolate`)用于显式创建独立堆叠上下文,防止内部 `z-index` 与外部元素交叉叠压,而 `isolation-auto` 恢复默认堆叠规则。针对图片或视频等替换元素,`object-contain`、`object-cover`、`object-fill`、`object-none` 与 `object-scale-down` 分别映射 5 种 `object-fit` 缩放策略(如 `object-cover h-48 w-96` 保持比例铺满并裁剪溢出边)。本站实测 `isolate`/`isolation-auto` 及全部 5 种 `object-fit` 计算属性均精确生效。
宽高比工具类(Aspect Ratio)
| 类名 | CSS 属性及值 | 说明 |
|---|---|---|
| aspect-auto | aspect-ratio: auto; | 保持元素自然宽高比,由内容或显式尺寸决定 |
| aspect-square | aspect-ratio: 1 / 1; | 锁定 1:1 正方形比例(200px 宽实测高 200px) |
| aspect-video | aspect-ratio: 16 / 9; | 锁定 16:9 视频比例(320px 宽实测高 180px) |
响应式容器断点宽度(Container)
| 类名 | CSS 属性及值 | 说明 |
|---|---|---|
| container | width: 100%; | 默认占满父容器宽度,并随各断点自动施加对应 max-width |
| sm:container | max-width: 640px; | 屏幕宽度 >= 640px 时最大宽度 640px |
| md:container | max-width: 768px; | 屏幕宽度 >= 768px 时最大宽度 768px(800px 视口实测 768px) |
| lg:container | max-width: 1024px; | 屏幕宽度 >= 1024px 时最大宽度 1024px |
| xl:container | max-width: 1280px; | 屏幕宽度 >= 1280px 时最大宽度 1280px(1400px 视口实测 1280px) |
| 2xl:container | max-width: 1536px; | 屏幕宽度 >= 1536px 时最大宽度 1536px |
多列布局工具类(Columns)
| 类名 | CSS 属性及值 | 说明 |
|---|---|---|
| columns-1 | columns: 1; | 将内容划分为 1 列多栏排布 |
| columns-2 | columns: 2; | 将内容划分为 2 列多栏排布 |
| columns-3 | columns: 3; | 将内容划分为 3 列多栏排布 |
| columns-4 | columns: 4; | 将内容划分为 4 列多栏排布 |
| columns-5 | columns: 5; | 将内容划分为 5 列多栏排布 |
| columns-6 | columns: 6; | 将内容划分为 6 列多栏排布 |
| columns-7 | columns: 7; | 将内容划分为 7 列多栏排布 |
| columns-8 | columns: 8; | 将内容划分为 8 列多栏排布 |
| columns-9 | columns: 9; | 将内容划分为 9 列多栏排布 |
| columns-10 | columns: 10; | 将内容划分为 10 列多栏排布 |
| columns-11 | columns: 11; | 将内容划分为 11 列多栏排布 |
| columns-12 | columns: 12; | 将内容划分为 12 列多栏排布 |
| columns-auto | columns: auto; | 由浏览器根据内容自动决定列数与列宽 |
| columns-3xs | columns: 16rem; /* 256px */ | 按理想列宽 16rem(256px)自动分栏 |
| columns-2xs | columns: 18rem; /* 288px */ | 按理想列宽 18rem(288px)自动分栏 |
| columns-xs | columns: 20rem; /* 320px */ | 按理想列宽 20rem(320px)自动分栏 |
| columns-sm | columns: 24rem; /* 384px */ | 按理想列宽 24rem(384px)自动分栏 |
| columns-md | columns: 28rem; /* 448px */ | 按理想列宽 28rem(448px)自动分栏 |
| columns-lg | columns: 32rem; /* 512px */ | 按理想列宽 32rem(512px)自动分栏 |
| columns-xl | columns: 36rem; /* 576px */ | 按理想列宽 36rem(576px)自动分栏 |
| columns-2xl | columns: 42rem; /* 672px */ | 按理想列宽 42rem(672px)自动分栏 |
| columns-3xl | columns: 48rem; /* 768px */ | 按理想列宽 48rem(768px)自动分栏 |
| columns-4xl | columns: 56rem; /* 896px */ | 按理想列宽 56rem(896px)自动分栏 |
| columns-5xl | columns: 64rem; /* 1024px */ | 按理想列宽 64rem(1024px)自动分栏 |
元素后分页/分列控制(Break After)
| 类名 | CSS 属性及值 | 说明 |
|---|---|---|
| break-after-auto | break-after: auto; | 允许浏览器自然决定元素后是否分页或分列 |
| break-after-avoid | break-after: avoid; | 尽量避免在元素后分页或分列 |
| break-after-all | break-after: all; | 强制在元素后同时触发分页与分列 |
| break-after-avoid-page | break-after: avoid-page; | 尽量避免元素后分页,仍允许分列 |
| break-after-page | break-after: page; | 强制在元素后分页 |
| break-after-left | break-after: left; | 强制分页使后续内容从左侧页开始 |
| break-after-right | break-after: right; | 强制分页使后续内容从右侧页开始 |
| break-after-column | break-after: column; | 强制在元素后开启新列(实测计算值 column) |
元素前分页/分列控制(Break Before)
| 类名 | CSS 属性及值 | 说明 |
|---|---|---|
| break-before-auto | break-before: auto; | 允许浏览器自然决定元素前是否分页或分列 |
| break-before-avoid | break-before: avoid; | 尽量避免在元素前分页或分列 |
| break-before-all | break-before: all; | 强制在元素前触发分页或分列 |
| break-before-avoid-page | break-before: avoid-page; | 尽量避免元素前分页,仍允许分列 |
| break-before-page | break-before: page; | 强制在元素前分页(实测计算值 page) |
| break-before-left | break-before: left; | 强制分页使该元素从左侧页开始 |
| break-before-right | break-before: right; | 强制分页使该元素从右侧页开始 |
| break-before-column | break-before: column; | 强制在元素前开启新列 |
元素内部分页/分列控制(Break Inside)
| 类名 | CSS 属性及值 | 说明 |
|---|---|---|
| break-inside-auto | break-inside: auto; | 允许在元素内部自然分页或分列 |
| break-inside-avoid | break-inside: avoid; | 尽量避免在元素内部断开(实测计算值 avoid) |
| break-inside-avoid-page | break-inside: avoid-page; | 尽量避免在元素内部分页,仍允许分列 |
| break-inside-avoid-column | break-inside: avoid-column; | 尽量避免在元素内部分列,仍允许分页 |
断行片段装饰控制(Box Decoration Break)
| 类名 | CSS 属性及值 | 说明 |
|---|---|---|
| box-decoration-clone | box-decoration-break: clone; | 每个跨行/跨列片段独立渲染完整边框与背景 |
| box-decoration-slice | box-decoration-break: slice; | 将装饰按连续整体绘制并在断行处切割 |
显示类型工具类(Display)
| Tailwind 类名 | CSS 属性及值 | 说明 |
|---|---|---|
| block | display: block; | 块级元素,独占一行 |
| inline-block | display: inline-block; | 行内块级元素,行内排布且支持显式宽高 |
| inline | display: inline; | 行内元素,随文本流排布并自然折行 |
| flex | display: flex; | 块级弹性布局容器 |
| inline-flex | display: inline-flex; | 行内级弹性布局容器 |
| table | display: table; | 块级表格容器 |
| inline-table | display: inline-table; | 行内级表格容器 |
| table-caption | display: table-caption; | 表格标题元素 |
| table-cell | display: table-cell; | 表格单元格 |
| table-column | display: table-column; | 表格列定义元素 |
| table-column-group | display: table-column-group; | 表格列组元素 |
| table-footer-group | display: table-footer-group; | 表格页脚组(等效 tfoot) |
| table-header-group | display: table-header-group; | 表格表头组(等效 thead) |
| table-row-group | display: table-row-group; | 表格表体行组(等效 tbody) |
| table-row | display: table-row; | 表格行(等效 tr) |
| flow-root | display: flow-root; | 建立独立块级格式化上下文(BFC) |
| grid | display: grid; | 块级 CSS Grid 网格容器 |
| inline-grid | display: inline-grid; | 行内级 CSS Grid 网格容器 |
| contents | display: contents; | 不生成自身盒子,由子元素直接参与父布局 |
| list-item | display: list-item; | 按列表项盒子渲染 |
| hidden | display: none; | 从渲染树与文档流中隐藏元素 |
浮动工具类(Float)
| Tailwind 类名 | CSS 属性及值 | 说明 |
|---|---|---|
| float-start | float: inline-start; | 向行内起始侧浮动(LTR 靠左、RTL 靠右) |
| float-end | float: inline-end; | 向行内结束侧浮动(LTR 靠右、RTL 靠左) |
| float-right | float: right; | 向包含块物理右侧浮动 |
| float-left | float: left; | 向包含块物理左侧浮动 |
| float-none | float: none; | 取消浮动,保持正常文档流 |
清除浮动工具类(Clear)
| 类名 | CSS 属性及值 | 说明 |
|---|---|---|
| clear-start | clear: inline-start; | 清除行内起始侧的浮动影响 |
| clear-end | clear: inline-end; | 清除行内结束侧的浮动影响 |
| clear-left | clear: left; | 下移至左侧浮动元素下方 |
| clear-right | clear: right; | 下移至右侧浮动元素下方 |
| clear-both | clear: both; | 同时清除左右两侧浮动影响 |
| clear-none | clear: none; | 不清除浮动,允许沿浮动元素排布 |
堆叠上下文隔离(Isolation)
| Tailwind 类名 | CSS 属性及值 | 说明 |
|---|---|---|
| isolate | isolation: isolate; | 显式创建新的独立堆叠上下文,隔离后代 z-index |
| isolation-auto | isolation: auto; | 按浏览器默认规则决定是否建立堆叠上下文 |
替换元素内容缩放(Object Fit)
| 类名 | CSS 属性及值 | 说明 |
|---|---|---|
| object-contain | object-fit: contain; | 保持纵横比完整显示在容器内,可能留白 |
| object-cover | object-fit: cover; | 保持纵横比铺满容器,超出部分裁剪 |
| object-fill | object-fit: fill; | 忽略原纵横比拉伸填满整个容器 |
| object-none | object-fit: none; | 维持替换元素固有尺寸不缩放 |
| object-scale-down | object-fit: scale-down; | 在 none 与 contain 中取较小尺寸显示 |
离线 Chromium 验证盒模型 128px/168px、宽高比、容器断点、多栏、display 与浮动/object-fit
"""Offline Playwright Chromium lab for Tailwind CSS Ch 15: Layout."""
import json
from playwright.sync_api import sync_playwright
CSS = """
body { margin: 0; }
.box-border { box-sizing: border-box; }
.box-content { box-sizing: content-box; }
.w-32 { width: 8rem; }
.h-32 { height: 8rem; }
.p-4 { padding: 1rem; }
.border-4 { border-width: 4px; border-style: solid; border-color: #334155; }
.aspect-auto { aspect-ratio: auto; }
.aspect-square { aspect-ratio: 1 / 1; }
.aspect-video { aspect-ratio: 16 / 9; }
.container { width: 100%; }
@media (min-width: 640px) { .container { max-width: 640px; } }
@media (min-width: 768px) { .container { max-width: 768px; } }
@media (min-width: 1024px) { .container { max-width: 1024px; } }
@media (min-width: 1280px) { .container { max-width: 1280px; } }
.mx-auto { margin-left: auto; margin-right: auto; }
.columns-3 { columns: 3; }
.columns-sm { columns: 24rem; }
.break-after-column { break-after: column; }
.break-before-page { break-before: page; }
.break-inside-avoid { break-inside: avoid; }
.box-decoration-clone { -webkit-box-decoration-break: clone; box-decoration-break: clone; }
.box-decoration-slice { -webkit-box-decoration-break: slice; box-decoration-break: slice; }
.block { display: block; }
.inline-block { display: inline-block; }
.inline { display: inline; }
.flex { display: flex; }
.grid { display: grid; }
.flow-root { display: flow-root; }
.contents { display: contents; }
.list-item { display: list-item; }
.hidden { display: none; }
.table { display: table; width: 100%; }
.table-header-group { display: table-header-group; }
.table-row-group { display: table-row-group; }
.table-row { display: table-row; }
.table-cell { display: table-cell; }
.float-left { float: left; }
.float-right { float: right; }
.float-start { float: inline-start; }
.float-end { float: inline-end; }
.float-none { float: none; }
.clear-left { clear: left; }
.clear-both { clear: both; }
.isolate { isolation: isolate; }
.isolation-auto { isolation: auto; }
.object-contain { object-fit: contain; }
.object-cover { object-fit: cover; }
.object-fill { object-fit: fill; }
.object-none { object-fit: none; }
.object-scale-down { object-fit: scale-down; }
"""
SVG_DATA = "data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='400' height='200'><rect width='400' height='200' fill='%233b82f6'/></svg>"
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": 1400, "height": 800})
page.set_content(f"""
<!doctype html><html><head><style>{CSS}</style></head>
<body>
<div id="bb" class="box-border h-32 w-32 p-4 border-4">Border</div>
<div id="bc" class="box-content h-32 w-32 p-4 border-4">Content</div>
<div id="bb100" class="box-border" style="width:100px;height:100px;padding:4px;border:2px solid #000">100</div>
<div id="bc100" class="box-content" style="width:100px;height:100px;padding:4px;border:2px solid #000">100</div>
<div id="sq" class="aspect-square" style="width:200px">Sq</div>
<div id="vid" class="aspect-video" style="width:320px">Vid</div>
<div id="cont" class="container mx-auto">Container</div>
<div id="col3" class="columns-3"><p id="ba" class="break-after-column">A</p><p id="bbp" class="break-before-page">B</p><p id="bi" class="break-inside-avoid">C</p></div>
<div id="colsm" class="columns-sm">ColSM</div>
<span id="bdc" class="box-decoration-clone">Hello<br>World</span>
<span id="bds" class="box-decoration-slice">Hello<br>World</span>
<span id="d-block" class="block">B</span>
<span id="d-ib" class="inline-block">IB</span>
<span id="d-in" class="inline">IN</span>
<div id="d-fr" class="flow-root">FR</div>
<div id="d-ct" class="contents"><span>CT</span></div>
<div id="d-hd" class="hidden">HD</div>
<div id="d-tbl" class="table">
<div class="table-header-group"><div class="table-row"><div id="tc1" class="table-cell">Song</div><div id="tc2" class="table-cell">Artist</div></div></div>
</div>
<article style="width:400px">
<div id="fl" class="float-left" style="width:80px;height:60px">L</div>
<div id="fr" class="float-right" style="width:80px;height:40px">R</div>
<p id="clr" class="clear-left" style="margin:0">Cleared</p>
</article>
<div id="rtl-wrap" dir="rtl" style="width:200px"><div id="fl-rtl" class="float-start" style="width:60px">RTL</div></div>
<div id="iso1" class="isolate">Iso</div>
<div id="iso2" class="isolation-auto">Auto</div>
<img id="o-cover" class="object-cover" src="{SVG_DATA}" style="width:384px;height:192px" />
<img id="o-contain" class="object-contain" src="{SVG_DATA}" style="width:384px;height:192px" />
<img id="o-fill" class="object-fill" src="{SVG_DATA}" style="width:384px;height:192px" />
<img id="o-none" class="object-none" src="{SVG_DATA}" style="width:384px;height:192px" />
<img id="o-sd" class="object-scale-down" src="{SVG_DATA}" style="width:384px;height:192px" />
</body></html>
""")
# Case 1: box-border vs box-content exact geometry
act1 = page.evaluate("""() => {
const rect = id => {
const r = document.getElementById(id).getBoundingClientRect();
return [Math.round(r.width), Math.round(r.height)];
};
return {
box_border_32: rect('bb'),
box_content_32: rect('bc'),
box_border_100: rect('bb100'),
box_content_100: rect('bc100'),
};
}""")
exp1 = {
"box_border_32": [128, 128],
"box_content_32": [168, 168],
"box_border_100": [100, 100],
"box_content_100": [112, 112],
}
cases.append({
"name": "box_border_vs_box_content_geometry",
"passed": act1 == exp1,
"expected": exp1,
"actual": act1,
})
# Case 2: aspect-ratio & responsive container
cont_1400 = page.evaluate("""() => {
const r = document.getElementById('cont').getBoundingClientRect();
const sq = document.getElementById('sq').getBoundingClientRect();
const vid = document.getElementById('vid').getBoundingClientRect();
return {
square: [Math.round(sq.width), Math.round(sq.height)],
video: [Math.round(vid.width), Math.round(vid.height)],
cont_xl_width: Math.round(r.width),
cont_xl_left: Math.round(r.left),
};
}""")
page.set_viewport_size({"width": 800, "height": 800})
cont_800 = page.evaluate("() => Math.round(document.getElementById('cont').getBoundingClientRect().width)")
page.set_viewport_size({"width": 500, "height": 800})
cont_500 = page.evaluate("() => Math.round(document.getElementById('cont').getBoundingClientRect().width)")
act2 = {**cont_1400, "cont_md_width": cont_800, "cont_mobile_width": cont_500}
exp2 = {
"square": [200, 200],
"video": [320, 180],
"cont_xl_width": 1280,
"cont_xl_left": 60,
"cont_md_width": 768,
"cont_mobile_width": 500,
}
cases.append({
"name": "aspect_ratio_and_responsive_container",
"passed": act2 == exp2,
"expected": exp2,
"actual": act2,
})
# Case 3: columns, break-* and box-decoration-break
act3 = page.evaluate("""() => {
const g = id => getComputedStyle(document.getElementById(id));
return {
col3_count: g('col3').columnCount,
colsm_width: g('colsm').columnWidth,
break_after: g('ba').breakAfter,
break_before: g('bbp').breakBefore,
break_inside: g('bi').breakInside,
dec_clone: g('bdc').webkitBoxDecorationBreak || g('bdc').boxDecorationBreak,
dec_slice: g('bds').webkitBoxDecorationBreak || g('bds').boxDecorationBreak,
};
}""")
exp3 = {
"col3_count": "3",
"colsm_width": "384px",
"break_after": "column",
"break_before": "page",
"break_inside": "avoid",
"dec_clone": "clone",
"dec_slice": "slice",
}
cases.append({
"name": "columns_break_and_box_decoration",
"passed": act3 == exp3,
"expected": exp3,
"actual": act3,
})
# Case 4: display utilities including table-* and hidden
act4 = page.evaluate("""() => {
const d = id => getComputedStyle(document.getElementById(id)).display;
const c1 = document.getElementById('tc1').getBoundingClientRect();
const c2 = document.getElementById('tc2').getBoundingClientRect();
return {
block: d('d-block'),
inline_block: d('d-ib'),
inline: d('d-in'),
flow_root: d('d-fr'),
contents: d('d-ct'),
hidden: d('d-hd'),
table: d('d-tbl'),
cells_same_row: Math.round(c1.top) === Math.round(c2.top) && c2.left > c1.left,
};
}""")
exp4 = {
"block": "block",
"inline_block": "inline-block",
"inline": "inline",
"flow_root": "flow-root",
"contents": "contents",
"hidden": "none",
"table": "table",
"cells_same_row": True,
}
cases.append({
"name": "display_utilities_and_table_cells",
"passed": act4 == exp4,
"expected": exp4,
"actual": act4,
})
# Case 5: float, clear, isolation, and object-fit
act5 = page.evaluate("""() => {
const g = id => getComputedStyle(document.getElementById(id));
const flR = document.getElementById('fl').getBoundingClientRect();
const clrR = document.getElementById('clr').getBoundingClientRect();
const rtlWrap = document.getElementById('rtl-wrap').getBoundingClientRect();
const rtlEl = document.getElementById('fl-rtl').getBoundingClientRect();
return {
float_left: g('fl').cssFloat,
float_right: g('fr').cssFloat,
float_start_computed: g('fl-rtl').cssFloat,
float_start_rtl_at_right_edge: Math.round(rtlEl.right) === Math.round(rtlWrap.right),
cleared_below_left: Math.round(clrR.top) >= Math.round(flR.bottom),
isolate: g('iso1').isolation,
isolation_auto: g('iso2').isolation,
object_fit: [
g('o-contain').objectFit,
g('o-cover').objectFit,
g('o-fill').objectFit,
g('o-none').objectFit,
g('o-sd').objectFit,
],
};
}""")
exp5 = {
"float_left": "left",
"float_right": "right",
"float_start_computed": "inline-start",
"float_start_rtl_at_right_edge": True,
"cleared_below_left": True,
"isolate": "isolate",
"isolation_auto": "auto",
"object_fit": ["contain", "cover", "fill", "none", "scale-down"],
}
cases.append({
"name": "float_clear_isolate_and_object_fit",
"passed": act5 == exp5,
"expected": exp5,
"actual": act5,
})
browser.close()
print(json.dumps({"cases": cases}, ensure_ascii=False))
if __name__ == "__main__":
main()
- 核对 box-border(128×128, 100×100) 与 box-content(168×168, 112×112)、aspect-video(320×180)、container 断点居中、columns/break/box-decoration、display 表格流及 RTL float-start/clear/isolate/object-fit。
所需环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
当前源码 SHA256:0d286361567405f38d67b5236cea3027a8eaa9d2c6ff8ff251ef48eaad3feb0a
本地运行
解压后按 README 说明在对应本地环境运行脚本。
所需环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
ZIP SHA256:db4da953cd904424902591e937eeda2551b5a4e82212eb8300b49eae174251ba
常见错误与边界
- 实际端口、文档资源与应用配置都应和测试条件一致。
练习与解答
同一元素设置 h-32 w-32 p-4 border-4 时,box-border 与 box-content 渲染出的外框尺寸分别是多少?
展开参考解答
box-border(border-box)把内边距和边框计入声明尺寸,总渲染尺寸为 128px × 128px;box-content(content-box)把内边距(两侧共 32px)与边框(两侧共 8px)叠加到内容区之外,总渲染尺寸为 168px × 168px。