"""Offline Playwright Chromium lab for Tailwind CSS Ch 20: Typography."""
import json
from playwright.sync_api import sync_playwright
CSS = """
body { margin: 0; font-size: 16px; }
.font-sans { font-family: ui-sans-serif, system-ui, sans-serif; }
.font-serif { font-family: ui-serif, Georgia, serif; }
.font-mono { font-family: ui-monospace, SFMono-Regular, monospace; }
.text-xs { font-size: 0.75rem; line-height: 1rem; }
.text-sm { font-size: 0.875rem; line-height: 1.25rem; }
.text-base { font-size: 1rem; line-height: 1.5rem; }
.text-lg { font-size: 1.125rem; line-height: 1.75rem; }
.text-xl { font-size: 1.25rem; line-height: 1.75rem; }
.text-2xl { font-size: 1.5rem; line-height: 2rem; }
.font-thin { font-weight: 100; }
.font-light { font-weight: 300; }
.font-normal { font-weight: 400; }
.font-medium { font-weight: 500; }
.font-semibold { font-weight: 600; }
.font-bold { font-weight: 700; }
.font-extrabold { font-weight: 800; }
.font-black { font-weight: 900; }
.leading-none { line-height: 1; }
.leading-tight { line-height: 1.25; }
.leading-snug { line-height: 1.375; }
.leading-normal { line-height: 1.5; }
.leading-relaxed { line-height: 1.625; }
.leading-loose { line-height: 2; }
.tracking-tight { letter-spacing: -0.025em; }
.tracking-normal { letter-spacing: 0em; }
.tracking-wide { letter-spacing: 0.025em; }
.tracking-wider { letter-spacing: 0.05em; }
.tracking-widest { letter-spacing: 0.1em; }
.text-sky-400\\/100 { color: rgb(56 189 248 / 1); }
.text-sky-400\\/50 { color: rgb(56 189 248 / 0.5); }
.text-sky-400\\/0 { color: rgb(56 189 248 / 0); }
.text-left { text-align: left; }
.text-center { text-align: center; }
.text-right { text-align: right; }
.underline { text-decoration-line: underline; }
.overline { text-decoration-line: overline; }
.line-through { text-decoration-line: line-through; }
.no-underline { text-decoration-line: none; }
.normal-case { text-transform: none; }
.uppercase { text-transform: uppercase; }
.lowercase { text-transform: lowercase; }
.capitalize { text-transform: capitalize; }
.text-shadow { text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.25); }
@media (min-width: 640px) { .sm\\:text-base { font-size: 1rem; line-height: 1.5rem; } }
@media (min-width: 768px) { .md\\:text-lg { font-size: 1.125rem; line-height: 1.75rem; } }
@media (min-width: 1024px) { .lg\\:text-xl { font-size: 1.25rem; line-height: 1.75rem; } }
@media (min-width: 1280px) { .xl\\:text-2xl { font-size: 1.5rem; line-height: 2rem; } }
"""
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": 400, "height": 500})
page.set_content(f"""
<!doctype html><html><head><style>{CSS}</style></head>
<body>
<p id="ff-sans" class="font-sans">Sans</p>
<p id="ff-serif" class="font-serif">Serif</p>
<p id="ff-mono" class="font-mono">Mono</p>
<p id="sz-xs" class="text-xs">xs</p>
<p id="sz-base" class="text-base">base</p>
<p id="sz-2xl" class="text-2xl">2xl</p>
<p id="fw-thin" class="font-thin">100</p>
<p id="fw-normal" class="font-normal">400</p>
<p id="fw-semi" class="font-semibold">600</p>
<p id="fw-bold" class="font-bold">700</p>
<p id="fw-black" class="font-black">900</p>
<p id="lh-none" class="leading-none">L</p>
<p id="lh-tight" class="leading-tight">L</p>
<p id="lh-snug" class="leading-snug">L</p>
<p id="lh-normal" class="leading-normal">L</p>
<p id="lh-relaxed" class="leading-relaxed">L</p>
<p id="lh-loose" class="leading-loose">L</p>
<p id="tr-tight" class="tracking-tight">T</p>
<p id="tr-wide" class="tracking-wide">T</p>
<p id="tr-wider" class="tracking-wider">T</p>
<p id="tr-widest" class="tracking-widest">T</p>
<p id="c-100" class="text-sky-400/100">C</p>
<p id="c-50" class="text-sky-400/50">C</p>
<p id="c-0" class="text-sky-400/0">C</p>
<p id="al-l" class="text-left">L</p>
<p id="al-c" class="text-center">C</p>
<p id="al-r" class="text-right">R</p>
<p id="dc-u" class="underline">U</p>
<p id="dc-o" class="overline">O</p>
<p id="dc-lt" class="line-through">LT</p>
<p id="dc-n" class="no-underline">N</p>
<p id="tf-u" class="uppercase">Up</p>
<p id="tf-l" class="lowercase">Lo</p>
<p id="tf-c" class="capitalize">Cap</p>
<p id="shd" class="text-shadow">Shadow</p>
<p id="resp" class="text-sm sm:text-base md:text-lg lg:text-xl xl:text-2xl">Resp</p>
</body></html>
""")
# Case 1: font-family, font-size, font-weight
act1 = page.evaluate("""() => {
const g = id => getComputedStyle(document.getElementById(id));
return {
families_ok: g('ff-sans').fontFamily.includes('sans-serif')
&& g('ff-serif').fontFamily.includes('serif')
&& g('ff-mono').fontFamily.includes('monospace'),
sizes: [g('sz-xs').fontSize, g('sz-base').fontSize, g('sz-2xl').fontSize],
weights: [g('fw-thin').fontWeight, g('fw-normal').fontWeight, g('fw-semi').fontWeight, g('fw-bold').fontWeight, g('fw-black').fontWeight],
};
}""")
exp1 = {
"families_ok": True,
"sizes": ["12px", "16px", "24px"],
"weights": ["100", "400", "600", "700", "900"],
}
cases.append({
"name": "font_family_size_and_weight_scale",
"passed": act1 == exp1,
"expected": exp1,
"actual": act1,
})
# Case 2: line-height, letter-spacing & color opacity modifiers
act2 = page.evaluate("""() => {
const g = id => getComputedStyle(document.getElementById(id));
return {
leadings: [g('lh-none').lineHeight, g('lh-tight').lineHeight, g('lh-snug').lineHeight, g('lh-normal').lineHeight, g('lh-relaxed').lineHeight, g('lh-loose').lineHeight],
trackings: [g('tr-tight').letterSpacing, g('tr-wide').letterSpacing, g('tr-wider').letterSpacing, g('tr-widest').letterSpacing],
sky_opacity: [g('c-100').color, g('c-50').color, g('c-0').color],
};
}""")
exp2 = {
"leadings": ["16px", "20px", "22px", "24px", "26px", "32px"],
"trackings": ["-0.4px", "0.4px", "0.8px", "1.6px"],
"sky_opacity": ["rgb(56, 189, 248)", "rgba(56, 189, 248, 0.5)", "rgba(56, 189, 248, 0)"],
}
cases.append({
"name": "leading_tracking_and_color_opacity",
"passed": act2 == exp2,
"expected": exp2,
"actual": act2,
})
# Case 3: alignment, decoration, transform & text-shadow
act3 = page.evaluate("""() => {
const g = id => getComputedStyle(document.getElementById(id));
return {
align: [g('al-l').textAlign, g('al-c').textAlign, g('al-r').textAlign],
decoration: [g('dc-u').textDecorationLine, g('dc-o').textDecorationLine, g('dc-lt').textDecorationLine, g('dc-n').textDecorationLine],
transform: [g('tf-u').textTransform, g('tf-l').textTransform, g('tf-c').textTransform],
text_shadow: g('shd').textShadow,
};
}""")
exp3 = {
"align": ["left", "center", "right"],
"decoration": ["underline", "overline", "line-through", "none"],
"transform": ["uppercase", "lowercase", "capitalize"],
"text_shadow": "rgba(0, 0, 0, 0.25) 2px 2px 4px",
}
cases.append({
"name": "alignment_decoration_transform_and_shadow",
"passed": act3 == exp3,
"expected": exp3,
"actual": act3,
})
# Case 4: responsive font-size breakpoints
resp_sizes = {}
for label, vw in [("base_400", 400), ("sm_640", 640), ("md_768", 768), ("lg_1024", 1024), ("xl_1280", 1280)]:
page.set_viewport_size({"width": vw, "height": 500})
resp_sizes[label] = page.evaluate("() => getComputedStyle(document.getElementById('resp')).fontSize")
exp4 = {
"base_400": "14px",
"sm_640": "16px",
"md_768": "18px",
"lg_1024": "20px",
"xl_1280": "24px",
}
cases.append({
"name": "responsive_font_size_breakpoints",
"passed": resp_sizes == exp4,
"expected": exp4,
"actual": resp_sizes,
})
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-typography-lab/explore-typography.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-typography.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a661.zip
课程关联工程包(登记关联24章) · 含当前章节或实例 · mbskycom202612a639.zip