E518 csswg-drafts/css2/syndata.html at 0df18e3695b863b9b4da44118a6fcfd7778dc21a · xfq/csswg-drafts · GitHub
Skip to content

Latest commit

 

History

History
1662 lines (1485 loc) · 73.5 KB

File metadata and controls

1662 lines (1485 loc) · 73.5 KB
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html lang="en">
<head>
<title>Syntax and basic data types</title>
<link rel="stylesheet" href="style/default.css" type="text/css">
<link rel="stylesheet" href="http://www.w3.org/StyleSheets/TR/W3C-ED.css" type="text/css">
<link rel="prev" href="conform.html">
<link rel="next" href="selector.html">
<link rel="contents" href="cover.html#minitoc">
<link rel="CSS-properties" href="propidx.html" title="properties">
<link rel="index" href="indexlist.html" title="index">
<link rel="first" href="cover.html">
<style type="text/css">
span.colorsquare { float:left; width:5em; height:3em; text-align:center; padding:1.2em 0 .8em }
span.colorname { font-weight:bold }
div.colordiagram { width:25em; height:20em; margin:1em auto; font-family:Verdana,sans-serif; font-size:12px }
div.diagramrow { height:5em }
</style>
<style type="text/css">
.current,.proposed,span.delcurrent { background:#feb }
ins.proposed,span.insproposed { background:#bfb }
del.proposed,span.delproposed { background:#fbb }
span.insproposed { text-decoration:underline }
span.delproposed,span.delcurrent { text-decoration:line-through }
body>del,body>ins {display:block}
</style>
</head>
<body>
<div class="navbar">
<p><a href="conform.html">previous</a> &nbsp;
<a href="selector.html">next</a> &nbsp;
<a href="cover.html#minitoc">contents</a> &nbsp;
<a href="propidx.html">properties</a> &nbsp;
<a href="indexlist.html">index</a> &nbsp;
</div>
<hr class="navbar">
<h1><a name="q0">4 Syntax and basic data types</a></h1>
<div class="subtoc">
<p><strong>Contents</strong>
<ul class="toc">
<li class="tocline2"><a href="syndata.html#syntax" class="tocxref">4.1 Syntax</a>
<ul class="toc">
<li class="tocline3"><a href="syndata.html#tokenization" class="tocxref">4.1.1 Tokenization</a>
<li class="tocline3"><a href="syndata.html#keywords" class="tocxref">4.1.2 Keywords</a>
<ul class="toc">
<li class="tocline4"><a href="syndata.html#vendor-keywords" class="tocxref">4.1.2.1 Vendor-specific extensions</a>
<li class="tocline4"><a href="syndata.html#vendor-keyword-history" class="tocxref">4.1.2.2 Informative Historical Notes</a>
</ul>
<li class="tocline3"><a href="syndata.html#characters" class="tocxref">4.1.3 Characters and case</a>
<li class="tocline3"><a href="syndata.html#statements" class="tocxref">4.1.4 Statements</a>
<li class="tocline3"><a href="syndata.html#at-rules" class="tocxref">4.1.5 <span class="index-def" title="at-rule"> At-rules</span></a>
<li class="tocline3"><a href="syndata.html#block" class="tocxref">4.1.6 Blocks</a>
<li class="tocline3"><a href="syndata.html#rule-sets" class="tocxref">4.1.7 Rule sets, declaration blocks, and selectors</a>
<li class="tocline3"><a href="syndata.html#declaration" class="tocxref">4.1.8 Declarations and properties</a>
<li class="tocline3"><a href="syndata.html#comments" class="tocxref">4.1.9 Comments</a>
</ul>
<li class="tocline2"><a href="syndata.html#parsing-errors" class="tocxref">4.2 Rules for handling parsing errors</a>
<li class="tocline2"><a href="syndata.html#values" class="tocxref">4.3 Values</a>
<ul class="toc">
<li class="tocline3"><a href="syndata.html#numbers" class="tocxref">4.3.1 Integers and real numbers</a>
<li class="tocline3"><a href="syndata.html#length-units" class="tocxref">4.3.2 Lengths</a>
<li class="tocline3"><a href="syndata.html#percentage-units" class="tocxref">4.3.3 Percentages</a>
<li class="tocline3"><a href="syndata.html#uri" class="tocxref">4.3.4 URLs and URIs</a>
<li class="tocline3"><a href="syndata.html#counter" class="tocxref">4.3.5 Counters</a>
<li class="tocline3"><a href="syndata.html#color-units" class="tocxref">4.3.6 Colors</a>
<li class="tocline3"><a href="syndata.html#strings" class="tocxref">4.3.7 Strings</a>
<li class="tocline3"><a href="syndata.html#unsupported-values" class="tocxref">4.3.8 Unsupported Values</a>
</ul>
<li class="tocline2"><a href="syndata.html#charset" class="tocxref">4.4 CSS style sheet representation</a>
<ul class="toc">
<li class="tocline3"><a href="syndata.html#escaping" class="tocxref">4.4.1 Referring to characters not represented in a character encoding</a>
</ul>
</ul>
</div>
<h2>4.1 <a name="syntax">Syntax</a></h2>
<p>This section describes a grammar (and <a name="x0"><span class="index-def"
title="forward-compatible parsing"><dfn>forward-compatible
parsing</dfn></span></a> rules) common to any level of CSS (including
CSS&nbsp;2.1). Future updates of CSS will adhere to this core syntax,
although they may add additional syntactic constraints.
</p>
<p>These descriptions are normative. They are also
complemented by the normative grammar rules presented in <a
href="grammar.html">Appendix G</a>.
</p>
<p>In this specification, the expressions "immediately before" or
"immediately after" mean with no intervening white space or comments.
<h3>4.1.1 <a name="tokenization">Tokenization</a></h3>
<p>All levels of CSS &mdash; level 1, level 2, and any future levels &mdash; use
the same core syntax. This allows UAs to parse (though not completely
understand) style sheets written in levels of CSS that did not exist at
the time the UAs were created. Designers can use this feature to
create style sheets that work with older user agents, while also
exercising the possibilities of the latest levels of CSS.
</p>
<p>At the lexical level, CSS style sheets consist of a sequence of tokens.
The list of tokens for CSS is as follows. The definitions use Lex-style
regular expressions. Octal codes refer to ISO 10646 (<a href="refs.html#ref-ISO10646" rel="biblioentry" class="noxref"><span class="normref">[ISO10646]</span></a>). As in
Lex, in case of multiple matches, the longest match determines the token.
</p>
<table>
<thead>
<tr><th>Token </th><th>Definition</th></tr>
</thead>
<tr><td colspan=2><hr></td></tr>
<tr><td>IDENT </td><td><code><var>{ident}</var></code></td></tr>
<tr><td>ATKEYWORD </td><td><code>@<var>{ident}</var></code></td></tr>
<tr><td>STRING </td><td><code><var>{string}</var></code></td></tr>
<tr><td>BAD_STRING </td><td><code><var>{badstring}</var></code></td></tr>
<tr><td>BAD_URI </td><td><code><var>{baduri}</var></code></td></tr>
<tr><td>BAD_COMMENT </td><td><code><var>{badcomment}</var></code></td></tr>
<tr><td>HASH </td><td><code>#<var>{name}</var></code></td></tr>
<tr><td>NUMBER </td><td><code><var>{num}</var></code></td></tr>
<tr><td>PERCENTAGE </td><td><code><var>{num}</var>%</code></td></tr>
<tr><td>DIMENSION </td><td><code><var>{num}{ident}</var></code></td></tr>
<tr><td>URI </td><td><code>{U}{R}{L}\(<var>{w}{string}{w}</var>\)|<br>
{U}{R}{L}\(<var>{w}</var>([!#$%&amp;*-\[\]-~]|<var>{nonascii}</var>|<var>{escape}</var>)*<var>{w}</var>\)</code></td></tr>
<tr><td>UNICODE-RANGE </td><td><code>u\+[?]{1,6}|<br>
u\+[0-9a-f]{1}[?]{0,5}|<br>
u\+[0-9a-f]{2}[?]{0,4}|<br>
u\+[0-9a-f]{3}[?]{0,3}|<br>
u\+[0-9a-f]{4}[?]{0,2}|<br>
u\+[0-9a-f]{5}[?]{0,1}|<br>
u\+[0-9a-f]{6}|<br>
u\+[0-9a-f]{1,6}-[0-9a-f]{1,6}</code></td></tr>
<tr><td>CDO </td><td><code>&lt;!--</code></td></tr>
<tr><td>CDC </td><td><code>--&gt;</code></td></tr>
<tr><td>: </td><td><code>:</code></td></tr>
<tr><td>; </td><td><code>;</code></td></tr>
<tr><td>{ </td><td><code>\{</code></td></tr>
<tr><td>} </td><td><code>\}</code></td></tr>
<tr><td>( </td><td><code>\(</code></td></tr>
<tr><td>) </td><td><code>\)</code></td></tr>
<tr><td>[ </td><td><code>\[</code></td></tr>
<tr><td>] </td><td><code>\]</code></td></tr>
<tr><td>S </td><td><code>[ \t\r\n\f]+</code></td></tr>
<tr><td>COMMENT </td><td><code>\/\*[^*]*\*+([^/*][^*]*\*+)*\/</code></td></tr>
<tr><td>FUNCTION </td><td><code><var>{ident}</var>\(</code></td></tr>
<tr><td>INCLUDES </td><td><code>~=</code></td></tr>
<tr><td>DASHMATCH </td><td><code>|=</code></td></tr>
<tr><td>DELIM </td><td><var>any other character not matched by
the above rules, and neither a single nor a double quote</var>
</td></tr></table>
<p>The macros in curly braces ({}) above are defined as follows:
</p>
<table>
<thead>
<tr><th>Macro </th><th>Definition</th></tr>
</thead>
<tr><td colspan=2><hr></td></tr>
<tr><td>ident </td><td><code>[-]?<var>{nmstart}</var><var>{nmchar}*</var></code></td></tr>
<tr><td>name </td><td><code><var>{nmchar}+</var></code></td></tr>
<tr><td>nmstart </td><td><code>[_a-z]|<var>{nonascii}</var>|<var>{escape}</var></code></td></tr>
<tr><td>nonascii</td><td><code>[^\0-\177]</code></td></tr>
<tr><td>unicode </td><td><code>\\[0-9a-f]{1,6}(\r\n|[ \n\r\t\f])?</code></td></tr>
<tr><td>escape </td><td><code><var>{unicode}</var>|\\[^\n\r\f0-9a-f]</code></td></tr>
<tr><td>nmchar </td><td><code>[_a-z0-9-]|<var>{nonascii}</var>|<var>{escape}</var></code></td></tr>
<tr><td>num </td><td><code>[+-]?([0-9]+|[0-9]*\.[0-9]+)(e[+-]?[0-9]+)?</code></td></tr>
<tr><td>string </td><td><code><var>{string1}</var>|<var>{string2}</var></code></td></tr>
<tr><td>string1 </td><td><code>\"([^\n\r\f\\"]|\\{nl}|<var>{escape}</var>)*\"</code></td></tr>
<tr><td>string2 </td><td><code>\'([^\n\r\f\\']|\\{nl}|<var>{escape}</var>)*\'</code></td></tr>
<tr><td>badstring </td><td><code><var>{badstring1}</var>|<var>{badstring2}</var></code></td></tr>
<tr><td>badstring1 </td><td><code>\"([^\n\r\f\\"]|\\{nl}|<var>{escape}</var>)*\\?</code></td></tr>
<tr><td>badstring2 </td><td><code>\'([^\n\r\f\\']|\\{nl}|<var>{escape}</var>)*\\?</code></td></tr>
<tr><td>badcomment </td><td><code><var>{badcomment1}</var>|<var>{badcomment2}</var></code></td></tr>
<tr><td>badcomment1 </td><td><code>\/\*[^*]*\*+([^/*][^*]*\*+)*</code></td></tr>
<tr><td>badcomment2 </td><td><code>\/\*[^*]*(\*+[^/*][^*]*)*</code></td></tr>
<tr><td>baduri </td><td><code><var>{baduri1}</var>|<var>{baduri2}</var>|<var>{baduri3}</var></code></td></tr>
<tr><td>baduri1 </td><td><code>{U}{R}{L}\(<var>{w}</var>([!#$%&amp;*-~]|<var>{nonascii}</var>|<var>{escape}</var>)*<var>{w}</var></code></td></tr>
<tr><td>baduri2 </td><td><code>{U}{R}{L}\(<var>{w}</var><var>{string}</var><var>{w}</var></code></td></tr>
<tr><td>baduri3 </td><td><code>{U}{R}{L}\(<var>{w}</var><var>{badstring}</var></code></td></tr>
<tr><td>nl </td><td><code>\n|\r\n|\r|\f</code></td></tr>
<tr><td>w </td><td><code>[ \t\r\n\f]*</code></td></tr>
<tr><td>L <td><code>l|\\0{0,4}(4c|6c)(\r\n|[ \t\r\n\f])?|\\l</code>
<tr><td>R & 298 lt;td><code>r|\\0{0,4}(52|72)(\r\n|[ \t\r\n\f])?|\\r</code>
<tr><td>U <td><code>u|\\0{0,4}(55|75)(\r\n|[ \t\r\n\f])?|\\u</code>
</table>
<div class="example"><P style="display:none">Example(s):</P>
<p>For example, the rule of the longest match means that
"<code>red--&gt;</code>" is tokenized as the IDENT "<code>red--</code>"
followed by the DELIM "<code>&gt;</code>", rather than as an IDENT
followed by a CDC.
</div>
<p>Below is the core syntax for CSS. The sections that follow describe
how to use it. <a href="grammar.html">Appendix G</a> describes a
more restrictive grammar that is closer to the CSS level 2 language.
Parts of style sheets that can be parsed according to this grammar but
not according to the grammar in Appendix G are among the parts that
will be ignored according to the <a href="#parsing-errors">rules for
handling parsing errors</a>.
</p>
<pre>
stylesheet : [ CDO | CDC | S | statement ]*;
statement : ruleset | at-rule;
at-rule : ATKEYWORD S* any* [ block | ';' S* ];
block : '{' S* [ any | block | ATKEYWORD S* | ';' S* ]* '}' S*;
ruleset : selector? '{' S* declaration? [ ';' S* declaration? ]* '}' S*;
selector : any+;
declaration : property S* ':' S* value;
property : IDENT;
value : [ any | block | ATKEYWORD S* ]+;
any : [ IDENT | NUMBER | PERCENTAGE | DIMENSION | STRING
| DELIM | URI | HASH | UNICODE-RANGE | INCLUDES
| DASHMATCH | ':' | FUNCTION S* [any|unused]* ')'
| '(' S* [any|unused]* ')' | '[' S* [any|unused]* ']'
] S*;
unused : block | ATKEYWORD S* | ';' S* | CDO S* | CDC S*;
</pre>
<p>The "unused" production is not used in CSS and will not be used by
any future extension. It is included here only to help with error
handling. (See <a href="#parsing-errors">4.2 "Rules for handling
parsing errors."</a>)
<p><a name="comment">COMMENT</a> tokens do not occur
in the grammar (to keep it readable), but any number of these tokens
may appear anywhere outside other tokens. (Note, however, that a
comment before or within the @charset rule disables the @charset.)</p>
<p>The token S in the grammar above stands for <a
name="whitespace">white space</a>. Only the characters "space" (U+0020), "tab" (U+0009), "line feed" (U+000A), "carriage return" (U+000D), and
"form feed" (U+000C) can occur in white space. Other space-like characters,
such as "em-space" (U+2003) and "ideographic space" (U+3000), are never part of white space.
</p>
<p>The meaning of input that cannot be tokenized or parsed is
undefined in CSS&nbsp;2.1.
</p>
<h3>4.1.2 <a name="keywords">Keywords</a></h3>
<p>Keywords have the form of <a
href="#value-def-identifier">identifiers.</a> Keywords must not be
placed between quotes ("..." or '...'). Thus,
</p>
<pre>
red
</pre>
<p>is a keyword, but
</p>
<pre>
"red"
</pre>
<p>is not. (It is a <a href="#strings">string</a>.) Other illegal examples:
<div class="illegal-example"><P style="display:none">Illegal example(s):</P>
<pre><code>
width: "auto";
border: "none";
background: "red";
</code></pre>
</div>
<h4>4.1.2.1 <a name="vendor-keywords">Vendor-specific extensions</a></h4>
<p>In CSS, identifiers may begin with '<code
class="css">-</code>' (dash) or '<code
class="css">_</code>' (underscore). Keywords
and <a href="#properties">property names</a> beginning
with <code class="css">-</code>' or '<code
class="css">_</code>' are reserved for vendor-specific extensions. Such vendor-specific extensions should have one of the following formats:
</p>
<pre>
'-' + vendor identifier + '-' + meaningful name
'_' + vendor identifier + '-' + meaningful name
</pre>
<div class="example"><P style="display:none">Example(s):</P>
<p>For example, if XYZ organization added a property to describe the color of the
border on the East side of the display, they might call it <span
class="css">-xyz-border-east-color</span>.
</p>
<p>Other known examples:</p>
<pre>
-moz-box-sizing
-moz-border-radius
-wap-accesskey
</pre>
</div>
<p>An initial dash or underscore is guaranteed never to be used in a property or keyword by any current or future level of CSS. Thus typical CSS implementations may not
recognize such properties and may ignore them according to the <a
href="#parsing-errors">rules for handling parsing errors</a>. However, because the initial dash or underscore is part of the grammar, CSS&nbsp;2.1 implementers should always be able to use a CSS-conforming parser, whether or not they support any vendor-specific extensions.
</p>
<p>Authors should avoid vendor-specific extensions</p>
<h4>4.1.2.2 <a name="vendor-keyword-history">Informative Historical Notes</a></h4>
<p>This section is informative.</p>
<p>At the time of writing, the following prefixes are known to exist:</p>
<table border='1'>
<thead><tr><th>prefix</th><th>organization</th></tr></thead>
<tbody>
<tr><td><code>-ms-&l 8892 t;/code>, <code>mso-</code></td><td>Microsoft</td></tr>
<tr><td><code>-moz-</code></td><td>Mozilla</td></tr>
<tr><td><code>-o-</code>, <code>-xv-</code></td><td>Opera Software</td></tr>
<tr><td><code>-atsc-</code></td><td>Advanced Television Standards Committee</td></tr>
<tr><td><code>-wap-</code></td><td>The WAP Forum</td></tr>
<tr><td><code>-khtml-</code></td><td>KDE</td></tr>
<tr><td><code>-webkit-</code></td><td>Apple</td></tr>
<tr><td><code>prince-</code></td><td>YesLogic</td></tr>
<tr><td><code>-ah-</code></td><td>Antenna House</td></tr>
<tr><td><code>-hp-</code></td><td>Hewlett Packard</td></tr>
<tr><td><code>-ro-</code></td><td>Real Objects</td></tr>
<tr><td><code>-rim-</code></td><td>Research In Motion</td></tr>
<tr><td><code>-tc-</code></td><td>TallComponents</td></tr>
</tbody>
</table>
<h3>4.1.3 <a name="characters">Characters and case</a></h3>
<p> The following rules always hold:</p>
<ul>
<li> All CSS syntax is <a name="x1"><span class="index-inst" title="case
sensitivity">case-insensitive</span></a> within the ASCII
range (i.e., [a-z] and [A-Z] are equivalent), except for parts that are
not under the control of CSS. For example, the case-sensitivity of
values of the HTML attributes "id" and "class", of font names, and
of URIs lies outside the scope of this specification. Note in
particular that element names are case-insensitive in HTML, but
case-sensitive in XML.
</li>
<li> In CSS, <span class="index-def"
title="identifier|identifier, definition of"><a
name="value-def-identifier"><dfn>identifiers</dfn></a></span>
(including element names, classes, and IDs in <a
href="selector.html">selectors</a>) can contain only the
characters [a-zA-Z0-9] and ISO 10646 characters U+0080 and higher,
plus the hyphen (-) and the underscore (_); they cannot start with
a digit, two hyphens, or a hyphen followed by a digit.
Identifiers can also contain escaped characters and any ISO 10646
character as a numeric code (see next item).
<span class="example">For instance, the identifier "B&amp;W?" may
be written as "B\&amp;W\?" or "B\26 W\3F".</span>
<p>Note that Unicode is code-by-code equivalent to ISO 10646 (see
<a href="refs.html#ref-UNICODE" rel="biblioentry" class="noxref"><span class="normref">[UNICODE]</span></a> and <a href="refs.html#ref-ISO10646" rel="biblioentry" class="noxref"><span class="normref">[ISO10646]</span></a>).
</p>
</li>
<li> In CSS&nbsp;2.1, a backslash (\) character can indicate one
of three types of <span class="index-def" title="backslash
escapes"> <a name="escaped-characters">character
escape.</a></span> Inside a CSS comment, a backslash stands for
itself, and if a backslash is immediately followed by the end of
the style sheet, it also stands for itself (i.e., a DELIM token).
<p>First, inside a <a href="#strings">string</a>, a backslash
followed by a newline is ignored (i.e., the string is deemed not
to contain either the backslash or the newline).
Outside a string, a backslash followed by a newline stands for
itself (i.e., a DELIM followed by a newline).
<p>Second, it cancels the meaning of special CSS characters.
Any character (except a
hexadecimal digit, linefeed, carriage return, or form feed)
can be escaped
with a backslash to remove its special meaning.
For example, <samp>"\""</samp> is a string consisting of one
double quote. Style sheet preprocessors must not remove
these backslashes from a style sheet since that would
change the style sheet's meaning.
</p>
<p>Third, backslash escapes allow authors to refer to characters
they cannot easily put in a document. In this case, the backslash
is followed by at most six hexadecimal digits (0..9A..F), which
stand for the ISO 10646 (<a href="refs.html#ref-ISO10646" rel="biblioentry" class="noxref"><span class="normref">[ISO10646]</span></a>)
character with that number, which must not be zero.
(It is undefined in CSS&nbsp;2.1 what happens if a style sheet
<em>does</em> contain a character with Unicode codepoint zero.)
If a character in the range [0-9a-fA-F] follows the hexadecimal number,
the end of the number needs to be made clear. There are two ways
to do that:
</p>
<ol>
<li>with a space (or other white space character): "\26 B" ("&amp;B").
In this case, user agents should treat a "CR/LF" pair
(U+000D/U+000A) as a single white space character.</li>
<li>by providing exactly 6 hexadecimal digits: "\000026B" ("&amp;B")</li>
</ol>
<p>In fact, these two methods may be combined. Only one white space
character is ignored after a hexadecimal escape. Note that this means
that a "real" space after the escape sequence must be
doubled.
</p>
<p>If the number is outside the range allowed by Unicode (e.g.,
"\110000" is above the maximum 10FFFF allowed in current Unicode),
the UA may replace the escape with the "replacement character"
(U+FFFD). If the character is to be displayed, the UA should show
a visible symbol, such as a "missing character" glyph (cf. <a
href="fonts.html#algorithm">15.2,</a> point 5).
</li>
<li class=note>Note: Backslash escapes are always
considered to be part of an <a
href="#value-def-identifier">identifier</a> or a string (i.e.,
"\7B" is not punctuation, even though "{" is, and "\32" is allowed
at the start of a class name, even though "2" is not).
<p>The identifier "te\st" is exactly the same identifier as "test".
</li>
</ul>
<h3>4.1.4 <a name="statements">Statements</a></h3>
<p> A CSS style sheet, for any level of CSS, consists of a list of
<a name="x5"><span class="index-inst" title="statements"><em>statements</em></span></a>
(see the <a href="#tokenization">grammar</a> above). There are two
kinds of statements: <a name="x6"><span
class="index-inst" title="at-rules"><em>at-rules</em></span></a>
and <a name="x7"><span class="index-inst" title="rule sets"><em>rule
sets.</em></span></a> There may be <a href="#whitespace">white space</a>
around the statements.
</p>
<h3>4.1.5 <span class="index-def" title="at-rule">
<a name="at-rules">At-rules</a></span></h3>
<p> At-rules start with an <dfn>at-keyword</dfn>, an '@' character
followed immediately by an <a
href="#value-def-identifier">identifier</a> (for example, '@import',
'@page').
</p>
<p> An at-rule consists of everything up to and including the next
semicolon (;) or the next <a href="#block">block,</a> whichever comes
first.
</p>
<p>CSS&nbsp;2.1 user agents must <span class="index-inst" title="ignore"><a name="x9"
href="#ignore">ignore</a></span> any <a
href="cascade.html#at-import">'@import'</a> rule that occurs inside a <a
href="#block">block</a> or after any non-ignored statement other than an <a name="x10"><span
class="index-inst">@charset</span></a> or an @import rule.
</p>
<div class="illegal-example"><P style="display:none">Illegal example(s):</P><p>
Assume, for example, that a CSS&nbsp;2.1 parser encounters this style sheet:
</p>
<pre><code>
@import "subs.css";
h1 { color: blue }
@import "list.css";
</code></pre>
<p> The second '@import' is illegal according to CSS&nbsp;2.1. The CSS&nbsp;2.1 parser
<span class="index-inst" title="ignore"><a name="x11" href="#ignore">ignores</a></span>
the whole at-rule, effectively reducing the style sheet to:
</p>
<pre><code class="css">
@import "subs.css";
h1 { color: blue }
</code></pre>
</div>
<div class="illegal-example"><P style="display:none">Illegal example(s):</P><p>
In the following example, the second '@import' rule is invalid,
since it occurs inside a '@media' <a href="#block">block</a>.
</p>
<pre><code>
@import "subs.css";
@media print {
@import "print-main.css";
body { font-size: 10pt }
}
h1 {color: blue }
</code></pre>
<p>Instead, to achieve the effect of only importing a style sheet
for 'print' media, use the @import rule with media syntax, e.g.:
</p>
<pre><code class="css">
@import "subs.css";
@import "print-main.css" print;
@media print {
body { font-size: 10pt }
}
h1 {color: blue }
</code></pre>
</div>
<h3>4.1.6 <a name="block">Blocks</a></h3>
<p> A <a name="x12"><span class="index-inst" title="block"><em>block</em></span></a>
starts with a left curly brace ({) and ends with the matching right
curly brace (}). In between there may be any tokens, except that
parentheses ((&nbsp;)), brackets ([&nbsp;]), and braces ({&nbsp;}) must
always occur in
matching pairs and may be nested. Single (') and double quotes (")
must also occur in matching pairs, and characters between them
are parsed as a <a name="x13"><span class="index-inst" title="string">string</span></a>.
See <a href="#tokenization">Tokenization</a> above for the definition
of a string.
</p>
<div class="illegal-example"><P style="display:none">Illegal example(s):</P>
<p> Here is an example of a block. Note that the right brace between
the double quotes does not match the opening brace of the block, and that the
second single quote is an <a href="#escaped-characters">escaped
character</a>, and thus does not match the first single quote:
</p>
<pre><code>
{ causta: "}" + ({7} * '\'') }
</code></pre>
<p>Note that the above rule is not valid CSS&nbsp;2.1, but it is still
a block as defined above.
</p>
</div>
<h3>4.1.7 <a name="rule-sets">Rule sets, declaration blocks, and selectors</a></h3>
<p> A rule set (also called "rule") consists of a selector followed by
a declaration block.
</p>
<p> A <a name="x14"><span class="index-def"
title="declaration block"><dfn>declaration block</dfn></span></a>
starts with a left curly
brace ({) and ends with the matching right curly brace (}). In between
there must be a list of zero or more semicolon-separated (;)
declarations.
</p>
<p>The <a name="x15"><span class="index-def"
title="selector"><em>selector</em></span></a> (see also the section on <a
href="selector.html">selectors</a>) consists of everything up to (but
not including) the first left curly brace ({). A selector always goes
together with a declaration block. When a user agent cannot parse the selector (i.e., it
is not valid CSS&nbsp;2.1), it must <span class="index-inst" title="ignore"><a name="x16"
href="#ignore">ignore</a></span> the selector and the following
declaration block (if any) as well.
</p>
<p>CSS&nbsp;2.1 gives a special meaning to the comma (,) in
selectors. However, since it is not known if the comma may acquire
other meanings in future updates of CSS, the whole statement should
be <span class="index-inst" title="ignore"><a name="x17"
href="#ignore">ignored</a></span> if there is an error anywhere in the
selector, even though the rest of the selector may look reasonable in
CSS&nbsp;2.1.
</p>
<div class=illegal-example><P style="display:none">Illegal example(s):</P>
<p>For example, since the "&amp;" is not a valid token in a CSS&nbsp;2.1
selector, a CSS&nbsp;2.1 user agent must
<span class="index-inst" title="ignore"><a name="x18" href="#ignore">ignore</a></span>
the whole second line, and not set the color of H3 to red:
</p>
<pre><code>
h1, h2 {color: green }
h3, h4 &amp; h5 {color: red }
h6 {color: black }
</code></pre>
</div>
<div class="example"><P style="display:none">Example(s):</P>
<p>Here is a more complex example. The first two pairs of curly braces
are inside a string, and do not mark the end of the selector. This is
a valid CSS&nbsp;2.1 rule.
</p>
<pre><code class="css">
p[example="public class foo\
{\
private int x;\
\
foo(int x) {\
this.x = x;\
}\
\
}"] { color: red }
</code></pre>
</div>
<h3>4.1.8 <a name="declaration">Declarations</a> and <a
name="properties">properties</a></h3>
<p> A <a name="x19"><span class="index-def"
title="declaration"><dfn>declaration</dfn></span></a> is either empty or
consists of a <a name="x20"><span class="index-inst"
title="property">property name</span></a>, followed by a colon (:), followed by
a property value. Around each of these there may be <a
href="#whitespace">white space</a>.
</p>
<p>Because of the way selectors work, multiple declarations for the
same selector may be organized into semicolon (;) separated
groups.</p>
<div class="example"><P style="display:none">Example(s):</P><p>
Thus, the following rules:</p>
<pre><code class="css">
h1 { font-weight: bold }
h1 { font-size: 12px }
h1 { line-height: 14px }
h1 { font-family: Helvetica }
h1 { font-variant: normal }
h1 { font-style: normal }
</code></pre>
<p>are equivalent to:</p>
<pre><code class="css">
h1 {
font-weight: bold;
font-size: 12px;
line-height: 14px;
font-family: Helvetica;
font-variant: normal;
font-style: normal
}
</code></pre>
</div>
<p>A property name is an <a
href="#value-def-identifier">identifier</a>. Any token may occur
in the property value. Parentheses ("(&nbsp;)"), brackets ("[&nbsp;]"),
braces ("{&nbsp;}"), single
quotes ('), and double quotes (") must come in matching
pairs, and semicolons not in strings must be <a
href="#escaped-characters">escaped</a>. Parentheses, brackets, and
braces may be nested. Inside the quotes, characters are parsed as a
string.
</p>
<p>The syntax of <a name="x21"><span class="index-def" title="value">values</span></a>
is specified separately for each property, but in any case, values are
built from identifiers, strings, numbers, lengths, percentages, URIs,
colors, etc.
</p>
<p>A user agent must <span class="index-inst" title="ignore"><a name="x22"
href="#ignore">ignore</a></span> a declaration with an invalid property
name or an invalid value. Every CSS property has its own syntactic
and semantic restrictions on the values it accepts.
</p>
<div class="illegal-example"><P style="display:none">Illegal example(s):</P><p>
For example, assume a CSS&nbsp;2.1 parser encounters this style sheet:
</p>
<pre><code>
h1 { color: red; font-style: 12pt } /* Invalid value: 12pt */
p { color: blue; font-vendor: any; /* Invalid prop.: font-vendor */
font-variant: small-caps }
em em { font-style: normal }
</code></pre>
<p> The second declaration on the first line has an invalid value
'12pt'. The second declaration on the second line contains an
undefined property 'font-vendor'. The CSS&nbsp;2.1 parser will <span
class="index-inst" title="ignore"><a name="x23" href="#ignore">ignore</a></span> these
declarations, effectively reducing the style sheet to:
</p>
<pre class="example"><code class="css">
h1 { color: red; }
p { color: blue; font-variant: small-caps }
em em { font-style: normal }
</code></pre>
</div>
<h3>4.1.9 <a name="comments">Comments</a></h3>
<p><a name="x24"><span class="index-inst" title="comments">Comments </span></a> begin
with the characters "/*" and end with the characters "*/". They may
occur anywhere outside other tokens,
and their contents have no influence on the rendering. Comments may
not be nested.
</p>
<p>CSS also allows the SGML comment delimiters ("&lt;!--" and
"--&gt;") in certain places defined by the grammar, but they do not
delimit CSS
comments. They are permitted so that style rules appearing in an HTML
source document (in the STYLE element) may be hidden from pre-HTML 3.2
user agents. See the HTML 4 specification (<a href="refs.html#ref-HTML4" rel="biblioentry" class="noxref"><span class="informref">[HTML4]</span></a>) for more information.
</p>
<h2>4.2 <a name="parsing-errors">Rules for handling parsing
errors</a></h2>
<p>In some cases, user agents must ignore part of an illegal style
sheet. This specification defines <span class="index-def"
title="ignore"><a name="ignore"><dfn>ignore</dfn></a></span> to mean
that the user agent parses the illegal part (in order to find its
beginning and end), but otherwise acts as if it had not been there.
CSS&nbsp;2.1 reserves for future updates of CSS all property:value combinations
and @-keywords that do not contain an identifier beginning with dash or
underscore. Implementations must ignore such combinations (other than those
introduced by future updates of CSS).
</p>
<p>To ensure that new properties and new values for existing
properties can be added in the future, user agents are required to
obey the following rules when they encounter the following
scenarios:</p>
<ul>
<li><strong>Unknown properties.</strong> User agents must <span
class="index-inst" title="ignore"><a name="x26" href="#ignore">ignore</a></span> a <a
href="syndata.html#declaration">declaration</a> with an unknown
property. For example, if the style sheet is:
<pre class="illegal-example">
h1 { color: red; rotation: 70minutes }
</pre>
<p> the user agent will treat this as if the style sheet had been
</p>
<pre class="example"><code class="css">
h1 { color: red }
</code></pre>
</li>
<li id="illegalvalues"><strong>Illegal values.</strong> User agents must ignore a
declaration with an illegal value. For example:
<pre class="illegal-example"><code>
img { float: left } /* correct CSS&nbsp;2.1 */
img { float: left here } /* "here" is not a value of 'float' */
img { background: "red" } /* keywords cannot be quoted */
img { border-width: 3 } /* a unit must be specified for length values */
</code></pre>
A CSS&nbsp;2.1 parser would honor the first rule and
<span class="index-inst" title="ignore"><a name="x27" href="#ignore">ignore</a></span>
the rest, as if the style sheet had been:
<pre class="example"><code class="css">
img { float: left }
img { }
img { }
img { }
</code></pre>
<p>A user agent conforming to a future CSS specification may accept one or
more of the other rules as well.</p>
</li>
<li><strong>Malformed declarations.</strong> User agents must handle
unexpected tokens encountered while parsing a declaration by reading
until the end of the declaration, while observing the rules for matching
pairs of (), [], {}, "", and '', and correctly handling escapes. For
example, a malformed declaration may be missing a property name, colon (:), or
property value. The following are all equivalent:
<pre class="example"><code>
p { color:green }
p { color:green; color } /* malformed declaration missing ':', value */
p { color:red; color; color:green } /* same with expected recovery */
p { color:green; color: } /* malformed declaration missing value */
p { color:red; color:; color:green } /* same with expected recovery */
p { color:green; color{;color:maroon} } /* unexpected tokens { } */
p { color:red; color{;color:maroon}; color:green } /* same with recovery */
</code></pre>
</li>
<li><strong>Malformed statements.</strong> User agents must handle
unexpected tokens encountered while parsing a statement by reading
until the end of the statement, while observing the rules for matching
pairs of (), [], {}, "", and '', and correctly handling escapes. For
example, a malformed statement may contain an unexpected closing brace
or at-keyword. E.g., the following lines are all ignored:
<pre>
p @here {color: red} /* ruleset with unexpected at-keyword "@here" */
@foo @bar; /* at-rule with unexpected at-keyword "@bar" */
}} {{ - }} /* ruleset with unexpected right brace */
) ( {} ) p {color: red } /* ruleset with unexpected right parenthesis */
</pre>
<li><strong>At-rules with unknown at-keywords.</strong> User agents must <span
class="index-inst" title="ignore"><a name="x28" href="#ignore">ignore</a></span>
an invalid at-keyword together with everything following it, up to the
end of the block that contains the invalid at-keyword, or up to and
including the next semicolon (;), or up to and including the next
block ({...}), whichever comes first. For example, consider the
following:
<pre class="illegal-example"><code>
@three-dee {
@background-lighting {
azimuth: 30deg;
elevation: 190deg;
}
h1 { color: red }
}
h1 { color: blue }
</code></pre>
<p> The '@three-dee' at-rule is not part of CSS&nbsp;2.1. Therefore, the whole
at-rule (up to, and including, the third right curly brace) is <span
class="index-inst" title="ignore"><a name="x29" href="#ignore">ignored.</a></span> A
CSS&nbsp;2.1 user agent <span class="index-inst" title="ignore"><a name="x30"
href="#ignore">ignores</a></span> it, effectively reducing the style sheet
to:</p>
<pre class="example"><code class="css">
h1 { color: blue }
</code></pre>
<p>Something inside an at-rule that is ignored because it is invalid,
such as an invalid declaration within an @media-rule, does not make
the entire at-rule invalid.
</p>
</li>
<li id=unexpected-eof><strong>Unexpected end of style sheet.</strong>
<p>
User agents must close all open constructs (for example: blocks, parentheses, brackets, rules, strings, and comments) at the end of the
style sheet. For example:
</p>
<pre class="illegal-example"><code>
@media screen {
p:before { content: 'Hello
</code></pre>
<p>
would be treated the same as:
</p>
<pre class="example"><code class="css">
@media screen {
p:before { content: 'Hello'; }
}
</code></pre>
<p>
in a conformant UA.
</p>
</li>
<li><strong>Unexpected end of string.</strong>
<p>
User agents must close strings upon reaching the end of a line
(i.e., before an unescaped line feed, carriage return or form
feed character), but
then drop the construct (declaration or rule) in which the string
was found. For example:
</p>
<pre><code>
p {
color: green;
font-family: 'Courier New Times
color: red;
color: green;
}
</code></pre>
<p>
...would be treated the same as:
</p>
<pre><code class="css">
p { color: green; color: green; }
</code></pre>
<p>
...because the second declaration (from 'font-family' to the
semicolon after 'color: red') is invalid and is dropped.
</p>
</li>
<li>See also <a href="#rule-sets">Rule sets, declaration blocks, and
selectors</a> for parsing rules for declaration blocks.
</li>
</ul>
<h2>4.3 <a name="values">Values</a></h2>
<h3>4.3.1 <a name="numbers">Integers and real numbers</a></h3>
<p>Some value types may have integer values (denoted by <span
class="index-def" title="&lt;integer&gt;::definition of"><a
name="value-def-integer" class="value-def">&lt;integer&gt;</a></span>)
or real number values (denoted by <span class="index-def"
title="&lt;number&gt;::definition of"><a name="value-def-number"
class="value-def">&lt;number&gt;</a></span>). Real numbers and
integers are specified in decimal notation only. An &lt;integer&gt;
consists of one or more digits "0" to "9". A &lt;number&gt; can either
be an &lt;integer&gt;, or it can be zero or more digits followed by a
dot (.) followed by one or more digits. Both integers and real numbers
may immediately be preceded by a "-" or "+" to indicate the sign.
-0 is equivalent to 0 and is not a negative number.</p>
<p>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.
</p>
<h3>4.3.2 <a name="length-units">Lengths</a></h3>
<p>Lengths refer to distance measurements.</p>
<p> The format of a length value (denoted by <span class="index-def"
title="&lt;length&gt;::definition of"><a name="value-def-length"
class="value-def">&lt;length&gt;</a></span> in this specification) is
a <a href="syndata.html#value-def-number" class="noxref"><span
class="value-inst-number">&lt;number&gt;</span></a> (with or without a
decimal point) immediately followed by a unit identifier (e.g., px,
em, etc.). After a zero length, the unit identifier is optional.
</p>
<p> Some properties allow negative length values, but this may
complicate the formatting model 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.
</p>
<p>If a negative length value is set on a property that does not allow
negative length values, the declaration is ignored.
</p>
<p>In cases where the <a href="cascade.html#usedValue">used</a>
length cannot be supported, user agents must approximate it in the
<a href="cascade.html#actual-value">actual value.</a>
<p><a name="absrel-units">There are two types of length units:
relative and absolute.</a> <a name="x34"><span class="index-def" title="relative
units"><em>Relative length</em></span></a> units specify a length relative
to another length property. Style sheets that use relative units
can more easily scale from one output environment
to another.
</p>
<p>Relative units are:</p>
<ul>
<li><strong>em</strong>: the <a href="fonts.html#propdef-font-size" class="noxref"><span class="propinst-font-size">'font-size'</span></a> of the relevant font</li>
<li><strong>ex</strong>: the <span class="descinst">'x-height'</span> of the relevant font</li>
</ul>
<div class="example"><P style="display:none">Example(s):</P>
<pre><code class="css">
h1 { margin: 0.5em } /* em */
h1 { margin: 1ex } /* ex */
</code></pre>
</div>
<p>The <span class="index-def" title="em (unit)|quad width"><a
name="em-width">'em'</a></span> unit is equal to the computed value of
the <a href="fonts.html#propdef-font-size" class="noxref"><span class="propinst-font-size">'font-size'</span></a> 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.)
</p>
<p>The <span class="index-def" title="x-height|ex (unit)"><a
name="ex">'ex'</a></span> unit is defined by the element's first available
font.
The exception is when 'ex' occurs in the value of the <a href="fonts.html#propdef-font-size" class="noxref"><span
class="propinst-font-size">'font-size'</span></a> property, in which case
it refers to the 'ex' of the parent element.
<p>The <span class="descinst-x-height">'x-height'</span> is so called
because it is often equal to the height of the lowercase "x". However,
an 'ex' is defined even for fonts that do not contain an "x".</p>
<p>The x-height of a font can be found in different ways. Some fonts
contain reliable metrics for the x-height. If reliable font metrics are
not available, UAs may determine the x-height from the height of a
lowercase glyph. One possible heuristic is to look at how far the glyph
for the lowercase "o" extends below the baseline, and subtract that value
from the top of its bounding box. In the cases where it is impossible or
impractical to determine the x-height, a value of 0.5em should be used.</p>
<div class="example"><P style="display:none">Example(s):</P>
<p>The rule:
</p>
<pre><code class="css">
h1 { line-height: 1.2em }
</code></pre>
<p>means that the line height of "h1" elements will be 20% greater
than the font size of the "h1" elements. On the other hand:
</p>
<pre><code class="css">
h1 { font-size: 1.2em }
</code></pre>
<p>means that the font-size of "h1" elements will be 20% greater than
the font size inherited by "h1" elements.</p>
</div>
<p>When specified for the root of the <a href="conform.html#doctree">
document tree</a> (e.g., "HTML" in HTML), 'em' and 'ex' refer to
the property's <a href="about.html#initial-value">initial value</a>.
</p>
<p>Child elements do not inherit the relative values specified for
their parent; they inherit the <a
href="cascade.html#computed-value">computed values</a>.</p>
<div class="example"><P style="display:none">Example(s):</P><p>
In the following rules, the computed <a href="text.html#propdef-text-indent" class="noxref"><span
class="propinst-text-indent">'text-indent'</span></a> value of "h1" elements
will be 36px, not 45px, if "h1" is a child of the "body" element.
</p>
<pre><code class="css">
body {
font-size: 12px;