Copyright � 2006 W3C� (MIT, ERCIM, Keio), All Rights Reserved. W3C liability, trademark and document use rules apply.
This CSS3 module describes the various values and units that CSS properties accept. Also, it describes how values are computed from "specified" (which is what the cascading process yields) through "computed" and "used" into "actual" values. The main purpose of this module is to define common values and units in one specification which can be referred to by other modules. As such, it does not make sense to claim conformance with this module alone.
This section describes the status of this document at the time of its publication. Other documents may supersede this document. A list of current W3C publications and the latest revision of this technical report can be found in the W3C technical reports index at http://www.w3.org/TR/.
Publication as a Working Draft does not imply endorsement by the W3C Membership. This is a draft document and may be updated, replaced or obsoleted by other documents at any time. It is inappropriate to cite this document as other than work in progress.
The (archived) public mailing list [email protected] (see instructions) is preferred for discussion of this specification. When sending e-mail, please put the text “css3-values” in the subject, preferably like this: “[css3-values] …summary of comment…”
This document was produced by the CSS Working Group (part of the Style Activity).
This document was produced by a group operating under the 5 February 2004 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.
All features described in this specification that also exist in CSS 2.1 [CSS21] are intended to be backwards compatible. In case of conflict between this draft and CSS�2.1 [CSS21], CSS�2.1 probably represents the intention of the CSS WG better than this draft (other than on values and units that are new to CSS3).
This is a draft of a module of CSS level 3. It will probably be bundled with some other modules before it becomes a W3C Recommendation.
This CSS3 module depends on the following other CSS3 modules:
There are eight main types of values in CSS:
'Figure: '
")
Should the special cases also be described in this draft?
Most properties accept values from several of the above types. Some properties accept space- or comma-separated lists of values. Comma-separated lists have optional whitespace before and after the comma.
Each CSS property has a formal definition of what types of values it accepts which can be found in the description of the property. For special case types not described in this module, the individual property descriptions should be consulted.
In the formal definition of property values, keywords appear literally.
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 }
All CSS3 properties accept the keyword values 'inherit' and 'initial'. Keywords must not be quoted.
Would it be useful to have a 'default' value, defined to be equivalent to 'inherit' for properties that are inherited by default and equivalent to 'initial' for properties that are not inherited by default? This might be easier for authors to use than 'initial' and 'inherit' since it wouldn't require thinking about whether a property is inherited by default or not (which isn't obvious for some properties, such as text-decoration and visibility).
Number values can either be integer or real numbers. Integer values are denoted by <integer> and real number values are denoted by <number>. Integers and real numbers are specified in decimal notation only. An <integer> consists of one or more digits "0" to "9". A <number> can either be an <integer>, or it can be zero or more digits followed by a dot (.) followed by one or more digits. Both integers and real numbers may be preceded by a "-" or "+" to indicate the sign, and the sign character is considered part of the <number> value.
Note that many properties that allow an integer or real number as a value actually restrict the value to some range. If the restriction is to a non-negative value, then a non-negative number type is used.
Non-negative number values can either be integer or real numbers. Integer values are denoted by <non-negative integer> and real number values are denoted by <non-negative number>. Integers and real numbers are specified in decimal notation only. A <non-negative-integer> consists of one or more digits "0" to "9". A <non-negative-number> can either be an <integer>, or it can be zero or more digits followed by a dot (.) followed by one or more digits. Both non-negative integers and non-negative real numbers may be preceded by a "+" to indicate the sign, but this is not required and conveys no useful information.
Lengths refer to horizontal or vertical measurements.
The format of a length value (denoted by <length>) is a <number> immediately
followed by a unit identifier (e.g., 'px',
'gd'). After the '0'
length, the unit identifier is optional.
Some properties allow negative length values, but this may complicate the formatting and there may be implementation-specific limits. If a negative length value is allowed but cannot be supported, it should be converted to the nearest value that can be supported.
There are two types of length units: relative and absolute. Relative length units specify a length relative to another length property. Style sheets that use relative units will more easily scale from one medium to another (e.g., from a computer display to a laser printer).
Relative units are:
unit | relative to |
---|---|
em | the font size of the element (or, to the parent element's font size if set on the 'font-size' property) |
ex | the x-height of the element's font |
px | viewing device |
gd | the grid defined by 'layout-grid' described in the CSS3 Text module [CSS3TEXT] |
rem | the font size of the root element |
vw | the viewport's width |
vh | the viewport's height |
vm | the viewport's height or width, whichever is smaller of the two |
ch | The width of the "0" (ZERO, U+0030) glyph found in the font for the font size used to render. If the "0" glyph is not found in the font, the average character width may be used. How is the "average character width" found? |
The 'em' unit is equal to the computed value of the 'font-size' property of the element on which it is used. The exception is when 'em' occurs in the value of the 'font-size' property itself, in which case it refers to the font size of the parent element. On the root element, It may be used for vertical or horizontal measurement. (This unit is also sometimes called the quad-width in typographic texts.)
The rule:
h1 { line-height: 1.2em }
means that the line height of h1
elements will be 20%
greater than the font size of the elements. On the other hand:
h1 { font-size: 1.2em }
means that the font size of h1
elements will be 20% greater
than the font size inherited by h1
elements.
h1 { margin: 0.5em } /* em */ h1 { margin: 1ex } /* ex */ p { font-size: 12px } /* px */
The 'ex' unit is defined by the font's x-height. The x-height is so called because it is often equal to the height of the lowercase "x". However, an 'ex' is defined even for fonts that don't contain an "x". Should we say that ex is 0.5em if no better value exists?
When specified on the 'font-size' property of the root element, 'em' and 'ex' units refer to the property's initial value.
The pixel unit, 'px' is relative to the resolution of the viewing device. For example, the viewing device can be a computer display or a printer. Normally, the pixel unit refers to physical pixels of the viewing device. However, if the pixel density of the output device is very different from that of a typical computer display, the user agent should rescale pixel values. It is recommended that the reference pixel be the visual angle of one pixel on a device with a pixel density of 96dpi and a distance from the reader of an arm's length. For a nominal arm's length of 28 inches, the visual angle is therefore about 0.0213 degrees.
For reading at arm's length, 1px thus corresponds to about 0.26mm (1/96�inch). When printed on a laser printer, meant for reading at a little less than arm's length (55�cm, 21�inches), 1px is about 0.21mm. On a 300 dots-per-inch (dpi) printer, that may be rounded up to 3 dots (0.25�mm); on a 600�dpi printer, it can be rounded to 5 dots.
The two images below illustrate the effect of viewing distance on the size of a pixel and the effect of a device's resolution. In the first image, a reading distance of 71cm (28�inch) results in a px of 0.26mm, while a reading distance of 3.5m (12�feet) requires a px of 1.3mm.
In the second image, an area of 1px by 1px is covered by a single dot in a low-resolution device (a computer screen), while the same area is covered by 16 dots in a higher resolution device (such as a 400�dpi laser printer).
Grid units, 'gd', are frequently used in East Asian typography, especially for margins. The existence of a grid in an element makes it possible to express various measurements in terms of grid units.
p { layout-grid: strict both 20pt 15pt; margin: 1gd 3gd 1gd 2gd }
The 'layout-grid' property, described in
the CSS3 Text module [CSS3TEXT], establishes the grid.
In the example above, p
elements acquire a 15pt
top margin, a 60pt right margin, a 15pt bottom margin and a 40pt left
margin.
If no grid is specified on the element, the 'gd' unit is treated as the 'em' unit.
The 'rem' unit ('root em') is relative to the computed value of the 'font-size' value of the root element. The exception is when 'rem' occurs in the value of the 'font-size' property of the root element itself, in which case it is relative to the 'medium' font-size. It may be used for vertical or horizontal measurement.
The 'vw' unit is relative to the viewport's width. The viewport's width is equal to 100 'vw' units.
h1 { font-size: 8vw }
If the width of the viewport is 200mm, the font size of h1
elements will be 16mm ((8�200)/100). When the
width of the viewport is changed (for example, when the browser window is
enlarged), lengths specifed in the 'vh' unit
is scaled propertionally.
The 'vh' unit is relative to the viewport's height. The viewport's width is equal to 100 'vh' units. When the height of the viewport is changed (for example, when the browser window is enlarged), lengths specifed in the 'vh' unit are scaled proportionally.
The 'vm' unit is relative to the viewport's height or width, whichever of the two is smaller. The minimum of the viewport's width/height is equivalent to 100 'vm' units. When the height or width of the viewport is changed, lengths specified in the 'vm' unit are scaled proportionally.
Absolute length units are useful when the physical properties of the output medium are known. The absolute units are:
unit | definition |
---|---|
in | inches; 1 inch is equal to 2.54 centimeters. |
cm | centimeters |
mm | millimeters |
pt | points; 1pt is equal to 1/72 inch. |
pc | picas; 1 pica is equal to 12 points. |
h1 { margin: 0.5in } /* inches */ h2 { line-height: 3cm } /* centimeters */ h3 { word-spacing: 4mm } /* millimeters */ h4 { font-size: 12pt } /* points */ h4 { font-size: 1pc } /* picas */
In cases where the specified length cannot be supported, user agents must approximate it in the actual value.
The format of a percentage value (denoted by <percentage> in this
specification) is a <number> immediately followed by '%'
.
Percentage values are always relative to another value, for example a length. Each property that allows percentages also defines the value to which the percentage refers. The value may be that of another property for the same element, a property for an ancestor element, or a value of the formatting context (e.g., the width of a containing block). When a percentage value is set for a property of the root element and the percentage is defined as referring to the inherited value of some property, the resultant value is the percentage times the initial value of that property.
Since child elements (generally) inherit the computed values of their parent, in the following example, the children of the P element will inherit a value of 12pt for 'line-height', not the percentage value (120%):
p { font-size: 10pt }
p { line-height: 120% } /* 120% of 'font-size' */
Do we need a "non-negative percentage", e.g. for "font-size"?
Angle values (denoted by <angle> in the text) are used with aural cascading style sheets.
Their format is a <number> immediately followed by an angle unit identifier.
Angle unit identifiers are:
Angle values should be normalized to the range 0-360deg by the user agent. For example, -10deg and 350deg are equivalent.
For example, a right angle is '90deg'
or '100grad'
or '1.570796326794897rad'
.
Time values (denoted by <time> in the text) are used with aural cascading style sheets.
Their format is a <number> immediately followed by a time unit identifier.
Time unit identifiers are:
Frequency values (denoted by <frequency> in the text) are used with aural cascading style sheets.
Their format is a <number> immediately followed by a frequency unit identifier.
Frequency unit identifiers are:
For example, 200Hz (or 200hz) is a bass sound, and 6kHz (or 6khz) is a treble sound.
Strings can
either be written with double quotes or with single quotes. Double quotes
cannot occur inside double quotes, unless escaped (as '\"'
or as '\22'
). Analogously for
single quotes ("\'" or "\27").
"this is a 'string'" "this is a \"string\""'this is a "string"'
'this is a \'
string\''
A string cannot directly contain 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). See the 'content' property for an example.
It is possible to break strings over several lines, for esthetic 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:
a[title="a not s\ o very long title"] {/*...*/} a[title="a not so very long title"] {/*...*/}
The functional notation is used to denote colors, attributes and URIs in CSS3. The syntax of the functional notation is: the name of the functions (e.g. 'rgb') followed by a left parenthesis followed by optional whitespace followed by the argument(s) to the functions followed by optional whitespace followed by a right parenthesis. If a function takes more than one argument, the arguments are separated by a comma (',') with optional whitespace before and after the comma.
In CSS2.1 [CSS21],
the 'attr()'
expression always returns a string. In
CSS3, the 'attr()'
expression can return many
different types. The new syntax for the attr() expression is:
'attr('
ident [','
<type> [','
<value> ]? ]?')'
The first argument represents the attribute name. The value of the attribute with that name on the element whose computed values are being computed is used as the value of the expression, according to the rules given below.
The first argument accepts an optional namespace prefix to identify the
namespace of the attribute. The namespace prefix and the attribute name is
separated by '|'
, with no whitespace before or
after the separator [CSS3NAMESPACE].
The second argument (which is optional but must be present if the third argument is present) is a <type> and tells the UA how to interpret the attribute value. It may be one of the values from the list below.
The third argument (which is optional) is a CSS value which must be valid where the attr() expression is placed. If it is not valid, then the whole attr() expression is invalid.
If the attribute named by the first argument is missing, cannot be parsed, or is invalid for the property, then the value returned by attr() will be the third argument, or, if the third argument is absent, will be the value given as the default for the relevant type in the list below.
'url()'
expression. The default is a UA-dependent URI
defined to point to a non-existent document with a generic error
condition. (i.e. it shouldn't be an FTP URI that causes a DNS error, or
an HTTP URI that results in a 404, it should be a non-descript error
condition.)
Should there also be a "keyword" type to, e.g., support
'float: attr(align)'
If the <type> is missing, 'string' is implied.
Ideally, it shouldn't be necessary to specify the type if it
is obvious. For example, this should be valid: "background-image:
attr(href);". This could be described as: If the property only
accepts one type of value (aside from 'inherit' and 'initial'), that type is implied
.
The attr() form is only valid if the type given (or implied, if it is missing) is valid for the property. For example, all of the following are invalid and would cause a parse-time error (and thus cause the relevant declaration, in this case all of them, to be ignored):
content: attr(title, color); /* 'content' doesn't accept colors */
content: attr(end-of-quote, string, inherit) close-quote; /* the
'inherit' value is not allowed there, since the result would be
'inherit close-quote'
, which is invalid. */
margin: attr(vertical, length) attr(horizontal, deg); /* deg
units are not valid at that point */
color: attr(color); /* 'color' doesn't accept strings */
The attr() expression cannot return everything, for example it cannot do
counters, named strings, quotes, or values such as 'auto', 'nowrap', or
'baseline'. This is intentional, as the intent
of the 'attr()'
expression is not to make it
possible to describe a presentational language's formatting using CSS, but
to enable CSS to take semantic data into account.
Note that the default value need not be of the type given. For instance,
if the type required of the attribute by the author is 'px', the default could still be '5em'
.
Examples:
<stock>
<wood length="12"/>
<wood length="5"/>
<metal length="19"/>
<wood length="4"/>
</stock>
stock::before {
display: block;
content: "To scale, the lengths of materials in stock are:";
}
stock > * {
display: block;
width: attr(length, em); /* default 0 */
height: 1em;
border: solid thin;
margin: 0.5em;
}
wood {
background: orange url(wood.png);
}
metal {
background: silver url(metal.png);
}
/* this also uses a possible extension to the 'content' property
to handle replaced content and alternatives to unavailable,
corrupted or unsupported content */
img {
content: replaced attr(src, url), attr(alt, string, none);
height: attr(height, px, auto);
width: attr(width, px, auto);
}
The attr() expression cannot currently fall back onto another attribute. Future versions of CSS may extend attr() in this direction.
Should 'attr()'
be allowed on any
property, in any source language? For example, do we expect UAs to honor
this rule for HTML documents?: P[COLOR] { color: attr(COLOR, color)
}.
URLs (Uniform Resource Locators, see [RFC1738] and [RFC1808]) provide the address of a resource on the Web. An alternative and more general term is URIs (Uniform Resource Identifiers, see [!URI]). This specification uses the term URI.
For historical reasons, the name of the URI function is "url". The URI function takes one URI as the argument. The URI may be quoted with single quote (') or double quote (") characters. If quoted, the two quote characters must be the same.
body { background: url("http://www.example.com/pinkish.gif") }
An example without quotes:
li { list-style: url(http://www.example.com/redball.png) disc }
Parentheses, commas, whitespace characters, single quotes (') and double
quotes (") appearing in a URI must be escaped with a backslash: '\('
, '\)'
, '\,'
.
Depending on the type of URI, it might also be possible to write the above characters as URI-escapes (where "(" = %28, ")" = %29, etc.) as described in [!URI].
In order to create modular style sheets that are not dependent on the absolute location of a resource, authors may use relative URIs. Relative URIs (as defined in [RFC1808]) are resolved to full URIs using a base URI. RFC�1808, section�3, defines the normative algorithm for this process. For CSS style sheets, the base URI is that of the style sheet, not that of the source document.
For example, suppose the following rule:
body { background: url("yellow") }
is located in a style sheet designated by the URI:
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 URI
http://www.example.org/style/yellow
User agents may vary in how they handle URIs that designate unavailable or inapplicable resources.
Counters are denoted by identifiers (see the 'counter-increment' and 'counter-reset' properties). To refer to the value of a counter, the notation 'counter(<identifier>)' or 'counter(<identifier>, <list-style-type>)' is used. The default style is 'decimal'.
To refer to a sequence of nested counters of the same name, the notation
is 'counters(<identifier>, <string>)'
or 'counters(<identifier>, <string>,
<list-style-type>)'
. See "Nested counters and scope" [add
ref] in the chapter on generated content [add ref].
In CSS2.1, the values of counters can only be referred to from the 'content' property. Note that 'none' is a possible <list-style-type>: 'counter(x, none)'
yields an empty string.
Here is a style sheet that numbers paragraphs (P) for each chapter (H1). The paragraphs are numbered with roman numerals, followed by a period and a space:
p { counter-increment: par-num } h1 { counter-reset: par-num } p:before { content: counter(par-num, upper-roman) ". " }
The calc(<expression>) function can be used wherever length values are allowed. The expression within the parethesis is computed at the same time as 'em' lengths are computed, with the syntax given below.
section { float: left; margin: 1em; border: solid 1px; width: calc(100%/3 - 2*1em - 2*1px); } p { margin: calc(1rem - 2px) calc(1rem - 1px); border: solid transparent; border-width: 2px 1px; } p:hover { border-color: yellow; }
The simple expression language of the 'calc()'
function supports five arithmetic operators (+ and - have lowest
precedence, *, /, and 'mod' have highest
precedence) and parentheses. At a later date new operators such as
min/max, conditionals, etc, and maybe new constants may be added.
The expression language is defined by 'length-expression' below:
<length> := calc( <length-expression> ) | <atomic-length> <length-expression> := <length-additive-expression> <length-additive-expression> := <length-multiplicative-expression> | <length-additive-expression>'+'
<length-multiplicative-expression> | <length-additive-expression>'-'
<length-multiplicative-expression> <length-multiplicative-expression> <length-term> | <length-multiplicative-expression>'*'
<number-term> | <number-multiplicative-expression>'*'
<length-term> | <length-multiplicative-expression>'/'
<number-term> | <length-multiplicative-expression> 'mod' <number-term> <length-term> :='('
<length-expression>')'
| <atomic-length> <number-additive-expression> := <number-multiplicative-expression> | <number-additive-expression>'+'
<number-multiplicative-expression> | <number-additive-expression>'-'
<number-multiplicative-expression> <number-multiplicative-expression> := <number-term> | <length-multiplicative-expression>'/'
<length-term> | <length-multiplicative-expression> 'mod' <length-term> <number-term> :='('
<number-additive-expression>')'
| <number> <atomic-length> := <number><length-unit>
'%'
for modulus since it is very easy to get
confused about whether '%'
is acting as a unit or
an operator. At least with 'mod' it always
causes a parse error — invalid unit — in the otherwise
ambiguous cases.Two common types of values fall outside the types described above: the value for font families and the hexadecimal color notation.
The 'font' and 'font-family' properties accept a comma-separated list of font families. Font families can either be the name of a certain font, or it can be one of five generic font families: 'serif', 'sans-serif', 'cursive','fantasy', and 'monospace'. Font family names are like strings, except that the quotes around them may be dropped but not always. Generic font family names are keywords and must not be quoted.
body { font-family: "Helvetica", "Univers", "Arial", sans-serif }
A color value can either be a keyword, a numerical RGB specification in a functional notation, or a numerical RGB specification in a hexadecimal notation. The hexadecimal notation is special shorthand format that allows compact color descriptions.
em { color: #F00 }
The CSS3 Color module defines the syntax of the hexadecimal notation.
The final value of a CSS3 property for a given element is the result of a four-step calculation: cascading and inheritance [!CSS3CASCADE] yields a value (the specified value), then relative values are computed into absolute values as far as possible without formatting the document (yielding the computed value), then the and finally transformed according to the limitations of the local environment (yielding the actual value).
If there are several competing declarations for a given element/property combination, the cascading process will select a winning declaration which contains the cascaded value. See the Cascading and Inheritance module [!CSS3CASCADE] for more information.
Often, the cascaded value will be equal to the specified, computed and actual values. This is the case in example (a) in the table below.
If a cascaded value is set on a shorthand property, it is considered to be set on all individual properties. See example (b) in the table below.
When there is no winning declaration, there is no cascaded value. See example (c) in the table below.
In the chain of computations from the cascaded value to the actual value, the specified value is the first value which is guaranteed to exist after inheritance has been performed. Furthermore, if the cascaded value is 'inherit' or 'initial', the specified value contains the inherited and initial value, respectively. See example (d) and (e) in the table below.
Specified values may be absolute (i.e., they are not specified relative
to another value, as in 'red' or '2mm'
) or relative (i.e., they are specified relative to
another value, as in 'auto', '2em'
). For absolute values, no processing is needed to
find the computed value.
For relative values, on the other hand, computation is necessary to find the computed values: percentages must be multiplied by a reference value (each property defines which value that is), values with relative units (em, ex, px) must be made absolute by multiplying with the appropriate font or pixel size, 'auto' values must be computed by the formulas given with each property, certain keywords (e.g., 'smaller', 'bolder') must be replaced according to their definitions. See example (f), (g) and (h) in the table below.
Also, relative URIs are computed into absolute URIs at this stage. The computed value of invalid and absolute URIs is the same as the specified value.
Computed values are processed as far as possible without formatting the document. Some values, however, can only be determined when the document is being laid out. For example, if the width of an element is set to be a certain percentage of its containing block, the width cannot be determine until the width of the containing block has been determined. The used value is the result of taking the computed value and resolving any remaining dependencies into an absolute value.
A used value is in principle ready to be used, but a user agent may not be able to make use of the value in a given environment. For example, a user agent may only be able to render borders with integer pixel widths and may therefore have to approximate the computed width. Also, the font size of an element may need adjustment based on the availability of fonts or the value of the 'font-size-adjust' property. The actual value is the computed value after adjustments have been made.
By probing the actual values of elements, much can be learned about how the document is laid out. However, not all information is recorded in the actual values. For example, the actual value of the 'page-break-after' property does not reflect whether there is a page break or not after the element. Similarly, the actual value of 'orphans' does not reflect how many orphan lines there is in a certain element. See examples (j) and (k) in the table below.
Example | Winning declaration | Property | Specified value | Computed value | Used value | Actual value |
---|---|---|---|---|---|---|
a | text-align: left | text-align | left | left | left | left |
b | border-width: inherit | border-top-width, border-right-width, border-bottom-width, border-left-width | 4.2px | 4.2px | 4.2px | 4px |
c | (no winning declaration) | width | auto (which is the initial value) | auto | 120px | 120px |
d | list-style-position: inherit | list-style-position | inside | inside | inside | inside |
e | list-style-position: initial | list-style-position | outside (which is the initial value) | outside | outside | outside |
f | font-size: 1.2em | font-size | 1.2em | 14.1px | 14.1px | 14px |
g | width: 80% | width | 80% | 80% | 354.2px | 354px |
h | width: auto | width | auto | auto | 134px | 134px |
i | height: auto | height | auto | auto | 176px | 176px |
j | (no winning declaration) | page-break-after | auto (which is the initial value) | auto | auto | auto |
k | orphans: 3 | orphans | 3 | 3 | 3 | 3 |
Comments and suggestions from Ian Hickson, David Baron, Edward Welbourne, Boris Zbarsky, Bj�rn H�hrmann and Michael Day improved this module.