Tailwind CSS 尺寸控制
核对 Tailwind CSS 宽度(w-*)、高度(h-* 与 dvh/lvh/svh 视口单位)、等宽高(size-*)及最小/最大/自动尺寸约束工具类的计算规则与实测几何。
学完这一章,你能够
- 核对 Tailwind CSS 宽度(w-*)、高度(h-* 与 dvh/lvh/svh 视口单位)、等宽高(size-*)及最小/最大/自动尺寸约束工具类的计算规则与实测几何。
宽度与高度控制(固定、百分比与现代视口单位)
Tailwind CSS 通过 `w-*` 统一管理元素宽度,支持固定刻度(`w-px`、`w-24`、`w-64`、`w-96` 等)、分数百分比(`w-1/2`、`w-2/5`、`w-3/5`、`w-full`)、视口宽度(`w-screen` 即 `100vw`)以及在断点取消定宽的 `w-auto`(如 `w-full md:w-auto`)。本站离线 Chromium 在 `800×600` 视口实测:`w-px`/`w-24`/`w-64`/`w-96`/`w-screen` 实测宽度依次为 `1px`、`96px`、`256px`、`384px`、`800px`;`600px` 弹性父容器内 `w-1/2`、`w-1/4`、`w-2/5`、`w-3/5` 依次为 `300px`、`150px`、`240px`、`360px`。
`h-*` 控制元素高度,需要注意百分比高度(`h-full`、`h-1/2`)生效的前提是父容器具有明确高度(例如父级设 `h-48` 即 `192px` 时,子级 `h-full` 实测 `192px`、`h-1/2` 实测 `96px`)。针对移动端浏览器地址栏显隐引发的视口跳动,Tailwind 除 `h-screen`(`100vh`)外还内置 `h-dvh`(动态视口高度)、`h-lvh`(大视口最大高度)与 `h-svh`(小视口最小高度);在桌面无头 `800×600` 视口中四者实测均等于 `600px`,其动态差异仅在带伸缩地址栏的移动端浏览器出现。
Size 等宽高工具类与响应式缩放
`size-*` 工具类一次性同时设置 `width` 与 `height`(例如 `size-16` 等效 `w-16 h-16`),非常适合圆形头像、图标按钮、固定正方形卡片等需要等宽高的元素,同样支持 `size-full`(占满有定高的父容器)与 `size-auto`(断点恢复内容自适应)以及响应式前缀。本站实测 `size-0`/`size-4`/`size-16` 分别为 `0×0`/`16×16`/`64×64`,`224×224` 父级内 `size-full` 为 `224×224`;`size-16 md:size-32 lg:size-64` 在 `500px`/`800px`/`1100px` 视口下依次呈现 `64×64`、`128×128` 与 `256×256`。
最小/最大尺寸约束与自动尺寸
为防止内容过少塌陷或过长撑破版面,可使用 `min-w-*`(`min-w-0`、`min-w-full`、`min-w-max`)、`min-h-*`(`min-h-0`、`min-h-full`、`min-h-screen`)、`max-w-*`(`max-w-xs`、`max-w-sm`、`max-w-full`)与 `max-h-*`(`max-h-32`、`max-h-screen`)施加边界钳制,并配合 `w-auto`/`h-auto` 交由内容自动决定尺寸。本站实测:`400px` 容器内宽 `50px` 的元素挂 `min-w-full` 后被撑至 `400px`;`600px` 容器内 `max-w-xs` 与 `max-w-sm` 将宽度上限分别锁在 `320px` 与 `384px`;`min-h-screen` 最小高度为 `600px`;`300px` 高的块挂 `max-h-32` 后被截限在 `128px`。
常用宽度工具类(w-*)
| 类名 | 含义 | 示例 |
| w-1/2 | 宽度为父元素 50%(600px 父级实测 300px) | <div class="w-1/2">Content</div> |
| w-1/4 | 宽度为父元素 25%(600px 父级实测 150px) | <div class="w-1/4">Content</div> |
| w-full | 宽度为父元素 100% | <div class="w-full">Content</div> |
| w-screen | 宽度为视口 100vw(800px 视口实测 800px) | <div class="w-screen">Content</div> |
| w-64 | 固定宽度 16rem(256px) | <div class="w-64">Content</div> |
| w-auto | 宽度按内容与流式布局自动计算 | <div class="w-auto">Content</div> |
常用高度工具类(h-*)
| 类名 | 含义 | 示例 |
| h-16 | 固定高度 4rem(实测 64px) | <div class="h-16">Content</div> |
| h-1/2 | 父级定高时的 50%(192px 父级实测 96px) | <div class="h-1/2">Content</div> |
| h-screen | 视口高度 100vh(600px 高视口实测 600px) | <div class="h-screen">Content</div> |
| h-auto | 高度随内部内容自动撑开 | <div class="h-auto">Content</div> |
等宽高工具类(size-*)
| 类名 | 含义 | 示例 |
| size-0 | 宽与高同为 0px | <div class="size-0">Content</div> |
| size-1 | 宽与高同为 0.25rem(4px) | <div class="size-1">Content</div> |
| size-2 | 宽与高同为 0.5rem(8px) | <div class="size-2">Content</div> |
| size-4 | 宽与高同为 1rem(实测 16×16) | <div class="size-4">Content</div> |
| size-8 | 宽与高同为 2rem(32px) | <div class="size-8">Content</div> |
| size-16 | 宽与高同为 4rem(实测 64×64) | <div class="size-16">Content</div> |
| size-32 | 宽与高同为 8rem(实测 128×128) | <div class="size-32">Content</div> |
| size-64 | 宽与高同为 16rem(实测 256×256) | <div class="size-64">Content</div> |
| size-auto | 宽与高均恢复 auto 自适应 | <div class="size-auto">Content</div> |
最小宽度工具类(min-w-*)
| 类名 | 含义 | 示例 |
| min-w-full | 最小宽度 100%(400px 父级实测 400px) | <div class="min-w-full">Content</div> |
| min-w-0 | 最小宽度 0px(常用于允许 Flex 子项收缩截断) | <div class="min-w-0">Content</div> |
| min-w-max | 最小宽度等于内容固有最大宽度 max-content | <div class="min-w-max">Content</div> |
最小高度工具类(min-h-*)
| 类名 | 含义 | 示例 |
| min-h-0 | 最小高度 0px | <div class="min-h-0">Content</div> |
| min-h-full | 最小高度为父元素 100% | <div class="min-h-full">Content</div> |
| min-h-screen | 最小高度为视口高度 100vh(实测 600px) | <div class="min-h-screen">Content</div> |
最大宽度工具类(max-w-*)
| 类名 | 含义 | 示例 |
| max-w-xs | 最大宽度 20rem(实测上限 320px) | <div class="max-w-xs">Content</div> |
| max-w-sm | 最大宽度 24rem(实测上限 384px) | <div class="max-w-sm">Content</div> |
| max-w-full | 最大宽度不超过父容器 100% | <div class="max-w-full">Content</div> |
最大高度工具类(max-h-*)
| 类名 | 含义 | 示例 |
| max-h-32 | 最大高度 8rem(实测上限 128px) | <div class="max-h-32">Content</div> |
| max-h-screen | 最大高度不超过视口高度 100vh | <div class="max-h-screen">Content</div> |
自动尺寸工具类(auto)
| 类名 | 含义 | 示例 |
| w-auto | 宽度自动适应内容或布局 | <div class="w-auto">Content</div> |
| h-auto | 高度自动适应内容 | <div class="h-auto">Content</div> |
离线 Chromium 验证宽高刻度、百分比/视口单位、响应式 size-* 及 min/max 钳制
展开查看收起
"""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()
查看独立实例 ↗ 在独立页面查看源码、运行环境与工程包
本地运行
解压后按 README 说明在对应本地环境运行脚本。
本章附件与实例包
常见错误与边界检查
- 实际端口、文档资源与应用配置都应和测试条件一致。
技术规范与官方资料
练习与参考解答
查看参考解答
100vh 通常按最大视口(等同 100lvh)计算,当移动端浏览器工具栏展开时容易超出可视区域;h-dvh 编译为 100dvh,会随浏览器顶部/底部 UI 的展开与收起动态更新高度。