"""Offline Playwright Chromium lab for Tailwind CSS Ch 18: Sizing."""
import json
from playwright.sync_api import sync_playwright
CSS = """
*, ::before, ::after { box-sizing: border-box; }
body { margin: 0; }
.flex { display: flex; }
.w-px { width: 1px; }
.w-24 { width: 6rem; }
.w-32 { width: 8rem; }
.w-64 { width: 16rem; }
.w-96 { width: 24rem; }
.w-1\\/2 { width: 50%; }
.w-1\\/4 { width: 25%; }
.w-2\\/5 { width: 40%; }
.w-3\\/5 { width: 60%; }
.w-full { width: 100%; }
.w-screen { width: 100vw; }
.w-auto { width: auto; }
.h-16 { height: 4rem; }
.h-24 { height: 6rem; }
.h-48 { height: 12rem; }
.h-56 { height: 14rem; }
.h-96 { height: 24rem; }
.h-1\\/2 { height: 50%; }
.h-full { height: 100%; }
.h-screen { height: 100vh; }
.h-dvh { height: 100dvh; }
.h-lvh { height: 100lvh; }
.h-svh { height: 100svh; }
.h-auto { height: auto; }
.size-0 { width: 0px; height: 0px; }
.size-4 { width: 1rem; height: 1rem; }
.size-16 { width: 4rem; height: 4rem; }
.size-32 { width: 8rem; height: 8rem; }
.size-64 { width: 16rem; height: 16rem; }
.size-full { width: 100%; height: 100%; }
.size-auto { width: auto; height: auto; }
.min-w-0 { min-width: 0px; }
.min-w-full { min-width: 100%; }
.min-w-max { min-width: max-content; }
.min-h-0 { min-height: 0px; }
.min-h-full { min-height: 100%; }
.min-h-screen { min-height: 100vh; }
.max-w-xs { max-width: 20rem; }
.max-w-sm { max-width: 24rem; }
.max-w-full { max-width: 100%; }
.max-h-32 { max-height: 8rem; }
.max-h-screen { max-height: 100vh; }
@media (min-width: 768px) {
.md\\:size-32 { width: 8rem; height: 8rem; }
.md\\:w-auto { width: auto; }
}
@media (min-width: 1024px) {
.lg\\:size-64 { width: 16rem; height: 16rem; }
}
"""
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": 800, "height": 600})
page.set_content(f"""
<!doctype html><html><head><style>{CSS}</style></head>
<body>
<div id="w-px" class="w-px"></div>
<div id="w-24" class="w-24"></div>
<div id="w-64" class="w-64"></div>
<div id="w-96" class="w-96"></div>
<div id="w-scr" class="w-screen"></div>
<div class="flex" style="width:600px">
<div id="w-half" class="w-1/2">1/2</div>
<div id="w-qtr" class="w-1/4">1/4</div>
</div>
<div class="flex" style="width:600px">
<div id="w-25" class="w-2/5">2/5</div>
<div id="w-35" class="w-3/5">3/5</div>
</div>
<div id="h-16" class="h-16"></div>
<div class="h-48"><div id="h-full" class="h-full"></div></div>
<div class="h-48"><div id="h-half" class="h-1/2"></div></div>
<div id="h-scr" class="h-screen"></div>
<div id="h-dvh" class="h-dvh"></div>
<div id="h-lvh" class="h-lvh"></div>
<div id="h-svh" class="h-svh"></div>
<div id="s0" class="size-0"></div>
<div id="s4" class="size-4"></div>
<div id="s16" class="size-16"></div>
<div style="width:224px;height:224px"><div id="sfull" class="size-full"></div></div>
<div id="sresp" class="size-16 md:size-32 lg:size-64"></div>
<div style="width:400px"><div id="minwf" class="min-w-full" style="width:50px">A</div></div>
<div style="width:600px"><div id="maxwxs" class="max-w-xs w-full">XS</div><div id="maxwsm" class="max-w-sm w-full">SM</div></div>
<div id="minhscr" class="min-h-screen">MinH</div>
<div id="maxh32" class="max-h-32" style="height:300px">MaxH</div>
</body></html>
""")
# Case 1: Width utilities (fixed, fraction, viewport)
act1 = page.evaluate("""() => {
const w = id => Math.round(document.getElementById(id).getBoundingClientRect().width);
return {
w_px: w('w-px'),
w_24: w('w-24'),
w_64: w('w-64'),
w_96: w('w-96'),
w_screen: w('w-scr'),
w_fractions: [w('w-half'), w('w-qtr'), w('w-25'), w('w-35')],
};
}""")
exp1 = {
"w_px": 1,
"w_24": 96,
"w_64": 256,
"w_96": 384,
"w_screen": 800,
"w_fractions": [300, 150, 240, 360],
}
cases.append({
"name": "width_fixed_fraction_and_viewport",
"passed": act1 == exp1,
"expected": exp1,
"actual": act1,
})
# Case 2: Height utilities (fixed, parent percentage, vh/dvh/lvh/svh)
act2 = page.evaluate("""() => {
const h = id => Math.round(document.getElementById(id).getBoundingClientRect().height);
return {
h_16: h('h-16'),
h_full_in_48: h('h-full'),
h_half_in_48: h('h-half'),
viewport_heights: [h('h-scr'), h('h-dvh'), h('h-lvh'), h('h-svh')],
};
}""")
exp2 = {
"h_16": 64,
"h_full_in_48": 192,
"h_half_in_48": 96,
"viewport_heights": [600, 600, 600, 600],
}
cases.append({
"name": "height_fixed_parent_and_viewport_units",
"passed": act2 == exp2,
"expected": exp2,
"actual": act2,
})
# Case 3: size-* utilities and responsive breakpoints
page.set_viewport_size({"width": 500, "height": 600})
sz_500 = page.evaluate("""() => {
const wh = id => {
const r = document.getElementById(id).getBoundingClientRect();
return [Math.round(r.width), Math.round(r.height)];
};
return {s0: wh('s0'), s4: wh('s4'), s16: wh('s16'), sfull: wh('sfull'), resp_sm: wh('sresp')};
}""")
page.set_viewport_size({"width": 800, "height": 600})
sz_800 = page.evaluate("""() => {
const r = document.getElementById('sresp').getBoundingClientRect();
return [Math.round(r.width), Math.round(r.height)];
}""")
page.set_viewport_size({"width": 1100, "height": 600})
sz_1100 = page.evaluate("""() => {
const r = document.getElementById('sresp').getBoundingClientRect();
return [Math.round(r.width), Math.round(r.height)];
}""")
act3 = {**sz_500, "resp_md": sz_800, "resp_lg": sz_1100}
exp3 = {
"s0": [0, 0],
"s4": [16, 16],
"s16": [64, 64],
"sfull": [224, 224],
"resp_sm": [64, 64],
"resp_md": [128, 128],
"resp_lg": [256, 256],
}
cases.append({
"name": "size_utilities_and_responsive_scaling",
"passed": act3 == exp3,
"expected": exp3,
"actual": act3,
})
# Case 4: min-w, max-w, min-h, max-h constraints
page.set_viewport_size({"width": 800, "height": 600})
act4 = page.evaluate("""() => {
const r = id => document.getElementById(id).getBoundingClientRect();
return {
min_w_full: Math.round(r('minwf').width),
max_w_xs: Math.round(r('maxwxs').width),
max_w_sm: Math.round(r('maxwsm').width),
min_h_screen: Math.round(r('minhscr').height),
max_h_32: Math.round(r('maxh32').height),
};
}""")
exp4 = {
"min_w_full": 400,
"max_w_xs": 320,
"max_w_sm": 384,
"min_h_screen": 600,
"max_h_32": 128,
}
cases.append({
"name": "min_and_max_sizing_constraints",
"passed": act4 == exp4,
"expected": exp4,
"actual": act4,
})
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-sizing-lab/explore-sizing.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-sizing.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a655.zip
课程关联工程包(登记关联24章) · 含当前章节或实例 · mbskycom202612a639.zip