Skip to content
Tutorials Center
My learning

Topics

CSS Reference ยท Functions

The env() function

Reference for the CSS env() function: reading environment variables such as safe-area-inset-top, fallback values, viewport-fit=cover and notched-device padding.

The env() function reads a value provided by the browser or device rather than by your CSS. The best-known use is the safe area insets: the distances by which a notch, rounded corner or system bar overlaps the screen. With them you can keep content out of those regions.

CSS
.toolbar {
  padding-bottom: max(0.75rem, env(safe-area-inset-bottom));
}

env() is similar to var(), but the names are defined by the environment, not by you, and cannot be set in a stylesheet.

Try it

env() with a fallback

Syntax

CSS
env( <environment-variable-name> , <fallback>? )

padding-top: env(safe-area-inset-top);
padding-top: env(safe-area-inset-top, 20px);

Syntax and arguments

Variable Description
safe-area-inset-top, -right, -bottom, -left Distance from each viewport edge to the safe area. Zero on devices with no obstruction.
safe-area-max-inset-* Largest value the inset can take; mainly in newer specifications, so check support.
titlebar-area-x, -y, -width, -height For installed web apps using Window Controls Overlay. Limited support.
keyboard-inset-top, -right, -bottom, -left, -width, -height On-screen keyboard geometry. Experimental, limited support.
fallback Optional. Used if the variable is not known to the browser.

Usage notes

  • Opt in with the viewport tag. On iOS-style devices the insets are non-zero only when the page extends under the unsafe areas: <meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">.
  • Combine with max() or calc() to keep a minimum padding: max(1rem, env(safe-area-inset-left)).
  • Always give a fallback when you use less common names; an unknown variable without fallback makes the declaration invalid at computed-value time.
  • Not for custom values: use var() for your own tokens.
  • Cannot be used in media queries, and values may change on rotation, so do not cache them in JavaScript.
  • Old syntax: early iOS used constant(). It is obsolete.

Browser support

The safe-area insets are widely supported on mobile browsers. The titlebar and keyboard variables are newer or experimental; check a compatibility table.