π What is env() in CSS?
env() is a CSS function that allows you to accessenvironment variables provided by the browser or operating system. These variables help layouts adapt to special device features such as **notches, safe areas, foldable screens, keyboard insets, and system UI overlays**.
Note
β Ideal for safe-area insets, foldable screens, immersive UI
π― Why Use env()?
- Handle iPhone notches & rounded corners
- Prevent content from being hidden under system UI bars
- Support full-screen apps with safe padding
- Adapt to foldable screens and dynamic UI insets
- Create notch-aware headers, footers, and navbars
π Syntax
env() Syntax
env(<environment-variable>, <fallback>?)The optional fallback value is used when a browser does not support that environment variable.
π Common Environment Variables
| Variable | Meaning |
|---|---|
| safe-area-inset-top | Space at top (iPhone notch area) |
| safe-area-inset-right | Space on right edge |
| safe-area-inset-bottom | Space at bottom (gesture bar area) |
| safe-area-inset-left | Space on left edge |
Note
π§ͺ Example 1 β Safe Area Padding
Safe Area Padding
.page {
padding-top: env(safe-area-inset-top);
padding-bottom: env(safe-area-inset-bottom);
}Ensures your UI never overlaps with device notches or system bars.
π§ͺ Example 2 β Using Fallback Values
Fallback Usage
.header {
padding-top: env(safe-area-inset-top, 16px);
}On unsupported browsers, padding defaults to 16px.
π§ͺ Example 3 β Fullscreen App Layout
Fullscreen Safe Area
body {
padding:
env(safe-area-inset-top, 0)
env(safe-area-inset-right, 0)
env(safe-area-inset-bottom, 0)
env(safe-area-inset-left, 0);
}Ideal for fullscreen PWAs or mobile-first web apps.
π± Example 4 β Fixed Navbar with Notch Support
Notch-Aware Navbar
.navbar {
position: fixed;
top: 0;
width: 100%;
padding-top: env(safe-area-inset-top);
background: #000;
}The navbar automatically extends below the notch.
π§ How env() Works Internally
The browser reads device-specific safe areas or system UI geometries and injects their values into CSS. If the variable doesnβt exist β fallback is used β or value becomes 0.
π§© env() vs constant()
iOS Safari originally used constant(); it is now deprecated. Always use env(), which is the official standardized function.
π Advanced: Foldable Screens & Dual Displays
Foldable Device Example
.content {
padding-right: env(fold-right, 0px);
}For foldable devices, browsers may expose additional environment variables like hinge areas or folding lines.
β οΈ Common Mistakes
- β Using env() without providing fallbacks
- β Forgetting that values may be 0 on flat screens
- β Assuming all devices have safe areas (most desktops don't)
- β Using env() for layout spacing when no notch exists (creates unexpected blank space)
Note
π₯ Summary
The env() function allows CSS to adapt to real device environments, improving usability and ensuring content isn't hidden under notches, bars, or fold areas. It is essential for building modern mobile and fullscreen experiences.
Learn more βMDN Docs π