Viewport height, commonly abbreviated as vh, is a dynamic unit of measurement within CSS that defines length relative to the height of the user's screen. One unit of vh corresponds to exactly 1% of the viewport's total height, meaning 100vh represents the full height of the visible browsing area. This relative sizing mechanism is a cornerstone of responsive web design, allowing elements to scale fluidly as devices and browser windows change dimensions.
How vh Differs From Other CSS Units
To truly grasp the utility of vh, it is essential to understand how it contrasts with traditional units like pixels (px) and percentages (%). Fixed units like pixels remain static regardless of screen size, which can lead to content being cut off on mobile devices or leaving excessive empty space on large monitors. Percentage units are relative to a parent container, which can sometimes make calculations complex and unpredictable. In contrast, vh provides a direct link to the visible screen, offering a predictable and immediate relationship to the user's display that is both simple to use and highly effective for creating impactful layouts.
Practical Applications in Modern Design
Designers leverage vh to solve specific layout challenges that static units cannot easily address. A common use case is ensuring a landing page hero section always fills the entire screen, creating an immersive first impression without relying on complex JavaScript calculations. This unit is also invaluable for vertically centering content. By combining vh with Flexbox or CSS Grid, developers can align elements in the exact center of the screen, a task that is difficult to achieve reliably with fixed heights.
Full-Screen Sections and Overlays
When building single-page websites or modal windows, vh is the go-to solution for creating full-screen sections. Setting a container to 100vh guarantees that it stretches from the top of the screen to the bottom, covering the entire visual area. This is particularly useful for splash pages, image galleries, or privacy overlays where you want to command the user's immediate attention by eliminating scrollbars and focusing the view.
Considerations for Accessibility and Performance
While vh offers significant advantages, responsible implementation requires attention to potential pitfalls. On mobile devices, the viewport height can fluctuate when the address bar or navigation buttons appear or disappear during scrolling. This can cause elements set to 100vh to briefly resize or shift, creating a jarring user experience. Modern CSS solutions, such as using `dvh` (dynamic viewport height) or careful testing across various mobile browsers, help mitigate these inconsistencies.
Typography and Spacing Control
Beyond structural layout, vh is a powerful tool for typography and spacing that scales with the screen. Instead of fixing a heading's pixel size, using vh units allows text to grow proportionally to the device, ensuring headlines remain prominent on both a smartwatch and a large desktop display. Similarly, vertical spacing between sections can be controlled using vh to maintain consistent breathing room on any screen, contributing to a polished and professional aesthetic.