Understanding env() Safe Area Insets in CSS
Modern devices have notches, rounded corners, home indicators, and dynamic islands that can hide your content. The CSS env() function with safe area insets makes your layouts adapt to these hardware features automatically.
The Problem: Hardware Intrusions
The iPhone X introduced the notch in 2017, creating a new challenge for web developers. Content that looked perfect on rectangular screens suddenly hid behind hardware features. Today, this problem covers:
- Notches and Dynamic Islands (iPhone, many Android devices)
- Rounded display corners (most modern smartphones)
- Home indicators (gesture-based navigation bars)
- Camera punch-holes (Samsung, Google Pixel)
Without safe area handling, fixed headers hide behind notches and bottom navigation conflicts with home indicators, making parts of your app unusable.
Enabling Safe Area Insets
By default, browsers letterbox your content to avoid hardware intrusions. To use the full screen and handle safe areas yourself, add the viewport-fit meta tag:
The viewport-fit property accepts three values:
| Value | Behavior |
|---|---|
auto | Default letterboxing, content stays in safe area |
contain | Same as auto, explicit safe area containment |
cover | Content extends to full screen, you handle safe areas |
Setting viewport-fit=cover without handling safe areas hides content behind hardware features. Always combine it with env() insets.
The env() Function
The env() function reads environment variables set by the user agent. For safe areas, browsers provide four variables:
These values are dynamic and change based on:
- Device orientation (portrait vs landscape)
- Device hardware (notch position, corner radius)
- Browser chrome visibility (address bar, toolbars)
Practical Examples
Fixed Header with Safe Area
A common pattern is a fixed header that accounts for the top safe area.
Try It: Fixed Header
This sandbox simulates a device with a notch using CSS custom properties. Toggle the "notch" to see how env() adapts the layout.
Bottom Navigation Bar
For bottom-fixed navigation, account for the home indicator.
Try It: Bottom Navigation
See how the bottom navigation adapts when the home indicator is present.
Full-Screen Hero Section
For immersive hero sections that extend edge-to-edge.
Try It: Complete App Layout
A full example combining header, content, and bottom navigation with all safe area insets.
Fallback Values
The env() function takes a fallback value for browsers that don't support safe area insets or devices without hardware intrusions.
On devices without notches, or when viewport-fit isn't set to cover, safe area inset values resolve to 0px.
Using with CSS Custom Properties
Combine env() with CSS custom properties for more maintainable code.
Tailwind CSS Integration
If you use Tailwind CSS, extend your config to add safe area utilities.
Then use them in your markup.
Landscape Orientation
Safe area insets matter most in landscape mode, where notches appear on the left or right side.
Try It: Landscape Orientation
In landscape mode, the notch shifts to the side. Toggle to see the difference.
Browser Support
The env() function and safe area insets have excellent browser support.
| Browser | Support |
|---|---|
| Chrome | 69+ |
| Firefox | 65+ |
| Safari | 11.1+ |
| Edge | 79+ |
| iOS Safari | 11.2+ |
| Chrome Android | 69+ |
All modern browsers support env() and safe area insets. Just provide fallback values for older browsers or non-notched devices.
Common Pitfalls
Forgetting viewport-fit
Safe area insets only work when viewport-fit=cover is set.
Using env() in Unsupported Contexts
The env() function works in property values but not in media queries.
Doubling Up Safe Areas
When nesting elements, don't apply safe area padding more than once.
Summary
The env() function with safe area insets is essential for modern mobile web development:
- Enable full-screen mode with
viewport-fit=cover - Use
env()variables for top, right, bottom, and left insets - Provide fallbacks for older browsers and standard displays
- Combine with
calc()to add safe areas to existing spacing - Test in landscape where notches shift position
Handle safe areas well, and your web apps will feel native and professional across all modern devices.