🔄 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 同理用高度)。