CSS3 module: Values and Units

W3C Working Draft, 13 July 2001

This version:
http://www.w3.org/TR/2001/WD-css3-values-20010713
Latest version:
http://www.w3.org/TR/css3-values
Previous version:
-
Editors:
Håkon Wium Lie, Opera Software, howcome@opera.com
Chris Lilley, W3C, chris@w3.org

Abstract

This CSS3 module describes the various values and units that CSS properties accept. Also, it describes how "specified values", which is what a style sheet contains, are processed into "computed values" and "actual values".

Status of this document

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.

All the properties and features described here that also exist in CSS level 2 are intended to be backwards compatible. (There are actually no new features in this draft, it only serves to rewrite the relevent parts of CSS level 2 in the form of a CSS3 module, but in future versions there will be.)

This draft should not be cited except as "work in progress." It is a work item of the CSS working group and part of the Style activity. It may be modified or dropped altogether at any point in time. Implementations for the purpose of experimenting with the specification are welcomed, as long as they are clearly marked as experimental.

Feedback on this draft is invited. The preferred place for discussion is the (archived) public mailing list www-style@w3.org. W3C members can also send comments to the CSS WG mailing list.

A list of current W3C Recommendations and other technical documents including Working Drafts and Notes can be found at http://www.w3.org/TR.

Table of contents


1. Dependencies on other modules

This CSS3 module depends on the following other CSS3 modules:

2. Introduction

There are five main types of values in CSS:

  1. keywords (e.g. "pitch-range: inherit")
  2. numbers (e.g. "orphans: 3")
  3. numbers with unit identifiers (e.g. "border-width: 0.2em")
  4. strings (e.g. "content: 'Figure: '")
  5. functions (e.g. "background: url(http://www.w3.org/image)")
  6. special cases (e.g. "color: #F00" and "font-size: Helvetica")

Most properties accepts values from several of the above types. Some properties accept space- or comma-separated lists of values.

Each CSS property has a formal definition of what types of values it accepts which can be found in the description of the property.

[add text about why the formal grammar of CSS differs from the grouping of values above]

3. Values

3.1. Keywords

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 | inherit

And here is an example of its use:

table { border-collapse: separate }

All CSS3 properties accept the keyword value "inherit". Keywords are never quoted.

3.2. Numbers

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.

Note that many properties that allow an integer or real number as a value actually restrict the value to some range, often to a non-negative value.

3.3. Numbers with unit identifiers

3.3.1. Lengths

Lengths refer to horizontal or vertical measurements.

The format of a length value (denoted by <length>) is an optional sign character ('+' or '-', with '+' being the default) immediately followed by a <number>(with or without a decimal point) immediately followed by a unit identifier (e.g., px, deg, etc.). 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 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:

Example(s):

H1 { margin: 0.5em }      /* em */
H1 { margin: 1ex }        /* ex */
P  { font-size: 12px }    /* px */

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. It may be used for vertical or horizontal measurement. (This unit is also sometimes called the quad-width in typographic texts.)

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".

Example(s):

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 H1 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.

When specified for the root of the document tree (e.g., "HTML" in HTML), 'em' and 'ex' refer to the property's initial value.

Pixel units are relative to the resolution of the viewing device, i.e., most often a computer display. 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 90dpi 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.0227 degrees.

For reading at arm's length, 1px thus corresponds to about 0.28 mm (1/90 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.21 mm. 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 71 cm (28 inch) results in a px of 0.28 mm, while a reading distance of 3.5 m (12 feet) requires a px of 1.4 mm.

Showing that pixels must become larger if the viewing distance
increases

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).

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

Child elements do not inherit the relative values specified for their parent; they (generally) inherit the computed values.

Example(s):

In the following rules, the computed 'text-indent' value of H1 elements will be 36pt, not 45pt, if H1 is a child of the BODY element.

BODY {
  font-size: 12pt;
  text-indent: 3em;  /* i.e., 36pt */
}
H1 { font-size: 15pt }

Absolute lengthunits are only useful when the physical properties of the output medium are known. The absolute units are:

Example(s):

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.

3.3.2. Percentages

The format of a percentage value (denoted by <percentage> in this specification) is an optional sign character ('+' or '-', with '+' being the default) immediately followed by 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.

Example(s):

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' */

3.3.3. Angles

Angle values (denoted by <angle> in the text) are used with aural style sheets [add ref].

