# CSS Environment Variables Module Level 1

## Editor’s Draft, 1 January 1970

Specification Metadata

This version:
  https://drafts.csswg.org/css-env-1/
Editors:
  Tab Atkins-Bittner (Google)
  Dean Jackson (Apple)
Issue Tracking:
  GitHub Issues
Suggest an Edit for this Spec:
  GitHub Editor

Copyright © 1970 W3C® (MIT, ERCIM, Keio, Beihang). W3C liability, trademark and permissive document license rules apply.

## Abstract

This specification defines the concept of environment variables and the env() function, which work similarly to custom properties and the var() function, but are defined globally for a document. These can be defined either by the User Agent, providing values that can be used on the page based on information the UA has special access to, or provided by the author for "global" variables that are guaranteed to be the same no matter where in the document they’re used.

CSS is a language for describing the rendering of structured documents (such as HTML and XML) on screen, on paper, etc.

## Status of this document

This is a public copy of the editors’ draft. It is provided for discussion only and may change at any moment. Its publication here does not imply endorsement of its contents by W3C. Don’t cite this document other than as work in progress.

GitHub Issues are preferred for discussion of this specification. When filing an issue, please put the text “css-env” in the title, preferably like this: “[css-env] …summary of comment…”. All issues and comments are archived, and there is also a historical archive.

This document was produced by the CSS Working Group.

This document was produced by a group operating under the W3C Patent Policy. W3C maintains a public list of any patent disclosures made in connection with the deliverables of the group; that page also includes instructions for disclosing a patent. An individual who has actual knowledge of a patent which the individual believes contains Essential Claim(s) must disclose the information in accordance with section 6 of the W3C Patent Policy.

This document is governed by the 1 March 2019 W3C Process Document.

## 1. Introduction

The [css-variables-1] specification defined the concept of "cascading variables", author-defined variables created from the value of custom properties, capable of being substituted into arbitrary other properties via the var() function.

This specification defines a related, but simpler, concept of environment variables. Unlike "cascading variables", which can change thruout the page as their corresponding custom property takes on different values, an environment variable is "global" to a particular document—its value is the same everywhere. The env() function can then be used to substitute the value into arbitrary locations, similar to the var() function.

These "global" variables have both benefits and downsides versus cascading variables:

- Many variables aren’t meant to change over the course of a page; they set up themes, or are helpers for particular numerical values. Using environment variables instead of custom properties to define these communicates the proper intent, which is good both for the author of the document (particularly when multiple people are collaborating on a single document), and for the user agent, as it can store these variables in a more optimal way.

- Because environment variables don’t depend on the value of anything drawn from a particular element, they can be used in places where there is no obvious element to draw from, such as in @media rules, where the var() function would not be valid.

- Information from the User Agent itself, such as the margin of the viewport to avoid laying out in by default (for example, to avoid overlapping a "notch" in the screen), can be retrieved via env(), whereas the element-specific nature of var() was not an appropriate place to pipe that information in.

## 2. Environment Variables

A CSS environment variable is a name associated with a <declaration-value> (a sequence of zero more CSS tokens, with almost no restrictions on what tokens can exist), similar to a custom property. Environment variables can be defined by the User Agent, or by the user. (In the latter case, the names are <custom-property-name>s, and start with `--` per standard for custom identifiers.)

Is the set of UA-defined environment variables visible to script? If so, define an API on `Document` to expose them.

Define how authors can add environment variables, preferably both via JS and via CSS. Note that mixing CSS rules and JS-defined stuff can easily get messy, as demonstrated by CSSFontFaceRule vs FontFace...

The following UA-defined environment variables are officially defined and must be supported. Additional UA-defined environment variables *must not* be supported unless/until they are added to this list.

### 2.1. Safe area inset variables

Name | Value
--- | ---
safe-area-inset-top | <length>
safe-area-inset-right | <length>
safe-area-inset-bottom | <length>
safe-area-inset-left | <length>

The safe area insets are four environment variables that define a rectangle by its top, right, bottom, and left insets from the edge of the viewport. For rectangular displays, these must all be zero, but for nonrectangular displays they must form a rectangle, chosen by the user agent, such that all content inside the rectangle is visible, and such that reducing any of the insets would cause some content inside of the rectangle to be invisible due to the nonrectangular nature of the display. This allows authors to limit the layout of essential content to the space inside of the safe area rectangle.

## 3. Using Environment Variables: the env() notation

In order to substitute the value of an environment variable into a CSS context, use the env() function:

    env() = env( <custom-ident> , <declaration-value>? )

The env() function can be used in place of any part of a value in any property on any element, or any part of a value in any descriptor on any at-rule, and in several other places where CSS values are allowed.

Define the full set of places env() can be used.

- Should be able to replace any subset of MQ syntax, for example.

- Should be able to replace selectors, maybe?

- Should it work on a rule level, so you can insert arbitrary stuff into a rule, like reusing a block of declarations?

The first argument to env() provides the name of an environment variable to be substituted. The second argument, if provided, is a fallback value, which is used as the substitution value when the referenced environment variable does not exist.

Note: The syntax of the fallback, like that of custom properties, allows commas. For example, env(foo, red, blue) defines a fallback of red, blue; that is, anything between the first comma and the end of the function is considered a fallback value.

If a property contains one or more env() functions, and those functions are syntactically valid, the entire property’s grammar must be assumed to be valid at parse time. It is only syntax-checked at computed-time, after env() functions have been substituted.

If a descriptor contains one or more env() functions, and those functions are syntactically valid, the entire declaration’s grammar must be assumed to be valid at parse time. It is only syntax-checked after env() functions have been substituted.

To substitute an env() in a property or descriptor:

1. If the name provided by the first argument of the env() function is a recognized environment variable, replace the env() function by the value of the named environment variable.

2. Otherwise, if the env() function has a fallback value as its second argument, replace the env() function by the fallback value. If there are any env() references in the fallback, substitute them as well.

3. Otherwise, the property or descriptor containing the env() function is invalid at computed-value time.

Define when substitution happens. It has to be before var() substitution. Alternately, should env() substitution happen at parse time, so unknown variable names cause it to fail syntax checking? There’s no particular reason to have it happen at computed-value time, like var() does—that was to ensure that custom properties could inherit their value down before they were picked up by a var().

When I figure out where else env() can go, define how/when it substitutes.

### 3.1. Environment Variables in Shorthand Properties

If env() substitution happens during parsing, then this is unnecessary.

The env() function causes the same difficulties with shorthand properties as the var() function does. When an env() is used in a shorthand property, then, it has the same effects as defined in CSS Variables 1 §3.2 Variables in Shorthand Properties.

## Conformance

### Document conventions

Conformance requirements are expressed with a combination of descriptive assertions and RFC 2119 terminology. The key words “MUST”, “MUST NOT”, “REQUIRED”, “SHALL”, “SHALL NOT”, “SHOULD”, “SHOULD NOT”, “RECOMMENDED”, “MAY”, and “OPTIONAL” in the normative parts of this document are to be interpreted as described in RFC 2119. However, for readability, these words do not appear in all uppercase letters in this specification.

All of the text of this specification is normative except sections explicitly marked as non-normative, examples, and notes. [RFC2119]

Examples in this specification are introduced with the words “for example” or are set apart from the normative text with `class="example"`, like this:

This is an example of an informative example.

Informative notes begin with the word “Note” and are set apart from the normative text with `class="note"`, like this:

Note, this is an informative note.

Advisements are normative sections styled to evoke special attention and are set apart from other normative text with `<strong class="advisement">`, like this: UAs MUST provide an accessible alternative.

### Conformance classes

Conformance to this specification is defined for three conformance classes:

style sheet
  A CSS style sheet.
renderer
  A UA that interprets the semantics of a style sheet and renders documents that use them.
authoring tool
  A UA that writes a style sheet.

A style sheet is conformant to this specification if all of its statements that use syntax defined in this module are valid according to the generic CSS grammar and the individual grammars of each feature defined in this module.

