"""Offline Playwright Chromium lab for Tailwind CSS Ch 14: Functions and Directives."""
import json
from playwright.sync_api import sync_playwright
CSS = """
@layer base, components, utilities;
@layer base {
*, ::before, ::after { box-sizing: border-box; }
h1 { font-size: inherit; font-weight: inherit; margin: 0; }
}
@layer components {
.btn { padding: 0.5rem 1rem; background-color: #3b82f6; color: #ffffff; border-radius: 0.5rem; }
.btn-warning { padding: 0.5rem 1rem; background-color: #eab308; color: #000000; border-radius: 0.5rem; }
.resp-btn { padding: 0.5rem 1rem; background-color: #3b82f6; color: #ffffff; border-radius: 0.375rem; }
@media (min-width: 768px) {
.resp-btn:hover { background-color: #48bb78; }
}
}
@layer utilities {
.no-scrollbar { overflow: hidden; }
.bg-slate-900 { background-color: #0f172a; }
.calc-box { width: calc(100% - 20px); }
.min-box { width: min(10px, 5vw); }
.max-box { width: max(10px, 5vw); }
.clamp-text { font-size: clamp(1rem, 5vw, 3rem); }
.theme-blue { background-color: #3b82f6; }
.theme-green { background-color: #10b981; }
.theme-third { width: 33.333333%; }
.rgba-box { background-color: rgba(52, 144, 220, 0.8); }
.unit-rem { font-size: 1rem; }
.unit-em { padding-left: 1em; }
.unit-px { margin-top: 16px; }
.unit-rad { border-radius: 8px; }
}
"""
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": 1000, "height": 500})
page.set_content(f"""
<!doctype html><html><head><style>{CSS}</style></head>
<body style="margin:0">
<h1 id="reset-h1">Heading</h1>
<button id="btn" class="btn">Btn</button>
<button id="btn-warn" class="btn-warning">Warn</button>
<button id="btn-ov" class="btn bg-slate-900">Override</button>
<div id="noscroll" class="no-scrollbar" style="width:100px;height:40px">Scroll</div>
<div style="width:400px"><div id="calc" class="calc-box">Calc</div></div>
<div id="minb" class="min-box">Min</div>
<div id="maxb" class="max-box">Max</div>
<p id="clampt" class="clamp-text">Clamp</p>
<div id="tblue" class="theme-blue">Blue</div>
<div id="tgreen" class="theme-green">Green</div>
<div style="width:300px"><div id="tthird" class="theme-third">Third</div></div>
<button id="rbtn" class="resp-btn">RespBtn</button>
<div id="rgbab" class="rgba-box unit-rad">RGBA</div>
<div style="font-size:20px"><div id="uem" class="unit-em unit-px">EM</div></div>
<div id="urem" class="unit-rem">REM</div>
</body></html>
""")
# Case 1: @tailwind layers & @apply compiled cascade
act1 = page.evaluate("""() => ({
h1_size: getComputedStyle(document.getElementById('reset-h1')).fontSize,
btn_bg: getComputedStyle(document.getElementById('btn')).backgroundColor,
warn_bg: getComputedStyle(document.getElementById('btn-warn')).backgroundColor,
override_bg: getComputedStyle(document.getElementById('btn-ov')).backgroundColor,
overflow: getComputedStyle(document.getElementById('noscroll')).overflow,
})""")
exp1 = {
"h1_size": "16px",
"btn_bg": "rgb(59, 130, 246)",
"warn_bg": "rgb(234, 179, 8)",
"override_bg": "rgb(15, 23, 42)",
"overflow": "hidden",
}
cases.append({
"name": "tailwind_layers_and_apply_cascade",
"passed": act1 == exp1,
"expected": exp1,
"actual": act1,
})
# Case 2: Native CSS calc(), min(), max(), clamp() across viewports
v1000 = page.evaluate("""() => ({
calc_w: Math.round(document.getElementById('calc').getBoundingClientRect().width),
min_w: Math.round(document.getElementById('minb').getBoundingClientRect().width),
max_w: Math.round(document.getElementById('maxb').getBoundingClientRect().width),
clamp_1000: getComputedStyle(document.getElementById('clampt')).fontSize,
})""")
page.set_viewport_size({"width": 640, "height": 500})
clamp_640 = page.evaluate("() => getComputedStyle(document.getElementById('clampt')).fontSize")
page.set_viewport_size({"width": 300, "height": 500})
clamp_300 = page.evaluate("() => getComputedStyle(document.getElementById('clampt')).fontSize")
act2 = {**v1000, "clamp_640": clamp_640, "clamp_300": clamp_300}
exp2 = {
"calc_w": 380,
"min_w": 10,
"max_w": 50,
"clamp_1000": "48px",
"clamp_640": "32px",
"clamp_300": "16px",
}
cases.append({
"name": "native_css_math_functions",
"passed": act2 == exp2,
"expected": exp2,
"actual": act2,
})
# Case 3: Compiled theme() tokens & screen('md') hover breakpoint
page.set_viewport_size({"width": 600, "height": 500})
page.hover("#rbtn")
hover_sm = page.evaluate("() => getComputedStyle(document.getElementById('rbtn')).backgroundColor")
page.set_viewport_size({"width": 800, "height": 500})
page.hover("#rbtn")
hover_md = page.evaluate("() => getComputedStyle(document.getElementById('rbtn')).backgroundColor")
theme_vals = page.evaluate("""() => ({
blue_500: getComputedStyle(document.getElementById('tblue')).backgroundColor,
green_500: getComputedStyle(document.getElementById('tgreen')).backgroundColor,
third_px: Math.round(document.getElementById('tthird').getBoundingClientRect().width),
})""")
act3 = {**theme_vals, "hover_below_md": hover_sm, "hover_at_md": hover_md}
exp3 = {
"blue_500": "rgb(59, 130, 246)",
"green_500": "rgb(16, 185, 129)",
"third_px": 100,
"hover_below_md": "rgb(59, 130, 246)",
"hover_at_md": "rgb(72, 187, 120)",
}
cases.append({
"name": "compiled_theme_and_screen_outputs",
"passed": act3 == exp3,
"expected": exp3,
"actual": act3,
})
# Case 4: Compiled unit conversions & RGBA transparency
act4 = page.evaluate("""() => ({
rgba_bg: getComputedStyle(document.getElementById('rgbab')).backgroundColor,
radius: getComputedStyle(document.getElementById('rgbab')).borderRadius,
rem_1: getComputedStyle(document.getElementById('urem')).fontSize,
em_1_in_20px: getComputedStyle(document.getElementById('uem')).paddingLeft,
px_16: getComputedStyle(document.getElementById('uem')).marginTop,
})""")
exp4 = {
"rgba_bg": "rgba(52, 144, 220, 0.8)",
"radius": "8px",
"rem_1": "16px",
"em_1_in_20px": "20px",
"px_16": "16px",
}
cases.append({
"name": "compiled_units_and_rgba_values",
"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-functions-and-directives-lab/explore-functions-and-directives.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-functions-and-directives.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a647.zip
课程关联工程包(登记关联24章) · 含当前章节或实例 · mbskycom202612a639.zip