Their format is an optional sign character ('+' or '-', with '+' being the default) immediately followed by a <number> immediately followed by an angle unit identifier.

Angle unit identifiers are:

Angle values may be negative. They 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'.

3.3.4. Times

Time values (denoted by <time> in the text) are used with aural style sheets [add ref].

Their format is a <number> immediately followed by a time unit identifier.

Time unit identifiers are:

Time values may not be negative.

3.3.5. Frequencies

Frequency values (denoted by <frequency> in the text) are used with aural cascading style sheets [add ref].

Their format is a <number> immediately followed by a frequency unit identifier.

Frequency unit identifiers are:

Frequency values may not be negative.

For example, 200Hz (or 200hz) is a bass sound, and 6kHz (or 6khz) is a treble sound.

3.4. Strings

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").

Example(s):

"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, 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 aesthetic or other reasons, but in such a case the newline itself has to be escaped with a backslash (\). 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"] {/*...*/}

3.5. Functions

3.5.1. Attribute identifiers

[describe attr(x) etc]

3.5.2. URIs

URLs(Uniform Resource Locators, see [RFC1738] and [RFC1808]) provide the address of a resource on the Web. An expected new way of identifying resources is called URN(Uniform Resource Name). Together they are called URIs(Uniform Resource Identifiers, see [URI]). This specification uses the term URI.

URI values in this specification are denoted by <uri>. The functional notation used to designate URIs in property values is "url()", as in:

Example(s):

BODY { background: url("http://www.bg.com/pinkish.gif") }

