When Apple introduced the iPhone X in 2017, web developers faced a new challenge: a smartphone display that extended beneath a visible "notch" housing the front camera and Face ID sensors. What initially seemed like a design obstacle became an opportunity to embrace edge-to-edge displays with proper content protection through CSS. Modern responsive web design now requires handling these display cutouts as standard practice. Understanding how CSS positioning works helps when implementing fixed headers and footers with safe area insets.
Key topics covered:
- The viewport-fit meta tag for full-screen content
- CSS environment variables (safe-area-inset-*)
- JavaScript integration for dynamic behavior
- Browser support and compatibility
- Best practices for modern development
Understanding The Notch Problem
What Is The Notch?
The notch is a display cutout at the top of smartphone screens that houses cameras, sensors, and speakers. Apple popularized this design with the iPhone X, and it has since appeared on numerous Android devices in various forms--including pill-shaped cutouts and hole-punch cameras. The challenge for web developers is that content displayed near screen edges can be obscured by these cutouts, creating a poor user experience.
Why Traditional Layouts Fail
Before safe area support, browsers added automatic margins to prevent content from appearing behind notches. This resulted in ugly white bars on the sides of content in landscape orientation, disrupting the immersive experience that edge-to-edge displays were meant to provide. Proper mobile optimization requires accounting for these display characteristics from the start. For more CSS techniques, learn about styling links with real underlines and modern digital typesetting with leading trim.
The Viewport-Fit Meta Tag
Enabling Full-Screen Content
The first step in handling notches is to tell the browser that your site should utilize the full screen area, including behind display cutouts:
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
The viewport-fit=cover value instructs browsers to expand the viewport to fill the entire screen, allowing content to render behind the notch area.
Understanding Viewport-Fit Values
| Value | Behavior |
|---|---|
auto | Default behavior, similar to contain |
contain | Content fits within rectangular safe area |
cover | Content extends to fill entire screen |
1/* Basic safe area implementation */2.header {3 padding-top: 16px;4 padding-top: env(safe-area-inset-top);5 padding-left: 16px;6 padding-left: env(safe-area-inset-left);7 padding-right: 16px;8 padding-right: env(safe-area-inset-right);9}10 11/* All four insets at once */12.content {13 padding: env(safe-area-inset-top)14 env(safe-area-inset-right)15 env(safe-area-inset-bottom)16 env(safe-area-inset-left);17}CSS Environment Variables: Safe Area Insets
The env() Function
CSS environment variables provide access to device-specific values that help position content appropriately. The env() function accepts system values defined by the browser, with safe-area-inset-* variables being the most relevant for notch handling.
Four Safe Area Values
env(safe-area-inset-top): Distance from top edge to safe content areaenv(safe-area-inset-right): Distance from right edge to safe content areaenv(safe-area-inset-bottom): Distance from bottom edge to safe content areaenv(safe-area-inset-left): Distance from left edge to safe content area
These values adapt automatically to device orientation and notch position, returning pixel values that account for the notch dimensions and rounded corners.
Browser Support
96.78%
Global browser support for safe area insets
0
JavaScript runtime cost
4
Safe area inset directions
1// JavaScript integration with CSS custom properties2 3// Set env() values as CSS custom properties4:root {5 --safe-area-top: env(safe-area-inset-top);6 --safe-area-bottom: env(safe-area-inset-bottom);7}8 9// Read values in JavaScript10const safeAreaTop = getComputedStyle(document.documentElement)11 .getPropertyValue('--safe-area-top')12 .trim();13 14// Feature detection15const hasSafeAreaSupport = CSS.supports(16 'padding-top', 17 'env(safe-area-inset-top)'18);Advanced Techniques
Sticky Headers And Footers
Sticky headers and footers require special attention because they remain visible as users scroll:
.sticky-header {
position: fixed;
top: 0;
left: 0;
right: 0;
padding-top: env(safe-area-inset-top);
padding-left: env(safe-area-inset-left);
padding-right: env(safe-area-inset-right);
z-index: 1000;
}
Sticky Footers With Safe Areas
.sticky-footer {
position: fixed;
bottom: 0;
left: 0;
right: 0;
padding-bottom: env(safe-area-inset-bottom);
padding-left: env(safe-area-inset-left);
padding-right: env(safe-area-inset-right);
}
Orientation-Based Styling
@media (orientation: portrait) {
.header {
padding-top: env(safe-area-inset-top);
}
}
@media (orientation: landscape) {
.header {
padding-left: env(safe-area-inset-left);
padding-right: env(safe-area-inset-right);
}
}
Always Include viewport-fit
Start every mobile-first project with viewport-fit=cover in the viewport meta tag to enable full-screen experiences.
Use Logical Properties
Use padding-block-start and padding-inline instead of physical properties for better internationalization support.
Test Both Orientations
Notch behavior differs between portrait and landscape. Always test layouts in both orientations.
Progressive Enhancement
Implement safe area insets with fallback values for maximum compatibility across all browsers.
Common Use Cases
Video Player Controls
Video players often stretch to full screen, making safe area handling critical:
.video-controls {
position: absolute;
bottom: 0;
left: 0;
right: 0;
padding: env(safe-area-inset-bottom);
background: linear-gradient(transparent, rgba(0,0,0,0.8));
}
Modal Dialogs
.modal-content {
max-height: calc(100vh - env(safe-area-inset-top) - env(safe-area-inset-bottom));
overflow-y: auto;
padding: env(safe-area-inset-top) env(safe-area-inset-right)
env(safe-area-inset-bottom) env(safe-area-inset-left);
}
Full-Width Backgrounds
For full-width backgrounds that extend behind notches:
.full-width-background {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}
.content-container {
padding: env(safe-area-inset-top) env(safe-area-inset-right)
env(safe-area-inset-bottom) env(safe-area-inset-left);
}
Conclusion
The notch challenge that emerged with the iPhone X has been elegantly solved through CSS environment variables and the viewport-fit meta tag. With browser support exceeding 96%, developers can confidently implement safe area layouts that provide immersive, full-screen experiences while protecting content from display cutouts. Our frontend development services incorporate these modern CSS techniques for polished user experiences.
Key takeaways:
- Always include
viewport-fit=coverin your viewport meta tag - Use
env(safe-area-inset-*)with fallback values - Test thoroughly in both portrait and landscape orientations
- Implement progressive enhancement for maximum compatibility
- No JavaScript required for basic implementations
This approach ensures websites look great on modern edge-to-edge displays while maintaining compatibility with older devices through progressive enhancement. For developers exploring modern CSS features, learn about the new CSS media query range syntax for responsive design patterns that complement safe area layouts.
Frequently Asked Questions
Sources
- CSS-Tricks: The Notch and CSS - Foundational article on safe area environment variables
- MDN Web Docs: env() - Official CSS environment variables specification
- Can I Use: CSS environment variables - Browser support statistics