Title: CSS Values and Units Module Level 4
Group: CSSWG
Shortname: css-values
Level: 4
Status: ED
Work Status: Exploring
ED: https://drafts.csswg.org/css-values-4/
TR: https://www.w3.org/TR/css-values-4/
Previous version: https://www.w3.org/TR/2018/WD-css-values-4-20180814/
Editor: Tab Atkins, Google, http://xanthir.com/contact/, w3cid 42199
Editor: fantasai, http://fantasai.inkedblade.net/contact, w3cid 35400
Abstract: This CSS module describes the common values and units that CSS properties accept and the syntax used for describing them in CSS property definitions.
At Risk: ''toggle()'', ''attr()''
Ignored Terms: , containing block
Ignored Vars: Cn+1, n
Inline Github Issues: yes

Introduction

The value definition field of each CSS property can contain keywords, data types (which appear between ''<'' and ''>''), and information on how they can be combined. Generic data types (<> being the most widely used) that can be used by many properties are described in this specification, while more specific data types (e.g., <>) are described in the corresponding modules.

Module Interactions

This module replaces and extends the data type definitions in [[!CSS21]] sections 1.4.2.1, 4.3, and A.2.

Value Definition Syntax

The syntax described here is used to define the set of valid values for CSS properties. A property value can have one or more components.

Component value types

Component value types are designated in several ways:
  1. keyword values (such as auto, ''disc'', etc.), which appear literally, without quotes (e.g. auto)
  2. basic data types, which appear between ''<'' and ''>'' (e.g., <>, <>, etc.).
  3. types that have the same range of values as a property bearing the same name (e.g., <<'border-width'>>, <<'background-attachment'>>, etc.). In this case, the type name is the property name (complete with quotes) between the brackets. Such a type does not include CSS-wide keywords such as ''inherit''.
  4. non-terminals that do not share the same name as a property. In this case, the non-terminal name appears between ''<'' and ''>'', as in <>. Notice the distinction between <> and <<'border-width'>>: the latter is defined as the value of the 'border-width' property, the former requires an explicit expansion elsewhere. The definition of a non-terminal is typically located near its first appearance in the specification.
Some property value definitions also include the slash (/), the comma (,), and/or parentheses as literals. These represent their corresponding tokens. Other non-keyword literal characters that may appear in a component value, such as “+”, must be written enclosed in single quotes. Commas specified in the grammar are implicitly omissible in some circumstances, when used to separate optional terms in the grammar. Within a top-level list in a property or other CSS value, or a function's argument list, a comma specified in the grammar must be omitted if:
For example, if a function can accept three arguments in order, but all of them are optional, the grammar can be written like:
		example( first? , second? , third? )
		
Given this grammar, writing ''example(first, second, third)'' is valid, as is ''example(first, second)'' or ''example(first, third)'' or ''example(second)''. However, ''example(first, , third)'' is invalid, as one of those commas are no longer separating two options; similarly, ''example(,second)'' and ''example(first,)'' are invalid. ''example(first second)'' is also invalid, as commas are still required to actually separate the options. If commas were not implicitly omittable, the grammar would have to be much more complicated to properly express the ways that the arguments can be omitted, greatly obscuring the simplicity of the feature.
All CSS properties also accept the CSS-wide keyword values as the sole component of their property value. For readability these are not listed explicitly in the property value syntax definitions. For example, the full value definition of 'border-color' is <color>{1,4} | inherit | initial | unset (even though it is listed as <color>{1,4}). Note: This implies that, in general, combining these keywords with other component values in the same declaration results in an invalid declaration. For example, ''background: url(corner.png) no-repeat, inherit;'' is invalid.

Component value combinators

Component values can be arranged into property values as follows: Juxtaposition is stronger than the double ampersand, the double ampersand is stronger than the double bar, and the double bar is stronger than the bar. Thus, the following lines are equivalent:
	  a b   |   c ||   d &&   e f
	[ a b ] | [ c || [ d && [ e f ]]]
	
For reorderable combinators (||, &&), ordering of the grammar does not matter: components in the same grouping may be interleaved in any order. Thus, the following lines are equivalent:
	a || b || c
	b || a || c
	

Component value multipliers