The format of a URI value is 'url(' followed by optional whitespace followed by an optional single quote (') or double quote (") character followed by the URI itself, followed by an optional single quote (') or double quote (") character followed by optional whitespace followed by ')'. The two quote characters must be the same.

Example(s):

An example without quotes:

LI { list-style: url(http://www.redballs.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.

Example(s):

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.

3.5.3. Counters

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, 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.

Example(s):

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) ". "}

Counters that are not in the scope [add ref] of any 'counter-reset', are assumed to have been reset to 0 by a 'counter-reset' on the root element.

3.6. Special cases

[There are two common cases that fall outside the types described above: color (due to the # notation) and font families (due to missing quotes being legal)]

3.6.1. Font families

[Font families are like strings, except one may drop the quotes around them]

3.6.2. Colors

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 list of keyword color names is: aqua, black, blue, fuchsia, gray, green, lime, maroon, navy, olive, purple, red, silver, teal, white, and yellow. These 16 colors are defined in HTML 4.0 ([HTML40]). In addition to these color keywords, users may specify keywords that correspond to the colors used by certain objects in the user's environment. Please consult the section on system colors [add ref] for more information.

Example(s):

BODY {color: black; background: white }
H1 { color: maroon }
H2 { color: olive }

The RGB color model is used in numerical color specifications. These examples all specify the same color:

Example(s):

EM { color: #f00 }              /* #rgb */
EM { color: #ff0000 }           /* #rrggbb */
EM { color: rgb(255,0,0) }      /* integer range 0 - 255 */
EM { color: rgb(100%, 0%, 0%) } /* float range 0.0% - 100.0% */

The format of an RGB value in hexadecimal notation is a '#' immediately followed by either three or six hexadecimal characters. The three-digit RGB notation (#rgb) is converted into six-digit form (#rrggbb) by replicating digits, not by adding zeros. For example, #fb0 expands to #ffbb00. This ensures that white (#ffffff) can be specified with the short notation (#fff) and removes any dependencies on the color depth of the display.

The format of an RGB value in the functional notation is 'rgb(' followed by a comma-separated list of three numerical values (either three integer values or three percentage values) followed by ')'. The integer value 255 corresponds to 100%, and to F or FF in the hexadecimal notation: rgb(255,255,255) = rgb(100%,100%,100%) = #FFF. Whitespace characters are allowed around the numerical values.

All RGB colors are specified in the sRGB color space (see [SRGB]). User agents may vary in the fidelity with which they represent these colors, but using sRGB provides an unambiguous and objectively measurable definition of what the color should be, which can be related to international standards (see [COLORIMETRY]).

Conforming user agents may limit their color-displaying efforts to performing a gamma-correction on them. sRGB specifies a display gamma of 2.2 under specified viewing conditions. User agents should adjust the colors given in CSS such that, in combination with an output device's "natural" display gamma, an effective display gamma of 2.2 is produced. See the section on gamma correction [add ref] for further details. Note that only colors specified in CSS are affected; e.g., images are expected to carry their own color information.

Values outside the device gamut should be clipped: the red, green, and blue values must be changed to fall within the range supported by the device. For a typical CRT monitor, whose device gamut is the same as sRGB, the three rules below are equivalent:

Example(s):

EM { color: rgb(255,0,0) }       /* integer range 0 - 255 */
EM { color: rgb(300,0,0) }       /* clipped to rgb(255,0,0) */
EM { color: rgb(255,-10,0) }     /* clipped to rgb(255,0,0) */
EM { color: rgb(110%, 0%, 0%) }  /* clipped to rgb(100%,0%,0%) */

Other devices, such as printers, have different gamuts to sRGB; some colors outside the 0..255 sRGB range will be representable (inside the device gamut), while other colors inside the 0..255 sRGB range will be outside the device gamut and will thus be clipped.

Note. Although colors can add significant amounts of information to document and make them more readable, please consider that certain color combinations may cause problems for users with color blindness. If you use a background image or set the background color, please adjust foreground colors accordingly.

4. Specified, computed, and actual values

The final value of a CSS3 property for a given element is the result of a three-step calculation: a value is specified in a style sheet (the "specified value"), then resolved into an absolute value if necessary (the "computed value"), and finally transformed according to the limitations of the local environment (the "actual value").

4.1. Specified values

A specified value is a value which is specified in the style sheet. The "winning declaration" contains a specified value. Often, the specified value does not need any computation, this is the case in example a in the table below.

If a specified 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 specified value. See example g in the table below.

4.2. Computed values

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', and '12%'). For absolute values, no computation is needed to find the computed value.

Relative values, on the other hand, must be transformed into 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 ('smaller', 'bolder', 'inherit') must be replaced according to their definitions. See example c, d and e in the table below.

Unless otherwise specified in the description of the property, child elements inherit the computed value from their parent.

In order to find the computed values the width of the "initial containing block" and the font size of the the root element must be known. Since lines have not yet been laid out, some computed values will remain "auto". See example f in the table below.

4.3. Actual values

A computed 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 h and i in the table below.

Example Winning declaration Property Specified value Computed value Actual value
a text-align: left text-align left left left
b border-width: inherit border-top-width, border-right-width, border-bottom-width, border-left-width inherit 4.2px 4px
c font-size: 1.2em font-size 1.2em 14.1px 14px
d width: 80% width 80% 354.2px 354px
e width: auto width auto 134px 134px
f height: auto height auto auto 176px
g (no winning declaration) width (no specified value) auto (since this is the initial value) 120px
h (no winning declaration) page-break-after (no specified value) auto (since this is the initial value) auto
i orphans: 3 orphans 3 3 3

Acknowledgments

[acknowledgments]

References

[COLORIMETRY]
Colorimetry, Second Edition. CIE Publication 15.2-1986. ISBN 3-900-734-00-3 URL: http://www.hike.te.chiba-u.ac.jp/ikeda/CIE/publ/abst/15-2-86.html
[HTML40]
Raggett, D.; Le Hors, A.; Jacobs, I.. HTML 4.0 Specification. 8 July 1997. W3C Recommendation. URL: http://www.w3.org/TR/REC-html40
[RFC1738]
T. Berners-Lee; L. Masinter; M. McCahill. Uniform Resource Locators (URL). December 1994. Internet RFC 1738. URL: ftp://ftp.nordu.net/rfc/rfc1738.txt
[RFC1808]
R. Fielding. Relative Uniform Resource Locators. June 1995. Internet RFC 1808. URL: ftp://ftp.nordu.net/rfc/rfc1808.txt
[SRGB]
Multimedia systems and equipment - Colour measurement and management - Part 2-1: Colour management - Default RGB colour space - sRGB. IEC 61966-2-1 (1999-10). ISBN: 2-8318-4989-6 ­ ICS codes: 33.160.60, 37.080 ­ TC 100 ­ 51 pp. URL: http://www.iec.ch/nr1899.htm
[URI]
T. Berners-Lee; R. Fielding; L. Masinter. Uniform Resource Identifiers (URI): generic syntax. August 1998. Internet RFC 2396. URL: http://www.isi.edu/in-notes/rfc2396.txt

Index