Tailwind CSS 排版
核对 Tailwind CSS 字体栈、字号阶梯、字重、行高、字距、文本色与透明度修饰、对齐、装饰线、大小写转换、文本阴影及响应式字号并离线实测。
学完这一章,你能够
- 核对 Tailwind CSS 字体栈、字号阶梯、字重、行高、字距、文本色与透明度修饰、对齐、装饰线、大小写转换、文本阴影及响应式字号并离线实测。
字体系列、字号阶梯与字重体系
Tailwind CSS 排版工具覆盖文本呈现的全部维度:字体族提供 `font-sans`(无衬线)、`font-serif`(衬线)与 `font-mono`(等宽);字号通过 `text-xs` 至 `text-9xl` 阶梯控制(同时附带协调的默认行高);字重涵盖 `font-thin`(`100`)、`font-light`(`300`)、`font-normal`(`400`)、`font-medium`(`500`)、`font-semibold`(`600`)、`font-bold`(`700`)、`font-extrabold`(`800`)与 `font-black`(`900`)。本站离线 Chromium 实测:`text-xs`/`text-base`/`text-2xl` 计算字号依次为 `12px`、`16px`、`24px`,五档代表字重精确对应 `100`、`400`、`600`、`700`、`900`。
行高、字间距与带透明度文本颜色
行高使用 `leading-none`(`1`)、`leading-tight`(`1.25`)、`leading-snug`(`1.375`)、`leading-normal`(`1.5`)、`leading-relaxed`(`1.625`)与 `leading-loose`(`2`);字间距提供 `tracking-tight`(`-0.025em`)、`tracking-normal`(`0em`)、`tracking-wide`(`0.025em`)、`tracking-wider`(`0.05em`)与极宽 `tracking-widest`(`0.1em`,部分资料常误重复写作 `tracking-wide`)。文本颜色既可直接写 `text-gray-500`、`text-red-500`、`text-blue-500`、`text-green-500`,也可加斜杠透明度修饰如 `text-sky-400/100` 至 `text-sky-400/0`。本站实测 `16px` 文本下六档行高依次为 `16px`、`20px`、`22px`、`24px`、`26px`、`32px`;四档字距为 `-0.4px`、`0.4px`、`0.8px`、`1.6px`;`text-sky-400` 在 `/100`、`/50`、`/0` 下计算颜色依次为 `rgb(56, 189, 248)`、`rgba(56, 189, 248, 0.5)` 与 `rgba(56, 189, 248, 0)`。
对齐、修饰线、大小写、阴影与响应式字号
文本对齐用 `text-left`/`text-center`/`text-right`,修饰线用 `underline`/`overline`/`line-through`/`no-underline`,英文大小写转换用 `normal-case`/`uppercase`/`lowercase`/`capitalize`。至于文本阴影,Tailwind v3 核心默认未内置 `text-shadow` 工具类,需通过插件或自定义工具类扩展(Tailwind v4.1 起已官方新增 `text-shadow-*`)。本站实测对齐、四类修饰线、三类大小写转换及自定义 `.text-shadow`(`rgba(0, 0, 0, 0.25) 2px 2px 4px`)均准确命中对应计算属性。
通过组合 `sm:`、`md:`、`lg:`、`xl:` 断点前缀可构建响应式字号阶梯(如 `text-sm sm:text-base md:text-lg lg:text-xl xl:text-2xl`)。本站实测该段落在 `400px`、`640px`、`768px`、`1024px`、`1280px` 五档视口宽度下,计算字号平滑递增为 `14px`、`16px`、`18px`、`20px` 与 `24px`。
离线 Chromium 验证字体族/字号/字重、行字距/透明度、装饰阴影及 5 档响应式字号
"""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()
- 核对 text-xs/base/2xl(12/16/24px)、字重 100-900、六档行高(16-32px)、四档字距(-0.4px 至 1.6px)、sky-400 透明度、修饰线/大小写/阴影及 400-1280px 视口字号 14-24px。
所需环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
当前源码 SHA256:5a684b6f6e77342f0b584d5f126424a64b78f262adc76d7f162d8f6bf37b9c1a
本地运行
解压后按 README 说明在对应本地环境运行脚本。
所需环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
ZIP SHA256:0355b421cfcffe97b529f1bdc34952fdccad391211c20fce8b47979d3f8fccb9
常见错误与边界
- 实际端口、文档资源与应用配置都应和测试条件一致。
练习与解答
Tailwind CSS 中 text-lg 与 text-sky-400/50 都以 text- 开头,框架是如何区分字号与颜色的?使用 text-shadow 时需要注意什么版本差异?
展开参考解答
Tailwind 依据后缀所属主题命名空间区分:xs/sm/base/lg/xl 等映射到 fontSize(并附带默认 line-height),而色板名加色阶及可选斜杠透明度映射到 color;text-shadow 在 Tailwind v3 默认未内置需通过插件或自定义类扩展,Tailwind v4.1+ 才原生提供 text-shadow-* 工具类。