"""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()
本例需在 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
入口文件:projects/tailwindcss-layout-lab/explore-layout.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-layout.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a651.zip
课程关联工程包(登记关联24章) · 含当前章节或实例 · mbskycom202612a639.zip