env()

Term side of the Card.
Topic: Global Functions
Unit title: env()
Categories: Function
Explanation side of the Card

Explanation: The function that accesses environment variables, like safe area insets on mobile devices, to adjust the layout dynamically. It helps ensure that content doesn't overlap with system UI elements, like the notch or home bar, on devices with special hardware features.

Browser compatibility:
  • The chrome browser supports this feature starting from version 69.
  • The edge browser supports this feature starting from version 79.
  • The firefox browser supports this feature starting from version 65.
  • The safari browser supports this feature starting from version 11.1.
Availability: 

Example

Wrongcss
.toolbar {
  position: fixed;
  bottom: 0;
  padding-bottom: 16px;
}
Rightcss
.toolbar {
  position: fixed;
  bottom: 0;
  padding-bottom: max(16px, env(safe-area-inset-bottom));
}

max() keeps the normal padding and grows it on devices with a home indicator, so buttons stay tappable.

Common mistakes

  • env(safe-area-inset-bottom) is always 0 because viewport-fit=cover is missing.
  • Writing var(safe-area-inset-top) instead of env(safe-area-inset-top).

Key points

  • The viewport meta tag includes viewport-fit=cover, otherwise safe-area insets are zero.
  • env() calls include a fallback, like env(safe-area-inset-bottom, 0px).
  • Safe-area padding is added to existing padding with max() or calc(), not replacing it.

Interview questions

What is the difference between env() and var()?

var() reads custom properties you define. env() reads values the browser provides, like safe-area insets.

Why is env(safe-area-inset-top) zero?

Usually the viewport meta tag lacks viewport-fit=cover, or the device has no notch or rounded corners.

Which safe-area values can env() read?

safe-area-inset-top, safe-area-inset-right, safe-area-inset-bottom and safe-area-inset-left.

Analytics cookiesWe'd like to use analytics cookies to see which pages are useful. They're set only if you accept. Privacy Policy