🔄 Viewport Unit (vw/vh) Converter
Convert between viewport units and pixels: vw and vh are 1% of the viewport width and height, handy for full-screen layouts.
📐 工作原理与说明
px → vw:vw = px ÷ 视口宽度 × 100;vw → px:px = vw × 视口宽度 ÷ 100(vh 同理按高度计算)
vw 与 vh 分别是视口宽、高的 1%,因此换算需给定基准视口尺寸。按宽度换算时以视口宽为基准、按高度换算时以视口高为基准,反向(vw/vh → px)则为乘算,用于替代写死的 px 实现自适应间距与全屏布局。
Formula & Standard References
| Item | Formula / Note | Source / Standard |
|---|---|---|
| Viewport percentage | 1vw is one percent of viewport width, 1vh of height | CSS Values and Units Level 4 |
| Mobile viewport height | browser chrome makes 100vh overflow, so use svh, lvh or dvh | CSS Values and Units Level 4 |
| Extreme units | vmin follows the shorter axis and vmax the longer | CSS Values and Units Level 4 |
⚠️ 使用说明与注意事项
- vw 永远基于视口【宽度】,vh 永远基于视口【高度】,二者互不相干。
- 移动端注意:部分浏览器的 vh 包含地址栏高度,可用 dvh/svh/lvh 等动态视口单位更精准。
- 100vw 在大屏可能超出预期并引发横向滚动,用于全宽时建议配合 overflow-x:hidden。
📺 常见视口断点参考
| 设备 | 宽度(px) | 典型 vw 示例 |
|---|---|---|
| 手机 | 360–414 | 100vw ≈ 360–414px |
| 平板 | 768–1024 | 50vw ≈ 384–512px |
| 笔记本 | 1280–1440 | 50vw ≈ 640–720px |
| 桌面 | 1920 | 50vw = 960px |
换算公式:vw = px ÷ 视口宽度 × 100;px = vw ÷ 100 × 视口宽度(vh 同理用高度)。