A renderer is conformant to this specification if, in addition to interpreting the style sheet as defined by the appropriate specifications, it supports all the features defined by this specification by parsing them correctly and rendering the document accordingly. However, the inability of a UA to correctly render a document due to limitations of the device does not make the UA non-conformant. (For example, a UA is not required to render color on a monochrome monitor.)

An authoring tool is conformant to this specification if it writes style sheets that are syntactically correct according to the generic CSS grammar and the individual grammars of each feature in this module, and meet all other conformance requirements of style sheets as described in this module.

### Requirements for Responsible Implementation of CSS

The following sections define several conformance requirements for implementing CSS responsibly, in a way that promotes interoperability in the present and future.

#### Partial Implementations

So that authors can exploit the forward-compatible parsing rules to assign fallback values, CSS renderers must treat as invalid (and ignore as appropriate) any at-rules, properties, property values, keywords, and other syntactic constructs for which they have no usable level of support. In particular, user agents must not selectively ignore unsupported property values and honor supported values in a single multi-value property declaration: if any value is considered invalid (as unsupported values must be), CSS requires that the entire declaration be ignored.

#### Implementations of Unstable and Proprietary Features

To avoid clashes with future stable CSS features, the CSSWG recommends following best practices for the implementation of unstable features and proprietary extensions to CSS.

#### Implementations of CR-level Features

Once a specification reaches the Candidate Recommendation stage, implementers should release an unprefixed implementation of any CR-level feature they can demonstrate to be correctly implemented according to spec, and should avoid exposing a prefixed variant of that feature.

To establish and maintain the interoperability of CSS across implementations, the CSS Working Group requests that non-experimental CSS renderers submit an implementation report (and, if necessary, the testcases used for that implementation report) to the W3C before releasing an unprefixed implementation of any CSS features. Testcases submitted to W3C are subject to review and correction by the CSS Working Group.

Further information on submitting testcases and implementation reports can be found from on the CSS Working Group’s website at http://www.w3.org/Style/CSS/Test/. Questions should be directed to the public-css-testsuite@w3.org mailing list.

## Index

### Terms defined by this specification

- env(), in §3
- environment variable, in §2
- safe-area-inset-bottom, in §2.1
- safe-area-inset-left, in §2.1
- safe-area-inset-right, in §2.1
- safe-area-inset-top, in §2.1
- substitute, in §3
- substitute an env(), in §3

https://drafts.csswg.org/css-cascade-4/#shorthand-property Referenced in:

- 3.1. Environment Variables in Shorthand Properties (2)

https://drafts.csswg.org/css-conditional-3/#at-ruledef-media Referenced in:

- 1. Introduction

https://drafts.csswg.org/css-syntax-3/#typedef-declaration-value Referenced in:

- 2. Environment Variables
- 3. Using Environment Variables: the env() notation

https://drafts.csswg.org/css-syntax-3/#at-rule Referenced in:

- 3. Using Environment Variables: the env() notation

https://drafts.csswg.org/css-values-3/#length-value Referenced in:

- 2.1. Safe area inset variables (2) (3) (4)

https://drafts.csswg.org/css-values-4/#comb-comma Referenced in:

- 3. Using Environment Variables: the env() notation

https://drafts.csswg.org/css-values-4/#identifier-value Referenced in:

- 3. Using Environment Variables: the env() notation

https://drafts.csswg.org/css-values-4/#mult-opt Referenced in:

- 3. Using Environment Variables: the env() notation

https://drafts.csswg.org/css-variables-1/#typedef-custom-property-name Referenced in:

- 2. Environment Variables

https://drafts.csswg.org/css-variables-1/#custom-property Referenced in:

- 1. Introduction (2) (3)
- 2. Environment Variables
- 3. Using Environment Variables: the env() notation

https://drafts.csswg.org/css-variables-1/#invalid-at-computed-value-time Referenced in:

- 3. Using Environment Variables: the env() notation

https://drafts.csswg.org/css-variables-1/#funcdef-var Referenced in:

- Unnumbered Section
- 1. Introduction (2) (3) (4)
- 3. Using Environment Variables: the env() notation (2) (3)
- 3.1. Environment Variables in Shorthand Properties

https://dom.spec.whatwg.org/#document Referenced in:

- 2. Environment Variables

### Terms defined by reference

- [css-cascade-4] defines the following terms:
  - shorthand property
- [css-conditional-3] defines the following terms:
  - @media
- [css-syntax-3] defines the following terms:
  - <declaration-value>
  - at-rule
- [css-values-3] defines the following terms:
  - <length>
- [css-values-4] defines the following terms:
  - ,
  - <custom-ident>
  - ?
- [css-variables-1] defines the following terms:
  - <custom-property-name>
  - custom property
  - invalid at computed-value time
  - var()
- [DOM] defines the following terms:
  - Document

## References

### Normative References

[CSS-CASCADE-4]
  Elika Etemad; Tab Atkins Jr.. CSS Cascading and Inheritance Level 4. 28 August 2018. CR. URL: https://www.w3.org/TR/css-cascade-4/
[CSS-CONDITIONAL-3]
  CSS Conditional Rules Module Level 3 URL: https://www.w3.org/TR/css3-conditional/
[CSS-SYNTAX-3]
  Tab Atkins Jr.; Simon Sapin. CSS Syntax Module Level 3. 20 February 2014. CR. URL: https://www.w3.org/TR/css-syntax-3/
[CSS-VALUES-3]
  Tab Atkins Jr.; Elika Etemad. CSS Values and Units Module Level 3. 31 January 2019. CR. URL: https://www.w3.org/TR/css-values-3/
[CSS-VALUES-4]
  Tab Atkins Jr.; Elika Etemad. CSS Values and Units Module Level 4. 31 January 2019. WD. URL: https://www.w3.org/TR/css-values-4/
[CSS-VARIABLES-1]
  Tab Atkins Jr.. CSS Custom Properties for Cascading Variables Module Level 1. 3 December 2015. CR. URL: https://www.w3.org/TR/css-variables-1/
[DOM]
  Anne van Kesteren. DOM Standard. Living Standard. URL: https://dom.spec.whatwg.org/
[RFC2119]
  S. Bradner. Key words for use in RFCs to Indicate Requirement Levels. March 1997. Best Current Practice. URL: https://tools.ietf.org/html/rfc2119

## Issues Index

Is the set of UA-defined environment variables visible to script? If so, define an API on `Document` to expose them. ↵
Define how authors can add environment variables, preferably both via JS and via CSS. Note that mixing CSS rules and JS-defined stuff can easily get messy, as demonstrated by CSSFontFaceRule vs FontFace... ↵
Define the full set of places env() can be used.

- Should be able to replace any subset of MQ syntax, for example.

- Should be able to replace selectors, maybe?

- Should it work on a rule level, so you can insert arbitrary stuff into a rule, like reusing a block of declarations?

↵
Define when substitution happens. It has to be before var() substitution. Alternately, should env() substitution happen at parse time, so unknown variable names cause it to fail syntax checking? There’s no particular reason to have it happen at computed-value time, like var() does—that was to ensure that custom properties could inherit their value down before they were picked up by a var(). ↵
When I figure out where else env() can go, define how/when it substitutes. ↵
If env() substitution happens during parsing, then this is unnecessary. ↵
#css-environment-variable Referenced in:

- 1. Introduction (2) (3) (4)
- 2. Environment Variables (2) (3) (4) (5)
- 2.1. Safe area inset variables
- 3. Using Environment Variables: the env() notation (2) (3) (4) (5)

#funcdef-env Referenced in:

- 1. Introduction (2)
- 3. Using Environment Variables: the env() notation (2) (3) (4) (5) (6) (7) (8) (9) (10) (11) (12) (13) (14) (15) (16) (17)
- 3.1. Environment Variables in Shorthand Properties (2) (3)

#substitute-an-env Referenced in:

- 3. Using Environment Variables: the env() notation (2) (3)