Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / CSS

env() — environment value with fallback

`env()` substitutes a UA-provided global environment variable, optional indices for an array-like value, and a fallback when the name or index is unavailable.

Version: CSS modules; structured CSSWG indexes reconciled with pinned MDN BCD; Chromium 152 scope gate, per-example verification still required

Syntax

env( <custom-ident> <integer [0,∞]>* [ , <declaration-value>? ]? )

env() substitutes a UA-provided global environment variable, optional indices for an array-like value, and a fallback when the name or index is unavailable.

The example obtains 17px from env(cktis-missing, 17px), while env(cktis-missing) without a fallback becomes invalid at computed-value time and auto width in a 200 px host yields 200px. env(safe-area-inset-left, 99px) returns existing 0px, so its 99 px fallback is not used. The @supports (env(safe-area-inset-left)) query returns false in this Chromium.

Grammar: env( <custom-ident> <integer [0,∞]>* [ , <declaration-value>? ]? ).

The fallback is everything after the first comma and may itself contain commas. Unlike var(), environment variables are document-global and do not come from the cascade. An existing zero value still exists; no fallback invalidates the declaration only at computed-value time. The env form in a CSS Conditional 5 supports query tests recognition of a name and is a separate, currently unsupported surface from value substitution.

Examples

const host=document.createElement('div'); host.style.width='200px';
const fallback=document.createElement('div'); fallback.style.width='env(cktis-missing, 17px)';
const missing=document.createElement('div'); missing.style.width='env(cktis-missing)';
const safe=document.createElement('div'); safe.style.paddingLeft='env(safe-area-inset-left, 99px)';
host.append(fallback,missing,safe); document.body.append(host);
const supportsQuery=CSS.supports('env(safe-area-inset-left)');
console.log('fallback='+getComputedStyle(fallback).width+'; missing-no-fallback='+getComputedStyle(missing).width+'; safe-area-left='+getComputedStyle(safe).paddingLeft+'; supports-env-query='+supportsQuery);

Result

fallback=17px; missing-no-fallback=200px; safe-area-left=0px; supports-env-query=false

`env()` substitutes a UA-provided global environment variable, optional indices for an array-like value, and a fallback when the name or index is unavailable.

Sources