Copyright © 2012 W3C® (MIT, ERCIM, Keio), All Rights Reserved. W3C liability, trademark and document use rules apply.
This CSS3 module describes the common values and units that CSS properties accept and the syntax used for describing them in CSS property definitions.
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 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 www-style@w3.org (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.
This specification is a Last Call Working Draft. All persons are encouraged to review this document and send comments to the www-style mailing list as described above. The deadline for comments is 29 March 2012.
All features described in this specification that also exist in CSS 2.1 [CSS21] are intended to be backwards compatible. If you notice a conflict between this draft and CSS 2.1 [CSS21], please inform the editors!
The following features are at-risk and may be dropped during the CR
period: ‘calc()
’,
‘cycle()
’,
‘attr()
’.
initial
’ and
‘inherit
’
<identifier>
’ type
<string>
’ type
<url>
’ type
<integer>
’ type
<number>
’ type
<percentage>
’ type
<length>
’ type
em
’,
‘ex
’, ‘ch
’, ‘rem
’
units
vw
’, ‘vh
’,
‘vmin
’ units
cm
’, ‘mm
’, ‘in
’,
‘pt
’, ‘pc
’, ‘px
’
units
<angle>
’ type and
‘deg
’, ‘grad
’, ‘rad
’,
‘turn
’ units
<time>
’ type and ‘s
’, ‘ms
’
units
<frequency>
’ type and
‘Hz
’, ‘kHz
’ units
dpi
’, ‘dpcm
’, and ‘dppx
’ units
<color>
’ type
<image>
’ type
<position>
’ type
calc()
’
cycle()
’
attr()
’
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 (<length>
being the most widely
used) that can be used by many properties are described in this
specification, while more specific data types (e.g.,
<spacing-limit>
) are described in the corresponding
modules.
This module replaces and extends the data type definitions in [CSS21] sections 1.4.2.1, 4.3, and A.2.
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 are designated in several ways:
auto
’, ‘disc
’,
etc.), which appear literally, without quotes (e.g. auto
)
<
’ and ‘>
’ (e.g., <length>
, <percentage>
, etc.).
<'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
’.
<
’ and ‘>
’, as in <spacing-limit>
.
Notice the distinction between <border-width>
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 located near its first appearance in the specification.
Some property value definitions also include the slash (/) and/or the comma (,) as literals. These represent their corresponding tokens.
All CSS properties also accept the keyword values ‘inherit
’ and ‘initial
’ as their property value, but for
readability these are not listed explicitly in the property value syntax
definitions. These keywords cannot be combined with other component values
in same declaration; such a declaration is invalid. For example,
‘"Gill Sans", Futura,
sans-serif
’
border-width
’
2px medium 4px
’
text-shadow
’
3px 3px rgba(50%, 50%, 50%, 50%),
lemonchiffon 0 0 4px inset
’
voice-pitch
’
<frequency> && absolute | [[x-low | low | medium | high | x-high] || [<frequency> | <semitones> | <percentage>]]
-2st x-low
’
An identifier is a sequence of characters
conforming to the IDENT
token in the grammar.
[CSS21] Identifiers
cannot be quoted; otherwise they would be interpreted as a string.
In the value definition fields, keywords with a pre-defined meaning appear literally. Keywords are CSS identifiers and are interpreted case-insensitively within the ASCII range (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 }
initial
’ and ‘inherit
’As defined above, all properties accept
the ‘initial
’ and ‘inherit
’ keywords, which represent value
computations common to all CSS properties.
The ‘inherit
’
keyword is defined
in [CSS21].
The ‘initial
’
keyword represents the specified value that is designated as the
property's initial value. [CSS3CASCADE]
<identifier>
’ typeSome properties accept arbitrary user-defined identifiers as a component
value. This generic data type is denoted by <identifier>
, and represents
any valid CSS identifier that does not
otherwise appear as a pre-defined keyword in that property's value
definition. Such identifiers are fully case-sensitive, even in the ASCII
range (e.g. ‘example
’ and ‘EXAMPLE
’ are two different, unrelated user-defined
identifiers).
<string>
’ typeStrings are denoted by <string>
and consist of a
sequence of characters delimited by double quotes or single quotes. They
correspond to the STRING
token in the grammar.
[CSS21]
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:
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.)
<url>
’ typeA URL is a pointer to a resource and is a specially-parsed functional notation denoted by <url>
. It corresponds to the
URI
token in the grammar.
[CSS21]
Below is an example of a URL being used as a background image:
body { "www.example.com/pinkish.gif") }
The same example can be written without quotes:
body { ") appearing in a URL must be escaped with a backslash so that the resulting value is a validURL
token, e.g. ‘url(open\(parens)
’, ‘url(close\)parens)
’. Depending on the type of URL, it might also be possible to write these characters as URI-escapes (e.g. ‘url(open%28parens)
’ or ‘url(close%29parens)
’) as described in [URI]. Alternatively a URL containing such characters may be represented as a quoted string within the ‘url()
’ notation.In order to create modular style sheets that are not dependent on the absolute location of a resource, authors should use relative URIs. Relative URIs (as defined in [URI]) are resolved to full URIs using a base URI. RFC 3986, 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.
When a <url> appears in the computed value of a property, it is resolved to an absolute URL, as described in the preceding paragraph.
For example, suppose the following rule:
body { "tile.png") }is located in a style sheet designated by the URL:
www.example.org/style/basic.cssThe background of the source document's
<body>
will be tiled with whatever image is described by the resource designated by the URL:www.example.org/style/tile.pngThe same image will be used regardless of the URL of the source document containing the
<body>
.4. Numeric Data Types
4.1. Integers: the ‘
<integer>
’ typeInteger values are denoted by
<integer>
. An integer is one or more decimal digits ‘0
’ through ‘9
’ and corresponds to a subset of theNUMBER
token in the grammar. Integers may be immediately preceded by ‘-
’ or ‘+
’ to indicate the sign.Properties may restrict the integer value to some range. If the value is outside the allowed range, the declaration is invalid and must be ignored. For unrestricted values, UAs must support at least up to ±230; unsupported values must be clamped to the closest supported value.
4.2. Numbers: the ‘
<number>
’ typeNumber values are denoted by
<number>
. A number is either an integer, or zero or more decimal digits followed by a dot (.) followed by one or more decimal digits. It corresponds to theNUMBER
token in the grammar. Like integers, numbers may also be immediately preceded by ‘-
’ or ‘+
’ to indicate the sign.Properties may restrict the number value to some range. If the value is outside the allowed range, the declaration is invalid and must be ignored. For unrestricted values, UAs must support at least up to ±230; unsupported values must be clamped to the closest supported value.
4.3. Percentages: the ‘
<percentage>
’ typeA percentage value is denoted by
<percentage>
, consists of a <number> immediately followed by a percent sign ‘%
’. It corresponds to thePERCENTAGE
token in the grammar.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.
Properties may restrict the percentage value to some range. If the value is outside the allowed range, the declaration is invalid and must be ignored. For unrestricted values, UAs must support at least up to ±230%; unsupported values must be clamped to the closest supported value.
5. Distance Units: the ‘
<length>
’ typeLengths refer to distance measurements and are denoted by
<length>
in the property definitions. A length is a dimension. A zero length may be represented instead as the<number>
‘0
’. (In other words, for zero lengths the unit identifier is optional.)A dimension is a number immediately followed by a unit identifier. It corresponds to the
DIMENSION
token in the grammar. [CSS21] Like keywords, unit identifiers are case-insensitive within the ASCII range.Properties may restrict the length value to some range. If the value is outside the allowed range, the declaration is invalid and must be ignored.
While some properties allow negative length values, this may complicate the formatting and there may be implementation-specific limits. If a negative length value is allowed but cannot be supported, it must be converted to the nearest value that can be supported.
In cases where the used length cannot be supported, user agents must approximate it in the actual value.
There are two types of length units: relative and absolute.
5.1. Relative lengths
Relative length units specify a length relative to another length. Style sheets that use relative units can more easily scale from one output environment to another.
The relative units are:
unit | relative to |
---|---|
‘em ’
| font size of the element |
‘ex ’
| x-height of the element's font |
‘ch ’
| width of the "0" glyph in the element's font |
‘rem ’
| font size of the root element |
‘vw ’
| viewport's width |
‘vh ’
| viewport's height |
‘vmin ’
| minimum of the viewport's height and width |
unit | definition |
---|---|
‘cm ’
| centimeters |
‘mm ’
| millimeters |
‘in ’
| inches; 1in is equal to 2.54cm |
‘px ’
| pixels; 1px is equal to 1/96th of 1in |
‘pt ’
| points; 1pt is equal to 1/72nd of 1in |
‘pc ’
| picas; 1pc is equal to 12pt |
h1 { margin: 0.5in } /* inches */ h2 { line-cm } /* centimeters */ h3 { word-spacing: 4mm } /* millimeters */ h4 { pt } /* points */ h4 { pc } /* picas */ p { px } /* px */
For a CSS device, these dimensions are either anchored (i) by relating the physical units to their physical measurements, or (ii) by relating the pixel unit to the reference pixel. For print media and similar high-resolution devices, the anchor unit should be one of the standard physical units (inches, centimeters, etc). For lower-resolution devices, and devices with unusual viewing distances, it is recommended instead that the anchor unit be the pixel unit. For such devices it is recommended that the pixel unit refer to the whole number of device pixels that best approximates the reference pixel.
Note that if the anchor unit is the pixel unit, the physical units might not match their physical measurements. Alternatively if the anchor unit is a physical unit, the pixel unit might not map to a whole number of device pixels.
Note that this definition of the pixel unit and the physical units differs from previous versions of CSS. In particular, in previous versions of CSS the pixel unit and the physical units were not related by a fixed ratio: the physical units were always tied to their physical measurements while the pixel unit would vary to most closely match the reference pixel. (This change was made because too much existing content relies on the assumption of 96dpi, and breaking that assumption breaks the content.)
The reference pixel is 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.26 mm (1/96 inch).
The image below illustrates the effect of viewing distance on the size of a reference pixel: a reading distance of 71 cm (28 inches) results in a reference pixel of 0.26 mm, while a reading distance of 3.5 m (12 feet) results in a reference pixel of 1.3 mm.
Showing that pixels must become larger if the viewing distance increases
This second image illustrates the effect of a device's resolution on the pixel unit: an area of 1px by 1px is covered by a single dot in a low-resolution device (e.g. a typical computer display), while the same area is covered by 16 dots in a higher resolution device (such as a printer).
Showing that more device pixels (dots) are needed to cover a 1px by 1px area on a high-resolution device than on a low-res one
<angle>
’ type
and ‘deg
’, ‘grad
’, ‘rad
’, ‘turn
’ unitsAngle values are dimensions denoted by <angle>. The angle unit identifiers are:
For example, a right angle is ‘90deg
’
or ‘100grad
’ or ‘0.25turn
’ or approximately ‘1.570796326794897rad
’.
<time>
’ type and
‘s
’, ‘ms
’ unitsTime values are dimensions denoted by <time>. The time unit identifiers are: