Viewport
The viewport is the user’s visible area of a web page. It varies with the device - it will be smaller on a mobile phone than on a computer screen. The viewport is fundamental to responsive web design and affects how content is displayed and interacted with across different devices.
Types of Viewports
Visual Viewport
The part of the page that’s currently visible in the browser window. When users pinch-zoom, the visual viewport gets smaller while showing a zoomed-in portion of the layout viewport.
Layout Viewport
The area where the browser renders the website. On mobile devices, this is typically wider than the visual viewport to accommodate desktop sites.
Viewport Meta Tag
Control how your page displays on mobile devices:
<meta name="viewport" content="width=device-width, initial-scale=1.0">
Common attributes:
- width: Sets viewport width (device-width or specific value)
- initial-scale: Sets initial zoom level
- maximum-scale: Maximum zoom level allowed
- minimum-scale: Minimum zoom level allowed
- user-scalable: Whether users can zoom (avoid disabling zoom for accessibility)
Viewport Units
CSS units relative to viewport dimensions:
- vw: 1% of viewport width
- vh: 1% of viewport height
- vmin: 1% of viewport’s smaller dimension
- vmax: 1% of viewport’s larger dimension
- dvh/dvw: Dynamic viewport height/width (accounts for browser UI)
- svh/svw: Small viewport height/width (minimum size)
- lvh/lvw: Large viewport height/width (maximum size)
Common Use Cases
Responsive Design
Adapting layouts based on viewport size using media queries:
@media (max-width: 768px) {
/* Mobile styles */
}
Full-Height Sections
Creating sections that fill the viewport:
.hero {
height: 100vh;
}
/* Note: On mobile, consider using dynamic viewport units (dvh/svh/lvh) to account for browser chrome that hides/shows on scroll */
Viewport-Based Typography
Scaling text based on viewport size:
h1 {
font-size: clamp(2rem, 5vw, 4rem);
}
Mobile Considerations
- Safe Areas: Account for device notches and rounded corners
- Orientation Changes: Handle landscape/portrait transitions
- Browser Chrome: Consider address bars that hide/show on scroll
- Pinch-to-Zoom: Ensure accessibility by not disabling zoom
Testing Viewports
Tools for testing across viewports:
- Browser DevTools responsive mode
- BrowserStack or similar services
- Physical device testing
- Viewport resizer extensions
Related Patterns
Viewport considerations are crucial for:
- Pagination - Ensuring controls are visible
- Infinite Scroll - Detecting when to load content
- Load More - Button positioning in viewport
Best Practices
- Always include the viewport meta tag
- Test on real devices, not just browser tools
- Consider both portrait and landscape orientations
- Account for dynamic viewport changes (mobile browsers)
- Ensure critical content is above the fold
- Use viewport units carefully with fallbacks
Stay updated with new patterns
Get notified when new UX patterns are added to the collection.