HTML & CSSEasy
What are viewport units (vh, vw)?
Viewport units size things relative to the browser's viewport: 1vw is 1% of the viewport width and 1vh is 1% of its height (plus vmin and vmax for the smaller and larger dimension). Because 100vh is wrong on mobile when the browser toolbars show and hide, modern CSS adds svh, lvh and dvh variants.
The units
vw,vh: percent of the viewport width or height.vmin,vmax: percent of the smaller or larger side.vi,vb: the logical (inline and block) versions.
The mobile 100vh problem and the fix
On mobile, 100vh equals the height with the address bar hidden, so a 100vh hero is taller than the visible screen and pushes buttons out of view.
svh(small): the viewport with toolbars visible; content always fits.lvh(large): with toolbars hidden (whatvhused to mean).dvh(dynamic): updates as the toolbars move. It is accurate but can cause relayout while scrolling.
Use min-height: 100svh (or 100dvh) for full-screen sections.
Common uses
- Full-height heroes:
min-height: 100svh. - Fluid typography:
font-size: clamp(1rem, 0.8rem + 1vw, 1.5rem). - Spacing that scales with the screen.
Gotchas
100vwincludes the vertical scrollbar width on desktop, causing horizontal overflow; use100%instead.- Pure
vwfont sizes do not respond to browser zoom (an accessibility failure); always combine withreminsideclamp(). - Prefer
min-heightoverheightso content can grow.
Code example
html
1<style>2 * { box-sizing: border-box; }3 body { margin: 0; font-family: system-ui; }4 .hero {5 min-height: 100vh; /* fallback */6 min-height: 100svh; /* fits even with mobile toolbars visible */7 display: grid; place-items: center; text-align: center;8 background: #2b4bff; color: white; padding: 1rem;9 }10 h1 { font-size: clamp(1.75rem, 1rem + 4vw, 4rem); margin: 0; } /* zoom-safe fluid type */11 .square { width: 30vmin; height: 30vmin; background: #ffd84d; border: 3px solid #0b1020; margin: 1rem auto; }12</style>13<section class="hero">14 <div>15 <h1>Full height on every phone</h1>16 <div class="square"></div>17 <p id="out"></p>18 </div>19</section>20<script>21 const show = () => (document.getElementById("out").textContent = `1vw = ${(innerWidth / 100).toFixed(1)}px, 1vh = ${(innerHeight / 100).toFixed(1)}px`);22 addEventListener("resize", show);23 show();24</script>
Practice this question
Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the HTML & CSS set.