Every type, keyword, or bracketed group may be followed by one of the following modifiers: For repeated component values (indicated by ''*'', ''+'', or ''#''), UAs must support at least 20 repetitions of the component. If a property value contains more than the supported number of repetitions, the declaration must be ignored as if it were invalid.

Combinator and Multiplier Patterns

There are a small set of common ways to combine multiple independent component values in particular numbers and orders. In particular, it's common to want to express that, from a set of component value, the author must select zero or more, one or more, or all of them, and in either the order specified in the grammar or in any order. All of these can be easily expressed using simple patterns of combinators and multipliers:
in order any order
zero or more A? B? C? A? || B? || C?
one or more [ A? B? C? ]! A || B || C
all A B C A && B && C
Note that all of the "any order" possibilities are expressed using combinators, while the "in order" possibilities are all variants on juxtaposition.

Component values and white space

Unless otherwise specified, white space and/or comments may appear before, after, and/or between components combined using the above combinators and multipliers. Note: In many cases, spaces will in fact be required between components in order to distinguish them from each other. For example, the value ''1em2em'' would be parsed as a single <> with the number ''1'' and the identifier ''em2em'', which is an invalid unit. In this case, a space would be required before the ''2'' to get this parsed as the two lengths ''1em'' and ''2em''.

Property value examples

Below are some examples of properties with their corresponding value definition fields
Property Value definition field Example value
'orphans' <integer> ''3''
'text-align' left | right | center | justify ''text-align/center''
'padding-top' <length> | <percentage> ''5%''
'outline-color' <color> | invert ''#fefefe''
'text-decoration' none | underline || overline || line-through || blink ''overline underline''
font-family [ <family-name> | <generic-family> ]# ''"Gill Sans", Futura, sans-serif''
'border-width' [ <length> | thick | medium | thin ]{1,4} ''2px medium 4px''
'text-shadow' [ inset? && [ <length>{2,4} && <color>? ] ]# | none ''3px 3px rgba(50%, 50%, 50%, 50%), lemonchiffon 0 0 4px inset''

Combining Values: Interpolation, Addition, and Accumulation

Some procedures, for example transitions and animations, combine two CSS property values. The following combining operations-- on the two computed values Va and VB yielding the computed value Vresult-- are defined:
interpolation
Given two property values Va and VB, produces an intermediate value Vresult at a distance of p along the interval between Va and Va such that p = 0 produces Va and p = 1 produces Vend. The range of p is (−∞, ∞) due to the effect of timing functions. As a result, this procedure must also define extrapolation behavior for p outside [0, 1].
addition
Given two property values Va and VB, returns the sum of the two properties, Vresult. For addition that is not commutative (for example, matrix multiplication) Va represents the first term of the operation and VB represents the second. Note: While addition can often be expressed in terms of the same weighted sum function used to define interpolation, this is not always the case. For example, interpolation of transform matrices involves decomposing and interpolating the matrix components whilst addition relies on matrix multiplication.
accumulation
Given two property values Va and VB, returns the result, Vresult, of combining the two operands such that VB is treated as a delta from Va. For accumulation that is not commutative (for example, accumulation of mismatched transform lists) Va represents the first term of the operation and VB represents the second.
Note: For many types of animation such as numbers or lengths, accumulation is defined to be identical to addition. A common case where the definitions differ is for list-based types where addition may be defined as appending to a list whilst accumulation may be defined as component-based addition. For example, the filter list values ''blur(2)'' and ''blur(3)'', when added together would produce ''blur(2) blur(3)'', but when accumulated would produce ''blur(5)''.
These operations are only defined on computed values. (As a result, it is not necessary to define, for example, how to add a <> value of ''15pt'' with ''5em'' since such values will be resolved to their canonical unit before being passed to any of the above procedures.) If a value type does not define a specific procedure for addition or is defined as not additive, its addition operation is simply Vresult = Va. If a value types does not define a specific procedure for accumulation, its accumulation operation is identical to addition.

Textual Data Types

Textual data types are used to represent identifiers or text. Aside from the casing of pre-defined keywords or as explicitly defined for a given property, no normalization is performed, not even Unicode normalization: the specified and computed value of a property are exactly the provided Unicode values after parsing (which includes character set conversion and [[css-syntax-3#escaping|escaping]]). [[!UNICODE]] [[!CSS3SYN]] CSS identifiers, generically denoted by <ident>, consist of a sequence of characters conforming to the <> grammar. [[!CSS3SYN]] Identifiers cannot be quoted; otherwise they would be interpreted as strings. CSS properties accept two classes of identifiers: [[#keywords|pre-defined keywords]] and [[#custom-idents|author-defined identifiers]]. Note: The <> production is not meant for property value definitions-- <> should be used instead. It is provided as a convenience for defining other syntactic constructs. All textual data types interpolate as discrete and are not additive.

Pre-defined Keywords

In the value definition fields, keywords with a pre-defined meaning appear literally. Keywords are CSS identifiers and are interpreted ASCII case-insensitively (i.e., [a-z] and \[A-Z] are equivalent).
For example, here is the value definition for the 'border-collapse' property:
Value: collapse | separate
And here is an example of its use:
table { border-collapse: separate }

CSS-wide keywords: ''initial'', ''inherit'' and ''unset''

As defined above, all properties accept the CSS-wide keywords, which represent value computations common to all CSS properties. The ''initial'' keyword represents the value specified as the property's initial value. The ''inherit'' keyword represents the computed value of the property on the element's parent. The ''unset'' keyword acts as either ''inherit'' or ''initial'', depending on whether the property is inherited or not. All of these keywords are normatively defined in the Cascade module. [[!CSS3CASCADE]] Other CSS specifications can define additional CSS-wide keywords.

Author-defined Identifiers: the <> type

Some properties accept arbitrary author-defined identifiers as a component value. This generic data type is denoted by <custom-ident>, and represents any valid CSS identifier that would not be misinterpreted as a pre-defined keyword in that property's value definition. Such identifiers are fully [=case-sensitive=] (meaning they're compared by codepoint), even in the ASCII range (e.g. ''example'' and ''EXAMPLE'' are two different, unrelated user-defined identifiers). The CSS-wide keywords are not valid <>s. The ''default'' keyword is reserved and is also not a valid <>. Specifications using <> must specify clearly what other keywords are excluded from <>, if any-- for example by saying that any pre-defined keywords in that property's value definition are excluded. Excluded keywords are excluded in all ASCII case permutations. When parsing positionally-ambiguous keywords in a property value, a <> production can only claim the keyword if no other unfulfilled production can claim it.
For example, the shorthand declaration ''animation: ease-in ease-out'' is equivalent to the longhand declarations ''animation-timing-function: ease-in; animation-name: ease-out;''. ''ease-in'' is claimed by the <> production belonging to 'animation-timing-function', leaving ''ease-out'' to be claimed by the <> production belonging to 'animation-name'.
Note: When designing grammars with <>, the <> should always be "positionally unambiguous", so that it's impossible to conflict with any keyword values in the property.

Quoted Strings: the <> type

Strings are denoted by <string> and consist of a sequence of characters delimited by double quotes or single quotes. They correspond to the <> production in the CSS Syntax Module [[!CSS3SYN]].
Double quotes cannot occur inside double quotes, unless escaped (as "\"" or as "\22"). Analogously for single quotes ('\'' or '\27').
		content: "this is a 'string'.";
		content: "this is a \"string\".";
		content: 'this is a "string".';
		content: 'this is a \'string\'.'
		
It is possible to break strings over several lines, for aesthetic or other reasons, but in such a case the newline itself has to be escaped with a backslash (\). The newline is subsequently removed from the string. For instance, the following two selectors are exactly the same:

Example(s):

		a[title="a not s\
		o very long title"] {/*...*/}
		a[title="a not so very long title"] {/*...*/}
		
Since a string cannot directly represent a newline, to include a newline in a string, use the escape "\A". (Hexadecimal A is the line feed character in Unicode (U+000A), but represents the generic notion of "newline" in CSS.)

Resource Locators: the <> type

The url() functional notation, denoted by <>, represents a [=/URL=], which is a pointer to a resource. The typical syntax of a <> is:
<url> = url( <> <>* )
Below is an example of a URL being used as a background image:
body { background: url("http://www.example.com/pinkish.gif") }
A <> may alternately be written without quotation marks around the URL itself, in which case it is specially-parsed as a <> [[!CSS3SYN]].
For example, the following declarations are identical:
			background: url("http://www.example.com/pinkish.gif");
			background: url(http://www.example.com/pinkish.gif);
		
Note: This unquoted syntax is cannot accept a <> argument and has extra escaping requirements: parentheses, whitespace characters, single quotes (') and double quotes (") appearing in a URL must be escaped with a backslash, e.g. ''url(open\(parens)'', ''url(close\)parens)''. (In quoted <> ''url()''s, only newlines and the character used to quote the string need to be escaped.) Depending on the type of URL, it might also be possible to write these characters as URL-escapes (e.g. ''url(open%28parens)'' or ''url(close%29parens)'') as described in [[URL]]. Some CSS contexts (such as ''@import'') also allow a <> to be represented by a bare <>, without the ''url()'' wrapper. In such cases the string behaves identically to a ''url()'' function containing that string.
For example, the following statements are identical:
			@import url("base-theme.css");
			@import "base-theme.css";
		

Relative URLs

In order to create modular style sheets that are not dependent on the absolute location of a resource, authors should use relative URLs. Relative URLs (as defined in [[!URL]]) are resolved to full URLs using a base URL. RFC 3986, section 3, defines the normative algorithm for this process. For CSS style sheets, the base URL is that of the style sheet itself, not that of the styled source document. Style sheets embedded within a document have the base URL associated with their container. When a <> appears in the computed value of a property, it is resolved to an absolute URL, as described in the preceding paragraph. The computed value of a URL that the UA cannot resolve to an absolute URL is the specified value.
For example, suppose the following rule:
body { background: url("tile.png") }
is located in a style sheet designated by the URL:
http://www.example.org/style/basic.css
The background of the source document's <body> will be tiled with whatever image is described by the resource designated by the URL:
http://www.example.org/style/tile.png
The same image will be used regardless of the URL of the source document containing the <body>.
Fragment URLs
To work around some common eccentriticites in browser URL handling, CSS has special behavior for fragment-only urls. If a ''url()''’s value starts with a U+0023 NUMBER SIGN (#) character, parse it as per normal for URLs, but additionally set the local url flag of the ''url()''. When matching a ''url()'' with the local url flag set, ignore everything but the URL's fragment, and resolve that fragment against the current document that relative URLs are resolved against. This reference must always be treated as same-document (rather than cross-document). When serializing a ''url()'' with the local url flag set, it must serialize as just the fragment.
What “browser eccentricities”? Theoretically, browsers should re-resolve any relative URLs, including fragment-only URLs, whenever the document's base URL changes (such as through mutation of the <{base}> element, or calling {{History/pushState()}}). In many cases they don't, however, and so without special handling, fragment-only URLs will suddenly become cross-document references (pointing at the previous base URL) and break in many of the places they're used. Since fragment-only URLs express a clear semantic of wanting to refer to the current document regardless of what its current URL is, this hack preserves the expected behavior at least in these cases.

Empty URLs

If the value of the ''url()'' is the empty string (like ''url("")'' or ''url()''), the url must resolve to an invalid resource (similar to what the url ''about:invalid'' does). Note: This matches the behavior of empty urls for embedded resources elsewhere in the web platform, and avoids excess traffic re-requesting the stylesheet or host document due to editting mistakes leaving the ''url()'' value empty, which are almost certain to be invalid resources for whatever the ''url()'' shows up in. Linking on the web platform does allow empty urls, so if/when CSS gains some functionality to control hyperlinks, this restriction can be relaxed in those contexts.

URL Modifiers

The ''url()'' function supports specifying additional <url-modifier>s, which change the meaning or the interpretation of the URL somehow. A <> is either an <> or a functional notation. This specification does not define any <>s, but other specs may do so. Note: A <> that is either unquoted or not wrapped in ''url()'' notation cannot accept any <>s.

Numeric Data Types

Numeric data types are used to represent quantities, indexes, positions, and other such values. Although many syntactic variations can exist in expressing the quantity (numeric aspect) in a given numeric value, the specified and computed value do not distinguish these variations: they represent the value’s abstract quantity, not its syntactic representation. Properties may restrict numeric values to some range. If the value is outside the allowed range, the declaration is invalid and must be ignored. CSS theoretically supports infinite precision and infinite ranges for all value types; however in reality implementations have finite capacity. UAs should support reasonably useful ranges and precisions.

Integers: the <> type

Integer values are denoted by <integer>. When written literally, an integer is one or more decimal digits ''0'' through ''9'' and corresponds to a subset of the <> production in the CSS Syntax Module [[!CSS3SYN]]. The first digit of an integer may be immediately preceded by - or + to indicate the integer's sign.

Combination of <>

Interpolation of <> is defined as Vresult = round((1 - p) × Va + p × Vb); that is, interpolation happens in the real number space as for <>s, and the result is converted to an <> by rounding to the nearest integer, with values halfway between adjacent integers rounded towards positive infinity. Addition of <> is defined as Vresult = Va + Vb

Real Numbers: the <> type

Number values are denoted by <number>, and represent real numbers, possibly with a fractional component. When written literally, a number is either an integer, or zero or more decimal digits followed by a dot (.) followed by one or more decimal digits and optionally an exponent composed of "e" or "E" and an integer. It corresponds to the <> production in the CSS Syntax Module [[!CSS3SYN]]. As with integers, the first character of a number may be immediately preceded by ''-'' or ''+'' to indicate the number's sign. The value <zero> represents a literal number with the value 0. Expressions that merely evaluate to a <> with the value 0 (for example, ''calc(0)'') do not match <>; only literal <>s do.

Combination of <>

Interpolation of <> is defined as Vresult = (1 - p) × Va + p × Vb Addition of <> is defined as Vresult = Va + Vb

Numbers with Units: dimension values

The general term dimension refers to a number with a unit attached to it; and is denoted by <dimension>. When written literally, a dimension is a number immediately followed by a unit identifier, which is an identifier. It corresponds to the <> production in the CSS Syntax Module [[!CSS3SYN]]. Like keywords, unit identifiers are ASCII case-insensitive. CSS uses <>s to specify distances (<>), durations (<