3 <script src=
"../htmlrunner.js"></script>
5 window
.onload = function(){
6 startTest("dom-modify", '9ecfa02b');
8 // Try to force real results
12 var htmlstr
= document
.body
.innerHTML
;
13 var div
= document
.createElement("div");
16 for ( var i
= 0; i
< 1024; i
++ )
17 str
+= String
.fromCharCode( (25 * Math
.random()) + 97 );
19 test( "createElement", function(){
20 for ( var i
= 0; i
< num
; i
++ ) {
21 ret
= document
.createElement("div");
22 ret
= document
.createElement("span");
23 ret
= document
.createElement("table");
24 ret
= document
.createElement("tr");
25 ret
= document
.createElement("select");
29 test( "createTextNode", function(){
30 for ( var i
= 0; i
< num
; i
++ ) {
31 ret
= document
.createTextNode(str
);
32 ret
= document
.createTextNode(str
+ "2");
33 ret
= document
.createTextNode(str
+ "3");
34 ret
= document
.createTextNode(str
+ "4");
35 ret
= document
.createTextNode(str
+ "5");
39 /* Need a better way to test this
40 test( "removeChild", function(){
41 while ( document.body.firstChild )
42 document.body.removeChild( document.body.firstChild );
46 test( "innerHTML", function(){
47 document
.body
.innerHTML
= htmlstr
;
52 var telems
= document
.body
.childNodes
;
53 for ( var i
= 0; i
< telems
.length
; i
++ )
54 elems
.push( telems
[i
] );
57 test( "cloneNode", function(){
58 for ( var i
= 0; i
< elems
.length
; i
++ ) {
59 ret
= elems
[i
].cloneNode(false);
60 ret
= elems
[i
].cloneNode(true);
61 ret
= elems
[i
].cloneNode(true);
65 test( "appendChild", function(){
66 for ( var i
= 0; i
< elems
.length
; i
++ )
67 document
.body
.appendChild( elems
[i
] );
70 test( "insertBefore", function(){
71 for ( var i
= 0; i
< elems
.length
; i
++ )
72 document
.body
.insertBefore( elems
[i
], document
.body
.firstChild
);
81 <p><a href=
"http://www.w3.org/"><img height=
48 alt=W3C
src=
"http://www.w3.org/Icons/w3c_home" width=
72></a>
83 <h1 id=
"title">Selectors
</h1>
85 <h2>W3C Working Draft
15 December
2005</h2>
91 <dd><a href=
"http://www.w3.org/TR/2005/WD-css3-selectors-20051215">
92 http://www.w3.org/TR/
2005/WD-css3-selectors-
20051215</a>
96 <dd><a href=
"http://www.w3.org/TR/css3-selectors">
97 http://www.w3.org/TR/css3-selectors
</a>
101 <dd><a href=
"http://www.w3.org/TR/2001/CR-css3-selectors-20011113">
102 http://www.w3.org/TR/
2001/CR-css3-selectors-
20011113</a>
104 <dt><a name=editors-list
></a>Editors:
106 <dd class=
"vcard"><span class=
"fn">Daniel Glazman
</span> (Invited Expert)
</dd>
108 <dd class=
"vcard"><a lang=
"tr" class=
"url fn" href=
"http://www.tantek.com/">Tantek
Çelik
</a> (Invited Expert)
110 <dd class=
"vcard"><a href=
"mailto:ian@hixie.ch" class=
"url fn">Ian Hickson
</a> (
<span
111 class=
"company"><a href=
"http://www.google.com/">Google
</a></span>)
113 <dd class=
"vcard"><span class=
"fn">Peter Linss
</span> (former editor,
<span class=
"company"><a
114 href=
"http://www.netscape.com/">Netscape/AOL
</a></span>)
116 <dd class=
"vcard"><span class=
"fn">John Williams
</span> (former editor,
<span class=
"company"><a
117 href=
"http://www.quark.com/">Quark, Inc.
</a></span>)
121 <p class=
"copyright"><a
122 href=
"http://www.w3.org/Consortium/Legal/ipr-notice#Copyright">
123 Copyright
</a> © 2005 <a href=
"http://www.w3.org/"><abbr
124 title=
"World Wide Web Consortium">W3C
</abbr></a><sup>®</sup>
125 (
<a href=
"http://www.csail.mit.edu/"><abbr title=
"Massachusetts
126 Institute of Technology">MIT
</abbr></a>,
<a
127 href=
"http://www.ercim.org/"><acronym title=
"European Research
128 Consortium for Informatics and Mathematics">ERCIM
</acronym></a>,
<a
129 href=
"http://www.keio.ac.jp/">Keio
</a>), All Rights Reserved. W3C
131 href=
"http://www.w3.org/Consortium/Legal/ipr-notice#Legal_Disclaimer">liability
</a>,
133 href=
"http://www.w3.org/Consortium/Legal/ipr-notice#W3C_Trademarks">trademark
</a>,
135 href=
"http://www.w3.org/Consortium/Legal/copyright-documents">document
138 <hr title=
"Separator for header">
142 <h2><a name=abstract
></a>Abstract
</h2>
144 <p><em>Selectors
</em> are patterns that match against elements in a
145 tree. Selectors have been optimized for use with HTML and XML, and
146 are designed to be usable in performance-critical code.
</p>
148 <p><acronym title=
"Cascading Style Sheets">CSS
</acronym> (Cascading
149 Style Sheets) is a language for describing the rendering of
<acronym
150 title=
"Hypertext Markup Language">HTML
</acronym> and
<acronym
151 title=
"Extensible Markup Language">XML
</acronym> documents on
152 screen, on paper, in speech, etc. CSS uses Selectors for binding
153 style properties to elements in the document. This document
154 describes extensions to the selectors defined in CSS level
2. These
155 extended selectors will be used by CSS level
3.
157 <p>Selectors define the following function:
</p>
159 <pre>expression
∗ element
→ boolean
</pre>
161 <p>That is, given an element and a selector, this specification
162 defines whether that element matches the selector.
</p>
164 <p>These expressions can also be used, for instance, to select a set
165 of elements, or a single element from a set of elements, by
166 evaluating the expression across all the elements in a
167 subtree.
<acronym title=
"Simple Tree Transformation
168 Sheets">STTS
</acronym> (Simple Tree Transformation Sheets), a
169 language for transforming XML trees, uses this mechanism.
<a href=
"#refsSTTS">[STTS]
</a></p>
171 <h2><a name=status
></a>Status of this document
</h2>
173 <p><em>This section describes the status of this document at the
174 time of its publication. Other documents may supersede this
175 document. A list of current W3C publications and the latest revision
176 of this technical report can be found in the
<a
177 href=
"http://www.w3.org/TR/">W3C technical reports index at
178 http://www.w3.org/TR/.
</a></em></p>
180 <p>This document describes the selectors that already exist in
<a
181 href=
"#refsCSS1"><abbr title=
"CSS level 1">CSS1
</abbr></a> and
<a
182 href=
"#refsCSS21"><abbr title=
"CSS level 2">CSS2
</abbr></a>, and
183 also proposes new selectors for
<abbr title=
"CSS level
184 3">CSS3
</abbr> and other languages that may need them.
</p>
186 <p>The CSS Working Group doesn't expect that all implementations of
187 CSS3 will have to implement all selectors. Instead, there will
188 probably be a small number of variants of CSS3, called profiles. For
189 example, it may be that only a profile for interactive user agents
190 will include all of the selectors.
</p>
192 <p>This specification is a last call working draft for the the
<a
193 href=
"http://www.w3.org/Style/CSS/members">CSS Working Group
</a>
194 (
<a href=
"/Style/">Style Activity
</a>). This
195 document is a revision of the
<a
196 href=
"http://www.w3.org/TR/2001/CR-css3-selectors-20011113/">Candidate
197 Recommendation dated
2001 November
13</a>, and has incorporated
198 implementation feedback received in the past few years. It is
199 expected that this last call will proceed straight to Proposed
200 Recommendation stage since it is believed that interoperability will
203 <p>All persons are encouraged to review and implement this
204 specification and return comments to the (
<a
205 href=
"http://lists.w3.org/Archives/Public/www-style/">archived
</a>)
206 public mailing list
<a
207 href=
"http://www.w3.org/Mail/Lists.html#www-style">www-style
</a>
208 (see
<a href=
"http://www.w3.org/Mail/Request">instructions
</a>). W3C
209 Members can also send comments directly to the CSS Working
211 The deadline for comments is
14 January
2006.
</p>
213 <p>This is still a draft document and may be updated, replaced, or
214 obsoleted by other documents at any time. It is inappropriate to
215 cite a W3C Working Draft as other than
"work in progress
".
217 <p>This document may be available in
<a
218 href=
"http://www.w3.org/Style/css3-selectors-updates/translations">translation
</a>.
219 The English version of this specification is the only normative
224 <h2 id=
"test1"><a name=contents
>Table of contents
</a></h2>
227 <li class=
"tocline2"><a href=
"#context">1. Introduction
</a>
229 <li><a href=
"#dependencies">1.1. Dependencies
</a> </li>
230 <li><a href=
"#terminology">1.2. Terminology
</a> </li>
231 <li><a href=
"#changesFromCSS2">1.3. Changes from CSS2
</a> </li>
233 <li class=
"tocline2"><a href=
"#selectors">2. Selectors
</a>
234 <li class=
"tocline2"><a href=
"#casesens">3. Case sensitivity
</a>
235 <li class=
"tocline2"><a href=
"#selector-syntax">4. Selector syntax
</a>
236 <li class=
"tocline2"><a href=
"#grouping">5. Groups of selectors
</a>
237 <li class=
"tocline2"><a href=
"#simple-selectors">6. Simple selectors
</a>
239 <li class=
"tocline3"><a href=
"#type-selectors">6.1. Type selectors
</a>
241 <li class=
"tocline4"><a href=
"#typenmsp">6.1.1. Type selectors and namespaces
</a></li>
243 <li class=
"tocline3"><a href=
"#universal-selector">6.2. Universal selector
</a>
245 <li><a href=
"#univnmsp">6.2.1. Universal selector and namespaces
</a></li>
247 <li class=
"tocline3"><a href=
"#attribute-selectors">6.3. Attribute selectors
</a>
249 <li class=
"tocline4"><a href=
"#attribute-representation">6.3.1. Representation of attributes and attributes values
</a>
250 <li><a href=
"#attribute-substrings">6.3.2. Substring matching attribute selectors
</a>
251 <li class=
"tocline4"><a href=
"#attrnmsp">6.3.3. Attribute selectors and namespaces
</a>
252 <li class=
"tocline4"><a href=
"#def-values">6.3.4. Default attribute values in DTDs
</a></li>
254 <li class=
"tocline3"><a href=
"#class-html">6.4. Class selectors
</a>
255 <li class=
"tocline3"><a href=
"#id-selectors">6.5. ID selectors
</a>
256 <li class=
"tocline3"><a href=
"#pseudo-classes">6.6. Pseudo-classes
</a>
258 <li class=
"tocline4"><a href=
"#dynamic-pseudos">6.6.1. Dynamic pseudo-classes
</a>
259 <li class=
"tocline4"><a href=
"#target-pseudo">6.6.2. The :target pseudo-class
</a>
260 <li class=
"tocline4"><a href=
"#lang-pseudo">6.6.3. The :lang() pseudo-class
</a>
261 <li class=
"tocline4"><a href=
"#UIstates">6.6.4. UI element states pseudo-classes
</a>
262 <li class=
"tocline4"><a href=
"#structural-pseudos">6.6.5. Structural pseudo-classes
</a>
264 <li><a href=
"#root-pseudo">:root pseudo-class
</a>
265 <li><a href=
"#nth-child-pseudo">:nth-child() pseudo-class
</a>
266 <li><a href=
"#nth-last-child-pseudo">:nth-last-child()
</a>
267 <li><a href=
"#nth-of-type-pseudo">:nth-of-type() pseudo-class
</a>
268 <li><a href=
"#nth-last-of-type-pseudo">:nth-last-of-type()
</a>
269 <li><a href=
"#first-child-pseudo">:first-child pseudo-class
</a>
270 <li><a href=
"#last-child-pseudo">:last-child pseudo-class
</a>
271 <li><a href=
"#first-of-type-pseudo">:first-of-type pseudo-class
</a>
272 <li><a href=
"#last-of-type-pseudo">:last-of-type pseudo-class
</a>
273 <li><a href=
"#only-child-pseudo">:only-child pseudo-class
</a>
274 <li><a href=
"#only-of-type-pseudo">:only-of-type pseudo-class
</a>
275 <li><a href=
"#empty-pseudo">:empty pseudo-class
</a></li>
277 <li class=
"tocline4"><a href=
"#negation">6.6.7. The negation pseudo-class
</a></li>
281 <li><a href=
"#pseudo-elements">7. Pseudo-elements
</a>
283 <li><a href=
"#first-line">7.1. The ::first-line pseudo-element
</a>
284 <li><a href=
"#first-letter">7.2. The ::first-letter pseudo-element
</a>
285 <li><a href=
"#UIfragments">7.3. The ::selection pseudo-element
</a>
286 <li><a href=
"#gen-content">7.4. The ::before and ::after pseudo-elements
</a></li>
288 <li class=
"tocline2"><a href=
"#combinators">8. Combinators
</a>
290 <li class=
"tocline3"><a href=
"#descendant-combinators">8.1. Descendant combinators
</a>
291 <li class=
"tocline3"><a href=
"#child-combinators">8.2. Child combinators
</a>
292 <li class=
"tocline3"><a href=
"#sibling-combinators">8.3. Sibling combinators
</a>
294 <li class=
"tocline4"><a href=
"#adjacent-sibling-combinators">8.3.1. Adjacent sibling combinator
</a>
295 <li class=
"tocline4"><a href=
"#general-sibling-combinators">8.3.2. General sibling combinator
</a></li>
299 <li class=
"tocline2"><a href=
"#specificity">9. Calculating a selector's specificity
</a>
300 <li class=
"tocline2"><a href=
"#w3cselgrammar">10. The grammar of Selectors
</a>
302 <li class=
"tocline3"><a href=
"#grammar">10.1. Grammar
</a>
303 <li class=
"tocline3"><a href=
"#lex">10.2. Lexical scanner
</a></li>
305 <li class=
"tocline2"><a href=
"#downlevel">11. Namespaces and down-level clients
</a>
306 <li class=
"tocline2"><a href=
"#profiling">12. Profiles
</a>
307 <li><a href=
"#Conformance">13. Conformance and requirements
</a>
308 <li><a href=
"#Tests">14. Tests
</a>
309 <li><a href=
"#ACKS">15. Acknowledgements
</a>
310 <li class=
"tocline2"><a href=
"#references">16. References
</a>
315 <h2><a name=context
>1. Introduction
</a></h2>
317 <h3><a name=dependencies
></a>1.1. Dependencies
</h3>
319 <p>Some features of this specification are specific to CSS, or have
320 particular limitations or rules specific to CSS. In this
321 specification, these have been described in terms of CSS2.1.
<a
322 href=
"#refsCSS21">[CSS21]
</a></p>
324 <h3><a name=terminology
></a>1.2. Terminology
</h3>
326 <p>All of the text of this specification is normative except
327 examples, notes, and sections explicitly marked as
330 <h3><a name=changesFromCSS2
></a>1.3. Changes from CSS2
</h3>
332 <p><em>This section is non-normative.
</em></p>
334 <p>The main differences between the selectors in CSS2 and those in
339 <li>the list of basic definitions (selector, group of selectors,
340 simple selector, etc.) has been changed; in particular, what was
341 referred to in CSS2 as a simple selector is now called a sequence
342 of simple selectors, and the term
"simple selector" is now used for
343 the components of this sequence
</li>
345 <li>an optional namespace component is now allowed in type element
346 selectors, the universal selector and attribute selectors
</li>
348 <li>a
<a href=
"#general-sibling-combinators">new combinator
</a> has been introduced
</li>
350 <li>new simple selectors including substring matching attribute
351 selectors, and new pseudo-classes
</li>
353 <li>new pseudo-elements, and introduction of the
"::" convention
354 for pseudo-elements
</li>
356 <li>the grammar has been rewritten
</li>
358 <li>profiles to be added to specifications integrating Selectors
359 and defining the set of selectors which is actually supported by
360 each specification
</li>
362 <li>Selectors are now a CSS3 Module and an independent
363 specification; other specifications can now refer to this document
364 independently of CSS
</li>
366 <li>the specification now has its own test suite
</li>
370 <h2><a name=selectors
></a>2. Selectors
</h2>
372 <p><em>This section is non-normative, as it merely summarizes the
373 following sections.
</em></p>
375 <p>A Selector represents a structure. This structure can be used as a
376 condition (e.g. in a CSS rule) that determines which elements a
377 selector matches in the document tree, or as a flat description of the
378 HTML or XML fragment corresponding to that structure.
</p>
380 <p>Selectors may range from simple element names to rich contextual
383 <p>The following table summarizes the Selector syntax:
</p>
385 <table class=
"selectorsReview">
388 <th class=
"pattern">Pattern
</th>
389 <th class=
"meaning">Meaning
</th>
390 <th class=
"described">Described in section
</th>
391 <th class=
"origin">First defined in CSS level
</th></tr>
394 <td class=
"pattern">*
</td>
395 <td class=
"meaning">any element
</td>
396 <td class=
"described"><a
397 href=
"#universal-selector">Universal
399 <td class=
"origin">2</td></tr>
401 <td class=
"pattern">E
</td>
402 <td class=
"meaning">an element of type E
</td>
403 <td class=
"described"><a
404 href=
"#type-selectors">Type selector
</a></td>
405 <td class=
"origin">1</td></tr>
407 <td class=
"pattern">E[foo]
</td>
408 <td class=
"meaning">an E element with a
"foo" attribute
</td>
409 <td class=
"described"><a
410 href=
"#attribute-selectors">Attribute
412 <td class=
"origin">2</td></tr>
414 <td class=
"pattern">E[
foo=
"bar"]
</td>
415 <td class=
"meaning">an E element whose
"foo" attribute value is exactly
417 <td class=
"described"><a
418 href=
"#attribute-selectors">Attribute
420 <td class=
"origin">2</td></tr>
422 <td class=
"pattern">E[foo~=
"bar"]
</td>
423 <td class=
"meaning">an E element whose
"foo" attribute value is a list of
424 space-separated values, one of which is exactly equal to
"bar"</td>
425 <td class=
"described"><a
426 href=
"#attribute-selectors">Attribute
428 <td class=
"origin">2</td></tr>
430 <td class=
"pattern">E[foo^=
"bar"]
</td>
431 <td class=
"meaning">an E element whose
"foo" attribute value begins exactly
432 with the string
"bar"</td>
433 <td class=
"described"><a
434 href=
"#attribute-selectors">Attribute
436 <td class=
"origin">3</td></tr>
438 <td class=
"pattern">E[foo$=
"bar"]
</td>
439 <td class=
"meaning">an E element whose
"foo" attribute value ends exactly
440 with the string
"bar"</td>
441 <td class=
"described"><a
442 href=
"#attribute-selectors">Attribute
444 <td class=
"origin">3</td></tr>
446 <td class=
"pattern">E[foo*=
"bar"]
</td>
447 <td class=
"meaning">an E element whose
"foo" attribute value contains the
449 <td class=
"described"><a
450 href=
"#attribute-selectors">Attribute
452 <td class=
"origin">3</td></tr>
454 <td class=
"pattern">E[hreflang|=
"en"]
</td>
455 <td class=
"meaning">an E element whose
"hreflang" attribute has a hyphen-separated
456 list of values beginning (from the left) with
"en"</td>
457 <td class=
"described"><a
458 href=
"#attribute-selectors">Attribute
460 <td class=
"origin">2</td></tr>
462 <td class=
"pattern">E:root
</td>
463 <td class=
"meaning">an E element, root of the document
</td>
464 <td class=
"described"><a
465 href=
"#structural-pseudos">Structural
466 pseudo-classes
</a></td>
467 <td class=
"origin">3</td></tr>
469 <td class=
"pattern">E:nth-child(n)
</td>
470 <td class=
"meaning">an E element, the n-th child of its parent
</td>
471 <td class=
"described"><a
472 href=
"#structural-pseudos">Structural
473 pseudo-classes
</a></td>
474 <td class=
"origin">3</td></tr>
476 <td class=
"pattern">E:nth-last-child(n)
</td>
477 <td class=
"meaning">an E element, the n-th child of its parent, counting
478 from the last one
</td>
479 <td class=
"described"><a
480 href=
"#structural-pseudos">Structural
481 pseudo-classes
</a></td>
482 <td class=
"origin">3</td></tr>
484 <td class=
"pattern">E:nth-of-type(n)
</td>
485 <td class=
"meaning">an E element, the n-th sibling of its type
</td>
486 <td class=
"described"><a
487 href=
"#structural-pseudos">Structural
488 pseudo-classes
</a></td>
489 <td class=
"origin">3</td></tr>
491 <td class=
"pattern">E:nth-last-of-type(n)
</td>
492 <td class=
"meaning">an E element, the n-th sibling of its type, counting
493 from the last one
</td>
494 <td class=
"described"><a
495 href=
"#structural-pseudos">Structural
496 pseudo-classes
</a></td>
497 <td class=
"origin">3</td></tr>
499 <td class=
"pattern">E:first-child
</td>
500 <td class=
"meaning">an E element, first child of its parent
</td>
501 <td class=
"described"><a
502 href=
"#structural-pseudos">Structural
503 pseudo-classes
</a></td>
504 <td class=
"origin">2</td></tr>
506 <td class=
"pattern">E:last-child
</td>
507 <td class=
"meaning">an E element, last child of its parent
</td>
508 <td class=
"described"><a
509 href=
"#structural-pseudos">Structural
510 pseudo-classes
</a></td>
511 <td class=
"origin">3</td></tr>
513 <td class=
"pattern">E:first-of-type
</td>
514 <td class=
"meaning">an E element, first sibling of its type
</td>
515 <td class=
"described"><a
516 href=
"#structural-pseudos">Structural
517 pseudo-classes
</a></td>
518 <td class=
"origin">3</td></tr>
520 <td class=
"pattern">E:last-of-type
</td>
521 <td class=
"meaning">an E element, last sibling of its type
</td>
522 <td class=
"described"><a
523 href=
"#structural-pseudos">Structural
524 pseudo-classes
</a></td>
525 <td class=
"origin">3</td></tr>
527 <td class=
"pattern">E:only-child
</td>
528 <td class=
"meaning">an E element, only child of its parent
</td>
529 <td class=
"described"><a
530 href=
"#structural-pseudos">Structural
531 pseudo-classes
</a></td>
532 <td class=
"origin">3</td></tr>
534 <td class=
"pattern">E:only-of-type
</td>
535 <td class=
"meaning">an E element, only sibling of its type
</td>
536 <td class=
"described"><a
537 href=
"#structural-pseudos">Structural
538 pseudo-classes
</a></td>
539 <td class=
"origin">3</td></tr>
541 <td class=
"pattern">E:empty
</td>
542 <td class=
"meaning">an E element that has no children (including text
544 <td class=
"described"><a
545 href=
"#structural-pseudos">Structural
546 pseudo-classes
</a></td>
547 <td class=
"origin">3</td></tr>
549 <td class=
"pattern">E:link
<br>E:visited
</td>
550 <td class=
"meaning">an E element being the source anchor of a hyperlink of
551 which the target is not yet visited (:link) or already visited
553 <td class=
"described"><a
554 href=
"#link">The link
555 pseudo-classes
</a></td>
556 <td class=
"origin">1</td></tr>
558 <td class=
"pattern">E:active
<br>E:hover
<br>E:focus
</td>
559 <td class=
"meaning">an E element during certain user actions
</td>
560 <td class=
"described"><a
561 href=
"#useraction-pseudos">The user
562 action pseudo-classes
</a></td>
563 <td class=
"origin">1 and
2</td></tr>
565 <td class=
"pattern">E:target
</td>
566 <td class=
"meaning">an E element being the target of the referring URI
</td>
567 <td class=
"described"><a
568 href=
"#target-pseudo">The target
569 pseudo-class
</a></td>
570 <td class=
"origin">3</td></tr>
572 <td class=
"pattern">E:lang(fr)
</td>
573 <td class=
"meaning">an element of type E in language
"fr" (the document
574 language specifies how language is determined)
</td>
575 <td class=
"described"><a
576 href=
"#lang-pseudo">The :lang()
577 pseudo-class
</a></td>
578 <td class=
"origin">2</td></tr>
580 <td class=
"pattern">E:enabled
<br>E:disabled
</td>
581 <td class=
"meaning">a user interface element E which is enabled or
583 <td class=
"described"><a
584 href=
"#UIstates">The UI element states
585 pseudo-classes
</a></td>
586 <td class=
"origin">3</td></tr>
588 <td class=
"pattern">E:checked
<!--<br>E:indeterminate--></td>
589 <td class=
"meaning">a user interface element E which is checked
<!-- or in an
590 indeterminate state--> (for instance a radio-button or checkbox)
</td>
591 <td class=
"described"><a
592 href=
"#UIstates">The UI element states
593 pseudo-classes
</a></td>
594 <td class=
"origin">3</td></tr>
596 <td class=
"pattern">E::first-line
</td>
597 <td class=
"meaning">the first formatted line of an E element
</td>
598 <td class=
"described"><a
599 href=
"#first-line">The ::first-line
600 pseudo-element
</a></td>
601 <td class=
"origin">1</td></tr>
603 <td class=
"pattern">E::first-letter
</td>
604 <td class=
"meaning">the first formatted letter of an E element
</td>
605 <td class=
"described"><a
606 href=
"#first-letter">The ::first-letter
607 pseudo-element
</a></td>
608 <td class=
"origin">1</td></tr>
610 <td class=
"pattern">E::selection
</td>
611 <td class=
"meaning">the portion of an E element that is currently
612 selected/highlighted by the user
</td>
613 <td class=
"described"><a
614 href=
"#UIfragments">The UI element
615 fragments pseudo-elements
</a></td>
616 <td class=
"origin">3</td></tr>
618 <td class=
"pattern">E::before
</td>
619 <td class=
"meaning">generated content before an E element
</td>
620 <td class=
"described"><a
621 href=
"#gen-content">The ::before
622 pseudo-element
</a></td>
623 <td class=
"origin">2</td></tr>
625 <td class=
"pattern">E::after
</td>
626 <td class=
"meaning">generated content after an E element
</td>
627 <td class=
"described"><a
628 href=
"#gen-content">The ::after
629 pseudo-element
</a></td>
630 <td class=
"origin">2</td></tr>
632 <td class=
"pattern">E.warning
</td>
633 <td class=
"meaning">an E element whose class is
634 "warning" (the document language specifies how class is determined).
</td>
635 <td class=
"described"><a
636 href=
"#class-html">Class
638 <td class=
"origin">1</td></tr>
640 <td class=
"pattern">E#myid
</td>
641 <td class=
"meaning">an E element with ID equal to
"myid".
</td>
642 <td class=
"described"><a
643 href=
"#id-selectors">ID
645 <td class=
"origin">1</td></tr>
647 <td class=
"pattern">E:not(s)
</td>
648 <td class=
"meaning">an E element that does not match simple selector s
</td>
649 <td class=
"described"><a
650 href=
"#negation">Negation
651 pseudo-class
</a></td>
652 <td class=
"origin">3</td></tr>
654 <td class=
"pattern">E F
</td>
655 <td class=
"meaning">an F element descendant of an E element
</td>
656 <td class=
"described"><a
657 href=
"#descendant-combinators">Descendant
659 <td class=
"origin">1</td></tr>
661 <td class=
"pattern">E
> F
</td>
662 <td class=
"meaning">an F element child of an E element
</td>
663 <td class=
"described"><a
664 href=
"#child-combinators">Child
666 <td class=
"origin">2</td></tr>
668 <td class=
"pattern">E + F
</td>
669 <td class=
"meaning">an F element immediately preceded by an E element
</td>
670 <td class=
"described"><a
671 href=
"#adjacent-sibling-combinators">Adjacent sibling combinator
</a></td>
672 <td class=
"origin">2</td></tr>
674 <td class=
"pattern">E ~ F
</td>
675 <td class=
"meaning">an F element preceded by an E element
</td>
676 <td class=
"described"><a
677 href=
"#general-sibling-combinators">General sibling combinator
</a></td>
678 <td class=
"origin">3</td></tr></tbody></table>
680 <p>The meaning of each selector is derived from the table above by
681 prepending
"matches" to the contents of each cell in the
"Meaning"
684 <h2><a name=casesens
>3. Case sensitivity
</a></h2>
686 <p>The case sensitivity of document language element names, attribute
687 names, and attribute values in selectors depends on the document
688 language. For example, in HTML, element names are case-insensitive,
689 but in XML, they are case-sensitive.
</p>
691 <h2><a name=selector-syntax
>4. Selector syntax
</a></h2>
693 <p>A
<dfn><a name=selector
>selector
</a></dfn> is a chain of one
694 or more
<a href=
"#sequence">sequences of simple selectors
</a>
695 separated by
<a href=
"#combinators">combinators
</a>.
</p>
697 <p>A
<dfn><a name=sequence
>sequence of simple selectors
</a></dfn>
698 is a chain of
<a href=
"#simple-selectors-dfn">simple selectors
</a>
699 that are not separated by a
<a href=
"#combinators">combinator
</a>. It
700 always begins with a
<a href=
"#type-selectors">type selector
</a> or a
701 <a href=
"#universal-selector">universal selector
</a>. No other type
702 selector or universal selector is allowed in the sequence.
</p>
704 <p>A
<dfn><a name=simple-selectors-dfn
></a><a
705 href=
"#simple-selectors">simple selector
</a></dfn> is either a
<a
706 href=
"#type-selectors">type selector
</a>,
<a
707 href=
"#universal-selector">universal selector
</a>,
<a
708 href=
"#attribute-selectors">attribute selector
</a>,
<a
709 href=
"#class-html">class selector
</a>,
<a
710 href=
"#id-selectors">ID selector
</a>,
<a
711 href=
"#content-selectors">content selector
</a>, or
<a
712 href=
"#pseudo-classes">pseudo-class
</a>. One
<a
713 href=
"#pseudo-elements">pseudo-element
</a> may be appended to the last
714 sequence of simple selectors.
</p>
716 <p><dfn>Combinators
</dfn> are: white space,
"greater-than
717 sign
" (U+
003E,
<code>></code>),
"plus sign
" (U+
002B,
718 <code>+
</code>) and
"tilde
" (U+
007E,
<code>~
</code>). White
719 space may appear between a combinator and the simple selectors around
720 it.
<a name=whitespace
></a>Only the characters
"space" (U+
0020),
"tab"
721 (U+
0009),
"line feed" (U+
000A),
"carriage return" (U+
000D), and
"form
722 feed" (U+
000C) can occur in white space. Other space-like characters,
723 such as
"em-space" (U+
2003) and
"ideographic space" (U+
3000), are
724 never part of white space.
</p>
726 <p>The elements of a document tree that are represented by a selector
727 are the
<dfn><a name=subject
></a>subjects of the selector
</dfn>. A
728 selector consisting of a single sequence of simple selectors
729 represents any element satisfying its requirements. Prepending another
730 sequence of simple selectors and a combinator to a sequence imposes
731 additional matching constraints, so the subjects of a selector are
732 always a subset of the elements represented by the last sequence of
733 simple selectors.
</p>
735 <p>An empty selector, containing no sequence of simple selectors and
736 no pseudo-element, is an
<a href=
"#Conformance">invalid
739 <h2><a name=grouping
>5. Groups of selectors
</a></h2>
741 <p>When several selectors share the same declarations, they may be
742 grouped into a comma-separated list. (A comma is U+
002C.)
</p>
744 <div class=
"example">
746 <p>In this example, we condense three rules with identical
747 declarations into one. Thus,
</p>
748 <pre>h1 { font-family: sans-serif }
749 h2 { font-family: sans-serif }
750 h3 { font-family: sans-serif }
</pre>
751 <p>is equivalent to:
</p>
752 <pre>h1, h2, h3 { font-family: sans-serif }
</pre>
755 <p><strong>Warning
</strong>: the equivalence is true in this example
756 because all the selectors are valid selectors. If just one of these
757 selectors were invalid, the entire group of selectors would be
758 invalid. This would invalidate the rule for all three heading
759 elements, whereas in the former case only one of the three individual
760 heading rules would be invalidated.
</p>
763 <h2><a name=simple-selectors
>6. Simple selectors
</a></h2>
765 <h3><a name=type-selectors
>6.1. Type selector
</a></h3>
767 <p>A
<dfn>type selector
</dfn> is the name of a document language
768 element type. A type selector represents an instance of the element
769 type in the document tree.
</p>
771 <div class=
"example">
773 <p>The following selector represents an
<code>h1
</code> element in the document tree:
</p>
778 <h4><a name=typenmsp
>6.1.1. Type selectors and namespaces
</a></h4>
780 <p>Type selectors allow an optional namespace (
<a
781 href=
"#refsXMLNAMES">[XMLNAMES]
</a>) component. A namespace prefix
782 that has been previously declared may be prepended to the element name
783 separated by the namespace separator
"vertical bar
"
784 (U+
007C,
<code>|
</code>).
</p>
786 <p>The namespace component may be left empty to indicate that the
787 selector is only to represent elements with no declared namespace.
</p>
789 <p>An asterisk may be used for the namespace prefix, indicating that
790 the selector represents elements in any namespace (including elements
791 with no namespace).
</p>
793 <p>Element type selectors that have no namespace component (no
794 namespace separator), represent elements without regard to the
795 element's namespace (equivalent to
"<code>*|</code>") unless a default
796 namespace has been declared. If a default namespace has been declared,
797 the selector will represent only elements in the default
800 <p>A type selector containing a namespace prefix that has not been
801 previously declared is an
<a href=
"#Conformance">invalid
</a> selector.
802 The mechanism for declaring a namespace prefix is left up to the
803 language implementing Selectors. In CSS, such a mechanism is defined
804 in the General Syntax module.
</p>
806 <p>In a namespace-aware client, element type selectors will only match
808 href=
"http://www.w3.org/TR/REC-xml-names/#NT-LocalPart">local part
</a>
810 href=
"http://www.w3.org/TR/REC-xml-names/#ns-qualnames">qualified
811 name
</a>. See
<a href=
"#downlevel">below
</a> for notes about matching
812 behaviors in down-level clients.
</p>
817 <dt><code>ns|E
</code></dt>
818 <dd>elements with name E in namespace ns
</dd>
819 <dt><code>*|E
</code></dt>
820 <dd>elements with name E in any namespace, including those without any
821 declared namespace
</dd>
822 <dt><code>|E
</code></dt>
823 <dd>elements with name E without any declared namespace
</dd>
824 <dt><code>E
</code></dt>
825 <dd>if no default namespace has been specified, this is equivalent to *|E.
826 Otherwise it is equivalent to ns|E where ns is the default namespace.
</dd>
829 <div class=
"example">
832 <pre>@namespace foo url(http://www.example.com);
833 foo|h1 { color: blue }
834 foo|* { color: yellow }
836 *|h1 { color: green }
837 h1 { color: green }
</pre>
839 <p>The first rule will match only
<code>h1
</code> elements in the
840 "http://www.example.com" namespace.
</p>
842 <p>The second rule will match all elements in the
843 "http://www.example.com" namespace.
</p>
845 <p>The third rule will match only
<code>h1
</code> elements without
846 any declared namespace.
</p>
848 <p>The fourth rule will match
<code>h1
</code> elements in any
849 namespace (including those without any declared namespace).
</p>
851 <p>The last rule is equivalent to the fourth rule because no default
852 namespace has been defined.
</p>
856 <h3><a name=universal-selector
>6.2. Universal selector
</a> </h3>
858 <p>The
<dfn>universal selector
</dfn>, written
"asterisk
"
859 (
<code>*
</code>), represents the qualified name of any element
860 type. It represents any single element in the document tree in any
861 namespace (including those without any declared namespace) if no
862 default namespace has been specified. If a default namespace has been
863 specified, see
<a href=
"#univnmsp">Universal selector and
864 Namespaces
</a> below.
</p>
866 <p>If the universal selector is not the only component of a sequence
867 of simple selectors, the
<code>*
</code> may be omitted.
</p>
869 <div class=
"example">
872 <li><code>*[hreflang|=en]
</code> and
<code>[hreflang|=en]
</code> are equivalent,
</li>
873 <li><code>*.warning
</code> and
<code>.warning
</code> are equivalent,
</li>
874 <li><code>*#myid
</code> and
<code>#myid
</code> are equivalent.
</li>
878 <p class=
"note"><strong>Note:
</strong> it is recommended that the
879 <code>*
</code>, representing the universal selector, not be
882 <h4><a name=univnmsp
>6.2.1. Universal selector and namespaces
</a></h4>
884 <p>The universal selector allows an optional namespace component. It
885 is used as follows:
</p>
888 <dt><code>ns|*
</code></dt>
889 <dd>all elements in namespace ns
</dd>
890 <dt><code>*|*
</code></dt>
891 <dd>all elements
</dd>
892 <dt><code>|*
</code></dt>
893 <dd>all elements without any declared namespace
</dd>
894 <dt><code>*
</code></dt>
895 <dd>if no default namespace has been specified, this is equivalent to *|*.
896 Otherwise it is equivalent to ns|* where ns is the default namespace.
</dd>
899 <p>A universal selector containing a namespace prefix that has not
900 been previously declared is an
<a href=
"#Conformance">invalid
</a>
901 selector. The mechanism for declaring a namespace prefix is left up
902 to the language implementing Selectors. In CSS, such a mechanism is
903 defined in the General Syntax module.
</p>
906 <h3><a name=attribute-selectors
>6.3. Attribute selectors
</a></h3>
908 <p>Selectors allow the representation of an element's attributes. When
909 a selector is used as an expression to match against an element,
910 attribute selectors must be considered to match an element if that
911 element has an attribute that matches the attribute represented by the
912 attribute selector.
</p>
914 <h4><a name=attribute-representation
>6.3.1. Attribute presence and values
917 <p>CSS2 introduced four attribute selectors:
</p>
920 <dt><code>[att]
</code>
921 <dd>Represents an element with the
<code>att
</code> attribute, whatever the value of
923 <dt><code>[att=val]
</code></dt>
924 <dd>Represents an element with the
<code>att
</code> attribute whose value is exactly
926 <dt><code>[att~=val]
</code></dt>
927 <dd>Represents an element with the
<code>att
</code> attribute whose value is a
<a
928 href=
"#whitespace">whitespace
</a>-separated list of words, one of
929 which is exactly
"val". If
"val" contains whitespace, it will never
930 represent anything (since the words are
<em>separated
</em> by
932 <dt><code>[att|=val]
</code>
933 <dd>Represents an element with the
<code>att
</code> attribute, its value either
934 being exactly
"val" or beginning with
"val" immediately followed by
935 "-" (U+
002D). This is primarily intended to allow language subcode
936 matches (e.g., the
<code>hreflang
</code> attribute on the
937 <code>link
</code> element in HTML) as described in RFC
3066 (
<a
938 href=
"#refsRFC3066">[RFC3066]
</a>). For
<code>lang
</code> (or
939 <code>xml:lang
</code>) language subcode matching, please see
<a
940 href=
"#lang-pseudo">the
<code>:lang
</code> pseudo-class
</a>.
</dd>
943 <p>Attribute values must be identifiers or strings. The
944 case-sensitivity of attribute names and values in selectors depends on
945 the document language.
</p>
947 <div class=
"example">
951 <p>The following attribute selector represents an
<code>h1
</code>
952 element that carries the
<code>title
</code> attribute, whatever its
957 <p>In the following example, the selector represents a
958 <code>span
</code> element whose
<code>class
</code> attribute has
959 exactly the value
"example":
</p>
961 <pre>span[
class=
"example"]
</pre>
963 <p>Multiple attribute selectors can be used to represent several
964 attributes of an element, or several conditions on the same
965 attribute. Here, the selector represents a
<code>span
</code> element
966 whose
<code>hello
</code> attribute has exactly the value
"Cleveland"
967 and whose
<code>goodbye
</code> attribute has exactly the value
970 <pre>span[
hello=
"Cleveland"][
goodbye=
"Columbus"]
</pre>
972 <p>The following selectors illustrate the differences between
"="
973 and
"~=". The first selector will represent, for example, the value
974 "copyright copyleft copyeditor" on a
<code>rel
</code> attribute. The
975 second selector will only represent an
<code>a
</code> element with
976 an
<code>href
</code> attribute having the exact value
977 "http://www.w3.org/".
</p>
979 <pre>a[rel~=
"copyright"]
980 a[
href=
"http://www.w3.org/"]
</pre>
982 <p>The following selector represents a
<code>link
</code> element
983 whose
<code>hreflang
</code> attribute is exactly
"fr".
</p>
985 <pre>link[hreflang=fr]
</pre>
987 <p>The following selector represents a
<code>link
</code> element for
988 which the values of the
<code>hreflang
</code> attribute begins with
989 "en", including
"en",
"en-US", and
"en-cockney":
</p>
991 <pre>link[hreflang|=
"en"]
</pre>
993 <p>Similarly, the following selectors represents a
994 <code>DIALOGUE
</code> element whenever it has one of two different
995 values for an attribute
<code>character
</code>:
</p>
997 <pre>DIALOGUE[character=romeo]
998 DIALOGUE[character=juliet]
</pre>
1002 <h4><a name=attribute-substrings
></a>6.3.2. Substring matching attribute
1005 <p>Three additional attribute selectors are provided for matching
1006 substrings in the value of an attribute:
</p>
1009 <dt><code>[att^=val]
</code></dt>
1010 <dd>Represents an element with the
<code>att
</code> attribute whose value begins
1011 with the prefix
"val".
</dd>
1012 <dt><code>[att$=val]
</code>
1013 <dd>Represents an element with the
<code>att
</code> attribute whose value ends with
1014 the suffix
"val".
</dd>
1015 <dt><code>[att*=val]
</code>
1016 <dd>Represents an element with the
<code>att
</code> attribute whose value contains
1017 at least one instance of the substring
"val".
</dd>
1020 <p>Attribute values must be identifiers or strings. The
1021 case-sensitivity of attribute names in selectors depends on the
1022 document language.
</p>
1024 <div class=
"example">
1026 <p>The following selector represents an HTML
<code>object
</code>, referencing an
1028 <pre>object[type^=
"image/"]
</pre>
1029 <p>The following selector represents an HTML anchor
<code>a
</code> with an
1030 <code>href
</code> attribute whose value ends with
".html".
</p>
1031 <pre>a[href$=
".html"]
</pre>
1032 <p>The following selector represents an HTML paragraph with a
<code>title
</code>
1033 attribute whose value contains the substring
"hello"</p>
1034 <pre>p[title*=
"hello"]
</pre>
1037 <h4><a name=attrnmsp
>6.3.3. Attribute selectors and namespaces
</a></h4>
1039 <p>Attribute selectors allow an optional namespace component to the
1040 attribute name. A namespace prefix that has been previously declared
1041 may be prepended to the attribute name separated by the namespace
1042 separator
"vertical bar
" (
<code>|
</code>). In keeping with
1043 the Namespaces in the XML recommendation, default namespaces do not
1044 apply to attributes, therefore attribute selectors without a namespace
1045 component apply only to attributes that have no declared namespace
1046 (equivalent to
"<code>|attr</code>"). An asterisk may be used for the
1047 namespace prefix indicating that the selector is to match all
1048 attribute names without regard to the attribute's namespace.
1050 <p>An attribute selector with an attribute name containing a namespace
1051 prefix that has not been previously declared is an
<a
1052 href=
"#Conformance">invalid
</a> selector. The mechanism for declaring
1053 a namespace prefix is left up to the language implementing Selectors.
1054 In CSS, such a mechanism is defined in the General Syntax module.
1056 <div class=
"example">
1057 <p>CSS examples:
</p>
1058 <pre>@namespace foo
"http://www.example.com";
1059 [foo|att=val] { color: blue }
1060 [*|att] { color: yellow }
1061 [|att] { color: green }
1062 [att] { color: green }
</pre>
1064 <p>The first rule will match only elements with the attribute
1065 <code>att
</code> in the
"http://www.example.com" namespace with the
1068 <p>The second rule will match only elements with the attribute
1069 <code>att
</code> regardless of the namespace of the attribute
1070 (including no declared namespace).
</p>
1072 <p>The last two rules are equivalent and will match only elements
1073 with the attribute
<code>att
</code> where the attribute is not
1074 declared to be in a namespace.
</p>
1078 <h4><a name=def-values
>6.3.4. Default attribute values in DTDs
</a></h4>
1080 <p>Attribute selectors represent explicitly set attribute values in
1081 the document tree. Default attribute values may be defined in a DTD or
1082 elsewhere, but cannot always be selected by attribute
1083 selectors. Selectors should be designed so that they work even if the
1084 default values are not included in the document tree.
</p>
1086 <p>More precisely, a UA is
<em>not
</em> required to read an
"external
1087 subset" of the DTD but
<em>is
</em> required to look for default
1088 attribute values in the document's
"internal subset." (See
<a
1089 href=
"#refsXML10">[XML10]
</a> for definitions of these subsets.)
</p>
1091 <p>A UA that recognizes an XML namespace
<a
1092 href=
"#refsXMLNAMES">[XMLNAMES]
</a> is not required to use its
1093 knowledge of that namespace to treat default attribute values as if
1094 they were present in the document. (For example, an XHTML UA is not
1095 required to use its built-in knowledge of the XHTML DTD.)
</p>
1097 <p class=
"note"><strong>Note:
</strong> Typically, implementations
1098 choose to ignore external subsets.
</p>
1100 <div class=
"example">
1103 <p>Consider an element EXAMPLE with an attribute
"notation" that has a
1104 default value of
"decimal". The DTD fragment might be
</p>
1106 <pre class=
"dtd-example"><!ATTLIST EXAMPLE notation (decimal,octal)
"decimal"></pre>
1108 <p>If the style sheet contains the rules
</p>
1110 <pre>EXAMPLE[notation=decimal] { /*... default property settings ...*/ }
1111 EXAMPLE[notation=octal] { /*... other settings...*/ }
</pre>
1113 <p>the first rule will not match elements whose
"notation" attribute
1114 is set by default, i.e. not set explicitly. To catch all cases, the
1115 attribute selector for the default value must be dropped:
</p>
1117 <pre>EXAMPLE { /*... default property settings ...*/ }
1118 EXAMPLE[notation=octal] { /*... other settings...*/ }
</pre>
1120 <p>Here, because the selector
<code>EXAMPLE[notation=octal]
</code> is
1121 more specific than the tag
1122 selector alone, the style declarations in the second rule will override
1123 those in the first for elements that have a
"notation" attribute value
1124 of
"octal". Care has to be taken that all property declarations that
1125 are to apply only to the default case are overridden in the non-default
1126 cases' style rules.
</p>
1130 <h3><a name=class-html
>6.4. Class selectors
</a></h3>
1132 <p>Working with HTML, authors may use the period (U+
002E,
1133 <code>.
</code>) notation as an alternative to the
<code>~=
</code>
1134 notation when representing the
<code>class
</code> attribute. Thus, for
1135 HTML,
<code>div.value
</code> and
<code>div[class~=value]
</code> have
1136 the same meaning. The attribute value must immediately follow the
1137 "period
" (
<code>.
</code>).
</p>
1139 <p>UAs may apply selectors using the period (.) notation in XML
1140 documents if the UA has namespace-specific knowledge that allows it to
1141 determine which attribute is the
"class
" attribute for the
1142 respective namespace. One such example of namespace-specific knowledge
1143 is the prose in the specification for a particular namespace (e.g. SVG
1144 1.0 <a href=
"#refsSVG">[SVG]
</a> describes the
<a
1145 href=
"http://www.w3.org/TR/2001/PR-SVG-20010719/styling.html#ClassAttribute">SVG
1146 "class
" attribute
</a> and how a UA should interpret it, and
1147 similarly MathML
1.01 <a href=
"#refsMATH">[MATH]
</a> describes the
<a
1148 href=
"http://www.w3.org/1999/07/REC-MathML-19990707/chapter2.html#sec2.3.4">MathML
1149 "class
" attribute
</a>.)
</p>
1151 <div class=
"example">
1152 <p>CSS examples:
</p>
1154 <p>We can assign style information to all elements with
1155 <code>class~=
"pastoral"</code> as follows:
</p>
1157 <pre>*.pastoral { color: green } /* all elements with class~=pastoral */
</pre>
1161 <pre>.pastoral { color: green } /* all elements with class~=pastoral */
</pre>
1163 <p>The following assigns style only to H1 elements with
1164 <code>class~=
"pastoral"</code>:
</p>
1166 <pre>H1.pastoral { color: green } /* H1 elements with class~=pastoral */
</pre>
1168 <p>Given these rules, the first H1 instance below would not have
1169 green text, while the second would:
</p>
1171 <pre><H1
>Not green
</H1
>
1172 <H1
class=
"pastoral">Very green
</H1
></pre>
1176 <p>To represent a subset of
"class" values, each value must be preceded
1177 by a
".", in any order.
</P>
1179 <div class=
"example">
1183 <p>The following rule matches any P element whose
"class" attribute
1184 has been assigned a list of
<a
1185 href=
"#whitespace">whitespace
</a>-separated values that includes
1186 "pastoral" and
"marine":
</p>
1188 <pre>p.pastoral.marine { color: green }
</pre>
1190 <p>This rule matches when
<code>class=
"pastoral blue aqua
1191 marine"</code> but does not match for
<code>class=
"pastoral
1196 <p class=
"note"><strong>Note:
</strong> Because CSS gives considerable
1197 power to the
"class" attribute, authors could conceivably design their
1198 own
"document language" based on elements with almost no associated
1199 presentation (such as DIV and SPAN in HTML) and assigning style
1200 information through the
"class" attribute. Authors should avoid this
1201 practice since the structural elements of a document language often
1202 have recognized and accepted meanings and author-defined classes may
1205 <p class=
"note"><strong>Note:
</strong> If an element has multiple
1206 class attributes, their values must be concatenated with spaces
1207 between the values before searching for the class. As of this time the
1208 working group is not aware of any manner in which this situation can
1209 be reached, however, so this behavior is explicitly non-normative in
1210 this specification.
</p>
1212 <h3><a name=id-selectors
>6.5. ID selectors
</a></h3>
1214 <p>Document languages may contain attributes that are declared to be
1215 of type ID. What makes attributes of type ID special is that no two
1216 such attributes can have the same value in a document, regardless of
1217 the type of the elements that carry them; whatever the document
1218 language, an ID typed attribute can be used to uniquely identify its
1219 element. In HTML all ID attributes are named
"id"; XML applications
1220 may name ID attributes differently, but the same restriction
1223 <p>An ID-typed attribute of a document language allows authors to
1224 assign an identifier to one element instance in the document tree. W3C
1225 ID selectors represent an element instance based on its identifier. An
1226 ID selector contains a
"number sign
" (U+
0023,
1227 <code>#
</code>) immediately followed by the ID value, which must be an
1230 <p>Selectors does not specify how a UA knows the ID-typed attribute of
1231 an element. The UA may, e.g., read a document's DTD, have the
1232 information hard-coded or ask the user.
1234 <div class=
"example">
1236 <p>The following ID selector represents an
<code>h1
</code> element
1237 whose ID-typed attribute has the value
"chapter1":
</p>
1238 <pre>h1#chapter1
</pre>
1239 <p>The following ID selector represents any element whose ID-typed
1240 attribute has the value
"chapter1":
</p>
1241 <pre>#chapter1
</pre>
1242 <p>The following selector represents any element whose ID-typed
1243 attribute has the value
"z98y".
</p>
1247 <p class=
"note"><strong>Note.
</strong> In XML
1.0 <a
1248 href=
"#refsXML10">[XML10]
</a>, the information about which attribute
1249 contains an element's IDs is contained in a DTD or a schema. When
1250 parsing XML, UAs do not always read the DTD, and thus may not know
1251 what the ID of an element is (though a UA may have namespace-specific
1252 knowledge that allows it to determine which attribute is the ID
1253 attribute for that namespace). If a style sheet designer knows or
1254 suspects that a UA may not know what the ID of an element is, he
1255 should use normal attribute selectors instead:
1256 <code>[name=p371]
</code> instead of
<code>#p371
</code>. Elements in
1257 XML
1.0 documents without a DTD do not have IDs at all.
</p>
1259 <p>If an element has multiple ID attributes, all of them must be
1260 treated as IDs for that element for the purposes of the ID
1261 selector. Such a situation could be reached using mixtures of xml:id,
1262 DOM3 Core, XML DTDs, and namespace-specific knowledge.
</p>
1264 <h3><a name=pseudo-classes
>6.6. Pseudo-classes
</a></h3>
1266 <p>The pseudo-class concept is introduced to permit selection based on
1267 information that lies outside of the document tree or that cannot be
1268 expressed using the other simple selectors.
</p>
1270 <p>A pseudo-class always consists of a
"colon
"
1271 (
<code>:
</code>) followed by the name of the pseudo-class and
1272 optionally by a value between parentheses.
</p>
1274 <p>Pseudo-classes are allowed in all sequences of simple selectors
1275 contained in a selector. Pseudo-classes are allowed anywhere in
1276 sequences of simple selectors, after the leading type selector or
1277 universal selector (possibly omitted). Pseudo-class names are
1278 case-insensitive. Some pseudo-classes are mutually exclusive, while
1279 others can be applied simultaneously to the same
1280 element. Pseudo-classes may be dynamic, in the sense that an element
1281 may acquire or lose a pseudo-class while a user interacts with the
1285 <h4><a name=dynamic-pseudos
>6.6.1. Dynamic pseudo-classes
</a></h4>
1287 <p>Dynamic pseudo-classes classify elements on characteristics other
1288 than their name, attributes, or content, in principle characteristics
1289 that cannot be deduced from the document tree.
</p>
1291 <p>Dynamic pseudo-classes do not appear in the document source or
1295 <h5>The
<a name=link
>link pseudo-classes: :link and :visited
</a></h5>
1297 <p>User agents commonly display unvisited links differently from
1298 previously visited ones. Selectors
1299 provides the pseudo-classes
<code>:link
</code> and
1300 <code>:visited
</code> to distinguish them:
</p>
1303 <li>The
<code>:link
</code> pseudo-class applies to links that have
1304 not yet been visited.
</li>
1305 <li>The
<code>:visited
</code> pseudo-class applies once the link has
1306 been visited by the user.
</li>
1309 <p>After some amount of time, user agents may choose to return a
1310 visited link to the (unvisited) ':link' state.
</p>
1312 <p>The two states are mutually exclusive.
</p>
1314 <div class=
"example">
1318 <p>The following selector represents links carrying class
1319 <code>external
</code> and already visited:
</p>
1321 <pre>a.external:visited
</pre>
1325 <p class=
"note"><strong>Note:
</strong> It is possible for style sheet
1326 authors to abuse the :link and :visited pseudo-classes to determine
1327 which sites a user has visited without the user's consent.
1329 <p>UAs may therefore treat all links as unvisited links, or implement
1330 other measures to preserve the user's privacy while rendering visited
1331 and unvisited links differently.
</p>
1333 <h5>The
<a name=useraction-pseudos
>user action pseudo-classes
1334 :hover, :active, and :focus
</a></h5>
1336 <p>Interactive user agents sometimes change the rendering in response
1337 to user actions. Selectors provides
1338 three pseudo-classes for the selection of an element the user is
1343 <li>The
<code>:hover
</code> pseudo-class applies while the user
1344 designates an element with a pointing device, but does not activate
1345 it. For example, a visual user agent could apply this pseudo-class
1346 when the cursor (mouse pointer) hovers over a box generated by the
1347 element. User agents not that do not support
<a
1348 href=
"http://www.w3.org/TR/REC-CSS2/media.html#interactive-media-group">interactive
1349 media
</a> do not have to support this pseudo-class. Some conforming
1350 user agents that support
<a
1351 href=
"http://www.w3.org/TR/REC-CSS2/media.html#interactive-media-group">interactive
1352 media
</a> may not be able to support this pseudo-class (e.g., a pen
1353 device that does not detect hovering).
</li>
1355 <li>The
<code>:active
</code> pseudo-class applies while an element
1356 is being activated by the user. For example, between the times the
1357 user presses the mouse button and releases it.
</li>
1359 <li>The
<code>:focus
</code> pseudo-class applies while an element
1360 has the focus (accepts keyboard or mouse events, or other forms of
1365 <p>There may be document language or implementation specific limits on
1366 which elements can become
<code>:active
</code> or acquire
1367 <code>:focus
</code>.
</p>
1369 <p>These pseudo-classes are not mutually exclusive. An element may
1370 match several pseudo-classes at the same time.
</p>
1372 <p>Selectors doesn't define if the parent of an element that is
1373 ':active' or ':hover' is also in that state.
</p>
1375 <div class=
"example">
1377 <pre>a:link /* unvisited links */
1378 a:visited /* visited links */
1379 a:hover /* user hovers */
1380 a:active /* active links */
</pre>
1381 <p>An example of combining dynamic pseudo-classes:
</p>
1384 <p>The last selector matches
<code>a
</code> elements that are in
1385 the pseudo-class :focus and in the pseudo-class :hover.
</p>
1388 <p class=
"note"><strong>Note:
</strong> An element can be both ':visited'
1389 and ':active' (or ':link' and ':active').
</p>
1391 <h4><a name=target-pseudo
>6.6.2. The target pseudo-class :target
</a></h4>
1393 <p>Some URIs refer to a location within a resource. This kind of URI
1394 ends with a
"number sign
" (#) followed by an anchor
1395 identifier (called the fragment identifier).
</p>
1397 <p>URIs with fragment identifiers link to a certain element within the
1398 document, known as the target element. For instance, here is a URI
1399 pointing to an anchor named
<code>section_2
</code> in an HTML
1402 <pre>http://example.com/html/top.html#section_2
</pre>
1404 <p>A target element can be represented by the
<code>:target
</code>
1405 pseudo-class. If the document's URI has no fragment identifier, then
1406 the document has no target element.
</p>
1408 <div class=
"example">
1410 <pre>p.note:target
</pre>
1411 <p>This selector represents a
<code>p
</code> element of class
1412 <code>note
</code> that is the target element of the referring
1416 <div class=
"example">
1418 <p>Here, the
<code>:target
</code> pseudo-class is used to make the
1419 target element red and place an image before it, if there is one:
</p>
1420 <pre>*:target { color : red }
1421 *:target::before { content : url(target.png) }
</pre>
1424 <h4><a name=lang-pseudo
>6.6.3. The language pseudo-class :lang
</a></h4>
1426 <p>If the document language specifies how the human language of an
1427 element is determined, it is possible to write selectors that
1428 represent an element based on its language. For example, in HTML
<a
1429 href=
"#refsHTML4">[HTML4]
</a>, the language is determined by a
1430 combination of the
<code>lang
</code> attribute, the
<code>meta
</code>
1431 element, and possibly by information from the protocol (such as HTTP
1432 headers). XML uses an attribute called
<code>xml:lang
</code>, and
1433 there may be other document language-specific methods for determining
1436 <p>The pseudo-class
<code>:lang(C)
</code> represents an element that
1437 is in language C. Whether an element is represented by a
1438 <code>:lang()
</code> selector is based solely on the identifier C
1439 being either equal to, or a hyphen-separated substring of, the
1440 element's language value, in the same way as if performed by the
<a
1441 href=
"#attribute-representation">'|='
</a> operator in attribute
1442 selectors. The identifier C does not have to be a valid language
1445 <p>C must not be empty. (If it is, the selector is invalid.)
</p>
1447 <p class=
"note"><strong>Note:
</strong> It is recommended that
1448 documents and protocols indicate language using codes from RFC
3066 <a
1449 href=
"#refsRFC3066">[RFC3066]
</a> or its successor, and by means of
1450 "xml:lang" attributes in the case of XML-based documents
<a
1451 href=
"#refsXML10">[XML10]
</a>. See
<a
1452 href=
"http://www.w3.org/International/questions/qa-lang-2or3.html">
1453 "FAQ: Two-letter or three-letter language codes."</a></p>
1455 <div class=
"example">
1457 <p>The two following selectors represent an HTML document that is in
1458 Belgian, French, or German. The two next selectors represent
1459 <code>q
</code> quotations in an arbitrary element in Belgian, French,
1461 <pre>html:lang(fr-be)
1464 :lang(de)
> q
</pre>
1467 <h4><a name=UIstates
>6.6.4. The UI element states pseudo-classes
</a></h4>
1469 <h5><a name=enableddisabled
>The :enabled and :disabled pseudo-classes
</a></h5>
1471 <p>The
<code>:enabled
</code> pseudo-class allows authors to customize
1472 the look of user interface elements that are enabled
— which the
1473 user can select or activate in some fashion (e.g. clicking on a button
1474 with a mouse). There is a need for such a pseudo-class because there
1475 is no way to programmatically specify the default appearance of say,
1476 an enabled
<code>input
</code> element without also specifying what it
1477 would look like when it was disabled.
</p>
1479 <p>Similar to
<code>:enabled
</code>,
<code>:disabled
</code> allows the
1480 author to specify precisely how a disabled or inactive user interface
1481 element should look.
</p>
1483 <p>Most elements will be neither enabled nor disabled. An element is
1484 enabled if the user can either activate it or transfer the focus to
1485 it. An element is disabled if it could be enabled, but the user cannot
1486 presently activate it or transfer focus to it.
</p>
1489 <h5><a name=checked
>The :checked pseudo-class
</a></h5>
1491 <p>Radio and checkbox elements can be toggled by the user. Some menu
1492 items are
"checked" when the user selects them. When such elements are
1493 toggled
"on" the
<code>:checked
</code> pseudo-class applies. The
1494 <code>:checked
</code> pseudo-class initially applies to such elements
1495 that have the HTML4
<code>selected
</code> and
<code>checked
</code>
1496 attributes as described in
<a
1497 href=
"http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.2.1">Section
1498 17.2.1 of HTML4
</a>, but of course the user can toggle
"off" such
1499 elements in which case the
<code>:checked
</code> pseudo-class would no
1500 longer apply. While the
<code>:checked
</code> pseudo-class is dynamic
1501 in nature, and is altered by user action, since it can also be based
1502 on the presence of the semantic HTML4
<code>selected
</code> and
1503 <code>checked
</code> attributes, it applies to all media.
1506 <h5><a name=indeterminate
>The :indeterminate pseudo-class
</a></h5>
1510 <p>Radio and checkbox elements can be toggled by the user, but are
1511 sometimes in an indeterminate state, neither checked nor unchecked.
1512 This can be due to an element attribute, or DOM manipulation.
</p>
1514 <p>A future version of this specification may introduce an
1515 <code>:indeterminate
</code> pseudo-class that applies to such elements.
1516 <!--While the <code>:indeterminate</code> pseudo-class is dynamic in
1517 nature, and is altered by user action, since it can also be based on
1518 the presence of an element attribute, it applies to all media.</p>
1520 <p>Components of a radio-group initialized with no pre-selected choice
1521 are an example of :indeterminate state.--></p>
1526 <h4><a name=structural-pseudos
>6.6.5. Structural pseudo-classes
</a></h4>
1528 <p>Selectors introduces the concept of
<dfn>structural
1529 pseudo-classes
</dfn> to permit selection based on extra information that lies in
1530 the document tree but cannot be represented by other simple selectors or
1533 <p>Note that standalone pieces of PCDATA (text nodes in the DOM) are
1534 not counted when calculating the position of an element in the list of
1535 children of its parent. When calculating the position of an element in
1536 the list of children of its parent, the index numbering starts at
1.
1539 <h5><a name=root-pseudo
>:root pseudo-class
</a></h5>
1541 <p>The
<code>:root
</code> pseudo-class represents an element that is
1542 the root of the document. In HTML
4, this is always the
1543 <code>HTML
</code> element.
1546 <h5><a name=nth-child-pseudo
>:nth-child() pseudo-class
</a></h5>
1549 <code>:nth-child(
<var>a
</var><code>n
</code>+
<var>b
</var>)
</code>
1550 pseudo-class notation represents an element that has
1551 <var>a
</var><code>n
</code>+
<var>b
</var>-
1 siblings
1552 <strong>before
</strong> it in the document tree, for a given positive
1553 integer or zero value of
<code>n
</code>, and has a parent element. In
1554 other words, this matches the
<var>b
</var>th child of an element after
1555 all the children have been split into groups of
<var>a
</var> elements
1556 each. For example, this allows the selectors to address every other
1557 row in a table, and could be used to alternate the color
1558 of paragraph text in a cycle of four. The
<var>a
</var> and
1559 <var>b
</var> values must be zero, negative integers or positive
1560 integers. The index of the first child of an element is
1.
1562 <p>In addition to this,
<code>:nth-child()
</code> can take
1563 '
<code>odd
</code>' and '
<code>even
</code>' as arguments instead.
1564 '
<code>odd
</code>' has the same signification as
<code>2n+
1</code>,
1565 and '
<code>even
</code>' has the same signification as
<code>2n
</code>.
1568 <div class=
"example">
1570 <pre>tr:nth-child(
2n+
1) /* represents every odd row of an HTML table */
1571 tr:nth-child(odd) /* same */
1572 tr:nth-child(
2n) /* represents every even row of an HTML table */
1573 tr:nth-child(even) /* same */
1575 /* Alternate paragraph colours in CSS */
1576 p:nth-child(
4n+
1) { color: navy; }
1577 p:nth-child(
4n+
2) { color: green; }
1578 p:nth-child(
4n+
3) { color: maroon; }
1579 p:nth-child(
4n+
4) { color: purple; }
</pre>
1582 <p>When
<var>a
</var>=
0, no repeating is used, so for example
1583 <code>:nth-child(
0n+
5)
</code> matches only the fifth child. When
1584 <var>a
</var>=
0, the
<var>a
</var><code>n
</code> part need not be
1585 included, so the syntax simplifies to
1586 <code>:nth-child(
<var>b
</var>)
</code> and the last example simplifies
1587 to
<code>:nth-child(
5)
</code>.
1589 <div class=
"example">
1591 <pre>foo:nth-child(
0n+
1) /* represents an element foo, first child of its parent element */
1592 foo:nth-child(
1) /* same */
</pre>
1595 <p>When
<var>a
</var>=
1, the number may be omitted from the rule.
1597 <div class=
"example">
1599 <p>The following selectors are therefore equivalent:
</p>
1600 <pre>bar:nth-child(
1n+
0) /* represents all bar elements, specificity (
0,
1,
1) */
1601 bar:nth-child(n+
0) /* same */
1602 bar:nth-child(n) /* same */
1603 bar /* same but lower specificity (
0,
0,
1) */
</pre>
1606 <p>If
<var>b
</var>=
0, then every
<var>a
</var>th element is picked. In
1607 such a case, the
<var>b
</var> part may be omitted.
1609 <div class=
"example">
1611 <pre>tr:nth-child(
2n+
0) /* represents every even row of an HTML table */
1612 tr:nth-child(
2n) /* same */
</pre>
1615 <p>If both
<var>a
</var> and
<var>b
</var> are equal to zero, the
1616 pseudo-class represents no element in the document tree.
</p>
1618 <p>The value
<var>a
</var> can be negative, but only the positive
1619 values of
<var>a
</var><code>n
</code>+
<var>b
</var>, for
1620 <code>n
</code>≥0, may represent an element in the document
1623 <div class=
"example">
1625 <pre>html|tr:nth-child(-n+
6) /* represents the
6 first rows of XHTML tables */
</pre>
1628 <p>When the value
<var>b
</var> is negative, the
"+" character in the
1629 expression must be removed (it is effectively replaced by the
"-"
1630 character indicating the negative value of
<var>b
</var>).
</p>
1632 <div class=
"example">
1634 <pre>:nth-child(
10n-
1) /* represents the
9th,
19th,
29th, etc, element */
1635 :nth-child(
10n+
9) /* Same */
1636 :nth-child(
10n+-
1) /* Syntactically invalid, and would be ignored */
</pre>
1640 <h5><a name=nth-last-child-pseudo
>:nth-last-child() pseudo-class
</a></h5>
1642 <p>The
<code>:nth-last-child(
<var>a
</var>n+
<var>b
</var>)
</code>
1643 pseudo-class notation represents an element that has
1644 <var>a
</var><code>n
</code>+
<var>b
</var>-
1 siblings
1645 <strong>after
</strong> it in the document tree, for a given positive
1646 integer or zero value of
<code>n
</code>, and has a parent element. See
1647 <code>:nth-child()
</code> pseudo-class for the syntax of its argument.
1648 It also accepts the '
<code>even
</code>' and '
<code>odd
</code>' values
1652 <div class=
"example">
1654 <pre>tr:nth-last-child(-n+
2) /* represents the two last rows of an HTML table */
1656 foo:nth-last-child(odd) /* represents all odd foo elements in their parent element,
1657 counting from the last one */
</pre>
1661 <h5><a name=nth-of-type-pseudo
>:nth-of-type() pseudo-class
</a></h5>
1663 <p>The
<code>:nth-of-type(
<var>a
</var>n+
<var>b
</var>)
</code>
1664 pseudo-class notation represents an element that has
1665 <var>a
</var><code>n
</code>+
<var>b
</var>-
1 siblings with the same
1666 element name
<strong>before
</strong> it in the document tree, for a
1667 given zero or positive integer value of
<code>n
</code>, and has a
1668 parent element. In other words, this matches the
<var>b
</var>th child
1669 of that type after all the children of that type have been split into
1670 groups of a elements each. See
<code>:nth-child()
</code> pseudo-class
1671 for the syntax of its argument. It also accepts the
1672 '
<code>even
</code>' and '
<code>odd
</code>' values.
1675 <div class=
"example">
1677 <p>This allows an author to alternate the position of floated images:
</p>
1678 <pre>img:nth-of-type(
2n+
1) { float: right; }
1679 img:nth-of-type(
2n) { float: left; }
</pre>
1683 <h5><a name=nth-last-of-type-pseudo
>:nth-last-of-type() pseudo-class
</a></h5>
1685 <p>The
<code>:nth-last-of-type(
<var>a
</var>n+
<var>b
</var>)
</code>
1686 pseudo-class notation represents an element that has
1687 <var>a
</var><code>n
</code>+
<var>b
</var>-
1 siblings with the same
1688 element name
<strong>after
</strong> it in the document tree, for a
1689 given zero or positive integer value of
<code>n
</code>, and has a
1690 parent element. See
<code>:nth-child()
</code> pseudo-class for the
1691 syntax of its argument. It also accepts the '
<code>even
</code>' and '
<code>odd
</code>' values.
1694 <div class=
"example">
1696 <p>To represent all
<code>h2
</code> children of an XHTML
1697 <code>body
</code> except the first and last, one could use the
1698 following selector:
</p>
1699 <pre>body
> h2:nth-of-type(n+
2):nth-last-of-type(n+
2)
</pre>
1700 <p>In this case, one could also use
<code>:not()
</code>, although the
1701 selector ends up being just as long:
</p>
1702 <pre>body
> h2:not(:first-of-type):not(:last-of-type)
</pre>
1706 <h5><a name=first-child-pseudo
>:first-child pseudo-class
</a></h5>
1708 <p>Same as
<code>:nth-child(
1)
</code>. The
<code>:first-child
</code> pseudo-class
1709 represents an element that is the first child of some other element.
1712 <div class=
"example">
1714 <p>The following selector represents a
<code>p
</code> element that is
1715 the first child of a
<code>div
</code> element:
</p>
1716 <pre>div
> p:first-child
</pre>
1717 <p>This selector can represent the
<code>p
</code> inside the
1718 <code>div
</code> of the following fragment:
</p>
1719 <pre><p
> The last P before the note.
</p
>
1720 <div
class=
"note">
1721 <p
> The first P inside the note.
</p
>
1722 </div
></pre>but cannot represent the second
<code>p
</code> in the following
1724 <pre><p
> The last P before the note.
</p
>
1725 <div
class=
"note">
1726 <h2
> Note
</h2
>
1727 <p
> The first P inside the note.
</p
>
1729 <p>The following two selectors are usually equivalent:
</p>
1730 <pre>*
> a:first-child /* a is first child of any element */
1731 a:first-child /* Same (assuming a is not the root element) */
</pre>
1734 <h5><a name=last-child-pseudo
>:last-child pseudo-class
</a></h5>
1736 <p>Same as
<code>:nth-last-child(
1)
</code>. The
<code>:last-child
</code> pseudo-class
1737 represents an element that is the last child of some other element.
1739 <div class=
"example">
1741 <p>The following selector represents a list item
<code>li
</code> that
1742 is the last child of an ordered list
<code>ol
</code>.
1743 <pre>ol
> li:last-child
</pre>
1746 <h5><a name=first-of-type-pseudo
>:first-of-type pseudo-class
</a></h5>
1748 <p>Same as
<code>:nth-of-type(
1)
</code>. The
<code>:first-of-type
</code> pseudo-class
1749 represents an element that is the first sibling of its type in the list of
1750 children of its parent element.
1752 <div class=
"example">
1754 <p>The following selector represents a definition title
1755 <code>dt
</code> inside a definition list
<code>dl
</code>, this
1756 <code>dt
</code> being the first of its type in the list of children of
1757 its parent element.
</p>
1758 <pre>dl dt:first-of-type
</pre>
1759 <p>It is a valid description for the first two
<code>dt
</code>
1760 elements in the following example but not for the third one:
</p>
1762 <dt
>gigogne
</dt
>
1765 <dt
>fus
ée
</dt
>
1766 <dd
>multistage rocket
</dd
>
1767 <dt
>table
</dt
>
1768 <dd
>nest of tables
</dd
>
1774 <h5><a name=last-of-type-pseudo
>:last-of-type pseudo-class
</a></h5>
1776 <p>Same as
<code>:nth-last-of-type(
1)
</code>. The
1777 <code>:last-of-type
</code> pseudo-class represents an element that is
1778 the last sibling of its type in the list of children of its parent
1781 <div class=
"example">
1783 <p>The following selector represents the last data cell
1784 <code>td
</code> of a table row.
</p>
1785 <pre>tr
> td:last-of-type
</pre>
1788 <h5><a name=only-child-pseudo
>:only-child pseudo-class
</a></h5>
1790 <p>Represents an element that has a parent element and whose parent
1791 element has no other element children. Same as
1792 <code>:first-child:last-child
</code> or
1793 <code>:nth-child(
1):nth-last-child(
1)
</code>, but with a lower
1796 <h5><a name=only-of-type-pseudo
>:only-of-type pseudo-class
</a></h5>
1798 <p>Represents an element that has a parent element and whose parent
1799 element has no other element children with the same element name. Same
1800 as
<code>:first-of-type:last-of-type
</code> or
1801 <code>:nth-of-type(
1):nth-last-of-type(
1)
</code>, but with a lower
1805 <h5><a name=empty-pseudo
></a>:empty pseudo-class
</h5>
1807 <p>The
<code>:empty
</code> pseudo-class represents an element that has
1808 no children at all. In terms of the DOM, only element nodes and text
1809 nodes (including CDATA nodes and entity references) whose data has a
1810 non-zero length must be considered as affecting emptiness; comments,
1811 PIs, and other nodes must not affect whether an element is considered
1814 <div class=
"example">
1816 <p><code>p:empty
</code> is a valid representation of the following fragment:
</p>
1817 <pre><p
></p
></pre>
1818 <p><code>foo:empty
</code> is not a valid representation for the
1819 following fragments:
</p>
1820 <pre><foo
>bar
</foo
></pre>
1821 <pre><foo
><bar
>bla
</bar
></foo
></pre>
1822 <pre><foo
>this is not
<bar
>:empty
</bar
></foo
></pre>
1825 <h4><a name=content-selectors
>6.6.6. Blank
</a></h4> <!-- It's the Return of Appendix H!!! Run away! -->
1827 <p>This section intentionally left blank.
</p>
1828 <!-- (used to be :contains()) -->
1830 <h4><a name=negation
></a>6.6.7. The negation pseudo-class
</h4>
1832 <p>The negation pseudo-class,
<code>:not(
<var>X
</var>)
</code>, is a
1833 functional notation taking a
<a href=
"#simple-selectors-dfn">simple
1834 selector
</a> (excluding the negation pseudo-class itself and
1835 pseudo-elements) as an argument. It represents an element that is not
1836 represented by the argument.
1838 <!-- pseudo-elements are not simple selectors, so the above paragraph
1839 may be a bit confusing -->
1841 <div class=
"example">
1843 <p>The following CSS selector matches all
<code>button
</code>
1844 elements in an HTML document that are not disabled.
</p>
1845 <pre>button:not([DISABLED])
</pre>
1846 <p>The following selector represents all but
<code>FOO
</code>
1848 <pre>*:not(FOO)
</pre>
1849 <p>The following group of selectors represents all HTML elements
1851 <pre>html|*:not(:link):not(:visited)
</pre>
1854 <p>Default namespace declarations do not affect the argument of the
1855 negation pseudo-class unless the argument is a universal selector or a
1858 <div class=
"example">
1860 <p>Assuming that the default namespace is bound to
1861 "http://example.com/", the following selector represents all
1862 elements that are not in that namespace:
</p>
1863 <pre>*|*:not(*)
</pre>
1864 <p>The following CSS selector matches any element being hovered,
1865 regardless of its namespace. In particular, it is not limited to
1866 only matching elements in the default namespace that are not being
1867 hovered, and elements not in the default namespace don't match the
1868 rule when they
<em>are
</em> being hovered.
</p>
1869 <pre>*|*:not(:hover)
</pre>
1872 <p class=
"note"><strong>Note
</strong>: the :not() pseudo allows
1873 useless selectors to be written. For instance
<code>:not(*|*)
</code>,
1874 which represents no element at all, or
<code>foo:not(bar)
</code>,
1875 which is equivalent to
<code>foo
</code> but with a higher
1878 <h3><a name=pseudo-elements
>7. Pseudo-elements
</a></h3>
1880 <p>Pseudo-elements create abstractions about the document tree beyond
1881 those specified by the document language. For instance, document
1882 languages do not offer mechanisms to access the first letter or first
1883 line of an element's content. Pseudo-elements allow designers to refer
1884 to this otherwise inaccessible information. Pseudo-elements may also
1885 provide designers a way to refer to content that does not exist in the
1886 source document (e.g., the
<code>::before
</code> and
1887 <code>::after
</code> pseudo-elements give access to generated
1890 <p>A pseudo-element is made of two colons (
<code>::
</code>) followed
1891 by the name of the pseudo-element.
</p>
1893 <p>This
<code>::
</code> notation is introduced by the current document
1894 in order to establish a discrimination between pseudo-classes and
1895 pseudo-elements. For compatibility with existing style sheets, user
1896 agents must also accept the previous one-colon notation for
1897 pseudo-elements introduced in CSS levels
1 and
2 (namely,
1898 <code>:first-line
</code>,
<code>:first-letter
</code>,
1899 <code>:before
</code> and
<code>:after
</code>). This compatibility is
1900 not allowed for the new pseudo-elements introduced in CSS level
3.
</p>
1902 <p>Only one pseudo-element may appear per selector, and if present it
1903 must appear after the sequence of simple selectors that represents the
1904 <a href=
"#subject">subjects
</a> of the selector.
<span class=
"note">A
1905 future version of this specification may allow multiple
1906 pesudo-elements per selector.
</span></p>
1908 <h4><a name=first-line
>7.1. The ::first-line pseudo-element
</a></h4>
1910 <p>The
<code>::first-line
</code> pseudo-element describes the contents
1911 of the first formatted line of an element.
1913 <div class=
"example">
1915 <pre>p::first-line { text-transform: uppercase }
</pre>
1916 <p>The above rule means
"change the letters of the first line of every
1917 paragraph to uppercase".
</p>
1920 <p>The selector
<code>p::first-line
</code> does not match any real
1921 HTML element. It does match a pseudo-element that conforming user
1922 agents will insert at the beginning of every paragraph.
</p>
1924 <p>Note that the length of the first line depends on a number of
1925 factors, including the width of the page, the font size, etc. Thus,
1926 an ordinary HTML paragraph such as:
</p>
1929 <P
>This is a somewhat long HTML
1930 paragraph that will be broken into several
1931 lines. The first line will be identified
1932 by a fictional tag sequence. The other lines
1933 will be treated as ordinary lines in the
1934 paragraph.
</P
>
1937 <p>the lines of which happen to be broken as follows:
1940 THIS IS A SOMEWHAT LONG HTML PARAGRAPH THAT
1941 will be broken into several lines. The first
1942 line will be identified by a fictional tag
1943 sequence. The other lines will be treated as
1944 ordinary lines in the paragraph.
1947 <p>This paragraph might be
"rewritten" by user agents to include the
1948 <em>fictional tag sequence
</em> for
<code>::first-line
</code>. This
1949 fictional tag sequence helps to show how properties are inherited.
</p>
1952 <P
><b><P::first-line
></b> This is a somewhat long HTML
1953 paragraph that
<b></P::first-line
></b> will be broken into several
1954 lines. The first line will be identified
1955 by a fictional tag sequence. The other lines
1956 will be treated as ordinary lines in the
1957 paragraph.
</P
>
1960 <p>If a pseudo-element breaks up a real element, the desired effect
1961 can often be described by a fictional tag sequence that closes and
1962 then re-opens the element. Thus, if we mark up the previous paragraph
1963 with a
<code>span
</code> element:
</p>
1966 <P
><b><SPAN
class=
"test"></b> This is a somewhat long HTML
1967 paragraph that will be broken into several
1968 lines.
<b></SPAN
></b> The first line will be identified
1969 by a fictional tag sequence. The other lines
1970 will be treated as ordinary lines in the
1971 paragraph.
</P
>
1974 <p>the user agent could simulate start and end tags for
1975 <code>span
</code> when inserting the fictional tag sequence for
1976 <code>::first-line
</code>.
1979 <P
><P::first-line
><b><SPAN
class=
"test"></b> This is a
1981 paragraph that will
<b></SPAN
></b></P::first-line
><b><SPAN
class=
"test"></b> be
1983 lines.
<b></SPAN
></b> The first line will be identified
1984 by a fictional tag sequence. The other lines
1985 will be treated as ordinary lines in the
1986 paragraph.
</P
>
1989 <p>In CSS, the
<code>::first-line
</code> pseudo-element can only be
1990 attached to a block-level element, an inline-block, a table-caption,
1991 or a table-cell.
</p>
1993 <p><a name=
"first-formatted-line"></a>The
"first formatted line" of an
1994 element may occur inside a
1995 block-level descendant in the same flow (i.e., a block-level
1996 descendant that is not positioned and not a float). E.g., the first
1997 line of the
<code>div
</code> in
<code><DIV
><P
>This
1998 line...
</P
></DIV
></code> is the first line of the
<code>p
</code> (assuming
1999 that both
<code>p
</code> and
<code>div
</code> are block-level).
2001 <p>The first line of a table-cell or inline-block cannot be the first
2002 formatted line of an ancestor element. Thus, in
<code><DIV
><P
2003 STYLE=
"display: inline-block">Hello
<BR
>Goodbye
</P
>
2004 etcetera
</DIV
></code> the first formatted line of the
2005 <code>div
</code> is not the line
"Hello".
2007 <p class=
"note">Note that the first line of the
<code>p
</code> in this
2008 fragment:
<code><p>
<br
>First...
</code> doesn't contain any
2009 letters (assuming the default style for
<code>br
</code> in HTML
2010 4). The word
"First" is not on the first formatted line.
2012 <p>A UA should act as if the fictional start tags of the
2013 <code>::first-line
</code> pseudo-elements were nested just inside the
2014 innermost enclosing block-level element. (Since CSS1 and CSS2 were
2015 silent on this case, authors should not rely on this behavior.) Here
2016 is an example. The fictional tag sequence for
</p>
2020 <P
>First paragraph
</P
>
2021 <P
>Second paragraph
</P
>
2029 <P
><DIV::first-line
><P::first-line
>First paragraph
</P::first-line
></DIV::first-line
></P
>
2030 <P
><P::first-line
>Second paragraph
</P::first-line
></P
>
2034 <p>The
<code>::first-line
</code> pseudo-element is similar to an
2035 inline-level element, but with certain restrictions. In CSS, the
2036 following properties apply to a
<code>::first-line
</code>
2037 pseudo-element: font properties, color property, background
2038 properties, 'word-spacing', 'letter-spacing', 'text-decoration',
2039 'vertical-align', 'text-transform', 'line-height'. UAs may apply other
2040 properties as well.
</p>
2043 <h4><a name=first-letter
>7.2. The ::first-letter pseudo-element
</a></h4>
2045 <p>The
<code>::first-letter
</code> pseudo-element represents the first
2046 letter of the first line of a block, if it is not preceded by any
2047 other content (such as images or inline tables) on its line. The
2048 ::first-letter pseudo-element may be used for
"initial caps" and
"drop
2049 caps", which are common typographical effects. This type of initial
2050 letter is similar to an inline-level element if its 'float' property
2051 is 'none'; otherwise, it is similar to a floated element.
</p>
2053 <p>In CSS, these are the properties that apply to
<code>::first-letter
</code>
2054 pseudo-elements: font properties, 'text-decoration', 'text-transform',
2055 'letter-spacing', 'word-spacing' (when appropriate), 'line-height',
2056 'float', 'vertical-align' (only if 'float' is 'none'), margin
2057 properties, padding properties, border properties, color property,
2058 background properties. UAs may apply other properties as well. To
2059 allow UAs to render a typographically correct drop cap or initial cap,
2060 the UA may choose a line-height, width and height based on the shape
2061 of the letter, unlike for normal elements.
</p>
2063 <div class=
"example">
2065 <p>This example shows a possible rendering of an initial cap. Note
2066 that the 'line-height' that is inherited by the
<code>::first-letter
</code>
2067 pseudo-element is
1.1, but the UA in this example has computed the
2068 height of the first letter differently, so that it doesn't cause any
2069 unnecessary space between the first two lines. Also note that the
2070 fictional start tag of the first letter is inside the
<span>span
</span>, and thus
2071 the font weight of the first letter is normal, not bold as the
<span>span
</span>:
2073 p { line-height:
1.1 }
2074 p::first-letter { font-size:
3em; font-weight: normal }
2075 span { font-weight: bold }
2077 <p
><span
>Het hemelsche
</span
> gerecht heeft zich ten lange lesten
<br
>
2078 Erbarremt over my en mijn benaeuwde vesten
<br
>
2079 En arme burgery, en op mijn volcx gebed
<br
>
2080 En dagelix geschrey de bange stad ontzet.
2082 <div class=
"figure">
2083 <p><img src=
"initial-cap.png" alt=
"Image illustrating the ::first-letter pseudo-element">
2087 <div class=
"example">
2088 <p>The following CSS will make a drop cap initial letter span about two lines:
</p>
2091 <!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.01//EN">
2094 <TITLE
>Drop cap initial letter
</TITLE
>
2095 <STYLE
type=
"text/css">
2096 P { font-size:
12pt; line-height:
1.2 }
2097 P::first-letter { font-size:
200%; font-weight: bold; float: left }
2098 SPAN { text-transform: uppercase }
2102 <P
><SPAN
>The first
</SPAN
> few words of an article
2103 in The Economist.
</P
>
2108 <p>This example might be formatted as follows:
</p>
2110 <div class=
"figure">
2111 <P><img src=
"first-letter.gif" alt=
"Image illustrating the combined effect of the ::first-letter and ::first-line pseudo-elements"></p>
2114 <p>The
<span class=
"index-inst" title=
"fictional tag
2115 sequence">fictional tag sequence
</span> is:
</p>
2120 <P::first-letter
>
2122 </P::first-letter
>he first
2124 few words of an article in the Economist.
2128 <p>Note that the
<code>::first-letter
</code> pseudo-element tags abut
2129 the content (i.e., the initial character), while the ::first-line
2130 pseudo-element start tag is inserted right after the start tag of the
2131 block element.
</p> </div>
2133 <p>In order to achieve traditional drop caps formatting, user agents
2134 may approximate font sizes, for example to align baselines. Also, the
2135 glyph outline may be taken into account when formatting.
</p>
2137 <p>Punctuation (i.e, characters defined in Unicode in the
"open" (Ps),
2138 "close" (Pe),
"initial" (Pi).
"final" (Pf) and
"other" (Po)
2139 punctuation classes), that precedes or follows the first letter should
2140 be included.
<a href=
"#refsUNICODE">[UNICODE]
</a></p>
2142 <div class=
"figure">
2143 <P><img src=
"first-letter2.gif" alt=
"Quotes that precede the
2144 first letter should be included."></p>
2147 <p>The
<code>::first-letter
</code> also applies if the first letter is
2148 in fact a digit, e.g., the
"6" in
"67 million dollars is a lot of
2151 <p>In CSS, the
<code>::first-letter
</code> pseudo-element applies to
2152 block, list-item, table-cell, table-caption, and inline-block
2153 elements.
<span class=
"note">A future version of this specification
2154 may allow this pesudo-element to apply to more element
2157 <p>The
<code>::first-letter
</code> pseudo-element can be used with all
2158 such elements that contain text, or that have a descendant in the same
2159 flow that contains text. A UA should act as if the fictional start tag
2160 of the ::first-letter pseudo-element is just before the first text of
2161 the element, even if that first text is in a descendant.
</p>
2163 <div class=
"example">
2165 <p>The fictional tag sequence for this HTMLfragment:
2167 <p
>The first text.
</pre>
2170 <p
><div::first-letter
><p::first-letter
>T
</...
></...
>he first text.
</pre>
2173 <p>The first letter of a table-cell or inline-block cannot be the
2174 first letter of an ancestor element. Thus, in
<code><DIV
><P
2175 STYLE=
"display: inline-block">Hello
<BR
>Goodbye
</P
>
2176 etcetera
</DIV
></code> the first letter of the
<code>div
</code> is not the
2177 letter
"H". In fact, the
<code>div
</code> doesn't have a first letter.
2179 <p>The first letter must occur on the
<a
2180 href=
"#first-formatted-line">first formatted line.
</a> For example, in
2181 this fragment:
<code><p>
<br
>First...
</code> the first line
2182 doesn't contain any letters and
<code>::first-letter
</code> doesn't
2183 match anything (assuming the default style for
<code>br
</code> in HTML
2184 4). In particular, it does not match the
"F" of
"First."
2186 <p>In CSS, if an element is a list item ('display: list-item'), the
2187 <code>::first-letter
</code> applies to the first letter in the
2188 principal box after the marker. UAs may ignore
2189 <code>::first-letter
</code> on list items with 'list-style-position:
2190 inside'. If an element has
<code>::before
</code> or
2191 <code>::after
</code> content, the
<code>::first-letter
</code> applies
2192 to the first letter of the element
<em>including
</em> that content.
2194 <div class=
"example">
2196 <p>After the rule 'p::before {content:
"Note: "}', the selector
2197 'p::first-letter' matches the
"N" of
"Note".
</p>
2200 <p>Some languages may have specific rules about how to treat certain
2201 letter combinations. In Dutch, for example, if the letter combination
2202 "ij" appears at the beginning of a word, both letters should be
2203 considered within the
<code>::first-letter
</code> pseudo-element.
2205 <p>If the letters that would form the ::first-letter are not in the
2206 same element, such as
"'T" in
<code><p
>'
<em
>T...
</code>, the UA
2207 may create a ::first-letter pseudo-element from one of the elements,
2208 both elements, or simply not create a pseudo-element.
</p>
2210 <p>Similarly, if the first letter(s) of the block are not at the start
2211 of the line (for example due to bidirectional reordering), then the UA
2212 need not create the pseudo-element(s).
2214 <div class=
"example">
2216 <p><a name=
"overlapping-example">The following example
</a> illustrates
2217 how overlapping pseudo-elements may interact. The first letter of
2218 each P element will be green with a font size of '
24pt'. The rest of
2219 the first formatted line will be 'blue' while the rest of the
2220 paragraph will be 'red'.
</p>
2222 <pre>p { color: red; font-size:
12pt }
2223 p::first-letter { color: green; font-size:
200% }
2224 p::first-line { color: blue }
2226 <P
>Some text that ends up on two lines
</P
></pre>
2228 <p>Assuming that a line break will occur before the word
"ends", the
2229 <span class=
"index-inst" title=
"fictional tag sequence">fictional tag
2230 sequence
</span> for this fragment might be:
</p>
2233 <P::first-line
>
2234 <P::first-letter
>
2236 </P::first-letter
>ome text that
2237 </P::first-line
>
2238 ends up on two lines
2241 <p>Note that the
<code>::first-letter
</code> element is inside the
<code>::first-line
</code>
2242 element. Properties set on
<code>::first-line
</code> are inherited by
2243 <code>::first-letter
</code>, but are overridden if the same property is set on
2244 <code>::first-letter
</code>.
</p>
2248 <h4><a name=UIfragments
>7.3.
</a> <a name=selection
>The ::selection pseudo-element
</a></h4>
2250 <p>The
<code>::selection
</code> pseudo-element applies to the portion
2251 of a document that has been highlighted by the user. This also
2252 applies, for example, to selected text within an editable text
2253 field. This pseudo-element should not be confused with the
<code><a
2254 href=
"#checked">:checked
</a></code> pseudo-class (which used to be
2255 named
<code>:selected
</code>)
2257 <p>Although the
<code>::selection
</code> pseudo-element is dynamic in
2258 nature, and is altered by user action, it is reasonable to expect that
2259 when a UA re-renders to a static medium (such as a printed page, see
2260 <a href=
"#refsCSS21">[CSS21]
</a>) which was originally rendered to a
2261 dynamic medium (like screen), the UA may wish to transfer the current
2262 <code>::selection
</code> state to that other medium, and have all the
2263 appropriate formatting and rendering take effect as well. This is not
2264 required
— UAs may omit the
<code>::selection
</code>
2265 pseudo-element for static media.
2267 <p>These are the CSS properties that apply to
<code>::selection
</code>
2268 pseudo-elements: color, background, cursor (optional), outline
2269 (optional). The computed value of the 'background-image' property on
2270 <code>::selection
</code> may be ignored.
2273 <h4><a name=gen-content
>7.4. The ::before and ::after pseudo-elements
</a></h4>
2275 <p>The
<code>::before
</code> and
<code>::after
</code> pseudo-elements
2276 can be used to describe generated content before or after an element's
2277 content. They are explained in CSS
2.1 <a
2278 href=
"#refsCSS21">[CSS21]
</a>.
</p>
2280 <p>When the
<code>::first-letter
</code> and
<code>::first-line
</code>
2281 pseudo-elements are combined with
<code>::before
</code> and
2282 <code>::after
</code>, they apply to the first letter or line of the
2283 element including the inserted text.
</p>
2285 <h2><a name=combinators
>8. Combinators
</a></h2>
2287 <h3><a name=descendant-combinators
>8.1. Descendant combinator
</a></h3>
2289 <p>At times, authors may want selectors to describe an element that is
2290 the descendant of another element in the document tree (e.g.,
"an
2291 <code>EM</code> element that is contained within an <code>H1</code>
2292 element"). Descendant combinators express such a relationship. A
2293 descendant combinator is
<a href=
"#whitespace">white space
</a> that
2294 separates two sequences of simple selectors. A selector of the form
2295 "<code>A B</code>" represents an element
<code>B
</code> that is an
2296 arbitrary descendant of some ancestor element
<code>A
</code>.
2298 <div class=
"example">
2300 <p>For example, consider the following selector:
</p>
2302 <p>It represents an
<code>em
</code> element being the descendant of
2303 an
<code>h1
</code> element. It is a correct and valid, but partial,
2304 description of the following fragment:
</p>
2305 <pre><h1
>This
<span
class=
"myclass">headline
2306 is
<em
>very
</em
> important
</span
></h1
></pre>
2307 <p>The following selector:
</p>
2309 <p>represents a
<code>p
</code> element that is a grandchild or later
2310 descendant of a
<code>div
</code> element. Note the whitespace on
2311 either side of the
"*" is not part of the universal selector; the
2312 whitespace is a combinator indicating that the DIV must be the
2313 ancestor of some element, and that that element must be an ancestor
2315 <p>The following selector, which combines descendant combinators and
2316 <a href=
"#attribute-selectors">attribute selectors
</a>, represents an
2317 element that (
1) has the
<code>href
</code> attribute set and (
2) is
2318 inside a
<code>p
</code> that is itself inside a
<code>div
</code>:
</p>
2319 <pre>div p *[href]
</pre>
2322 <h3><a name=child-combinators
>8.2. Child combinators
</a></h3>
2324 <p>A
<dfn>child combinator
</dfn> describes a childhood relationship
2325 between two elements. A child combinator is made of the
2326 "greater-than sign
" (
<code>></code>) character and
2327 separates two sequences of simple selectors.
2330 <div class=
"example">
2332 <p>The following selector represents a
<code>p
</code> element that is
2333 child of
<code>body
</code>:
</p>
2334 <pre>body
> p
</pre>
2335 <p>The following example combines descendant combinators and child
2337 <pre>div ol
>li p
</pre><!-- LEAVE THOSE SPACES OUT! see below -->
2338 <p>It represents a
<code>p
</code> element that is a descendant of an
2339 <code>li
</code> element; the
<code>li
</code> element must be the
2340 child of an
<code>ol
</code> element; the
<code>ol
</code> element must
2341 be a descendant of a
<code>div
</code>. Notice that the optional white
2342 space around the
">" combinator has been left out.
</p>
2345 <p>For information on selecting the first child of an element, please
2346 see the section on the
<code><a
2347 href=
"#structural-pseudos">:first-child
</a></code> pseudo-class
2350 <h3><a name=sibling-combinators
>8.3. Sibling combinators
</a></h3>
2352 <p>There are two different sibling combinators: the adjacent sibling
2353 combinator and the general sibling combinator. In both cases,
2354 non-element nodes (e.g. text between elements) are ignored when
2355 considering adjacency of elements.
</p>
2357 <h4><a name=adjacent-sibling-combinators
>8.3.1. Adjacent sibling combinator
</a></h4>
2359 <p>The adjacent sibling combinator is made of the
"plus
2360 sign
" (U+
002B,
<code>+
</code>) character that separates two
2361 sequences of simple selectors. The elements represented by the two
2362 sequences share the same parent in the document tree and the element
2363 represented by the first sequence immediately precedes the element
2364 represented by the second one.
</p>
2366 <div class=
"example">
2368 <p>The following selector represents a
<code>p
</code> element
2369 immediately following a
<code>math
</code> element:
</p>
2371 <p>The following selector is conceptually similar to the one in the
2372 previous example, except that it adds an attribute selector
— it
2373 adds a constraint to the
<code>h1
</code> element, that it must have
2374 <code>class=
"opener"</code>:
</p>
2375 <pre>h1.opener + h2
</pre>
2379 <h4><a name=general-sibling-combinators
>8.3.2. General sibling combinator
</a></h4>
2381 <p>The general sibling combinator is made of the
"tilde
"
2382 (U+
007E,
<code>~
</code>) character that separates two sequences of
2383 simple selectors. The elements represented by the two sequences share
2384 the same parent in the document tree and the element represented by
2385 the first sequence precedes (not necessarily immediately) the element
2386 represented by the second one.
</p>
2388 <div class=
"example">
2391 <p>represents a
<code>pre
</code> element following an
<code>h1
</code>. It
2392 is a correct and valid, but partial, description of:
</p>
2393 <pre><h1
>Definition of the function a
</h1
>
2394 <p
>Function a(x) has to be applied to all figures in the table.
</p
>
2395 <pre
>function a(x) =
12x/
13.5</pre
></pre>
2398 <h2><a name=specificity
>9. Calculating a selector's specificity
</a></h2>
2400 <p>A selector's specificity is calculated as follows:
</p>
2403 <li>count the number of ID selectors in the selector (= a)
</li>
2404 <li>count the number of class selectors, attributes selectors, and pseudo-classes in the selector (= b)
</li>
2405 <li>count the number of element names in the selector (= c)
</li>
2406 <li>ignore pseudo-elements
</li>
2409 <p>Selectors inside
<a href=
"#negation">the negation pseudo-class
</a>
2410 are counted like any other, but the negation itself does not count as
2413 <p>Concatenating the three numbers a-b-c (in a number system with a
2414 large base) gives the specificity.
</p>
2416 <div class=
"example">
2418 <pre>* /* a=
0 b=
0 c=
0 -
> specificity =
0 */
2419 LI /* a=
0 b=
0 c=
1 -
> specificity =
1 */
2420 UL LI /* a=
0 b=
0 c=
2 -
> specificity =
2 */
2421 UL OL+LI /* a=
0 b=
0 c=
3 -
> specificity =
3 */
2422 H1 + *[REL=up] /* a=
0 b=
1 c=
1 -
> specificity =
11 */
2423 UL OL LI.red /* a=
0 b=
1 c=
3 -
> specificity =
13 */
2424 LI.red.level /* a=
0 b=
2 c=
1 -
> specificity =
21 */
2425 #x34y /* a=
1 b=
0 c=
0 -
> specificity =
100 */
2426 #s12:not(FOO) /* a=
1 b=
0 c=
1 -
> specificity =
101 */
2430 <p class=
"note"><strong>Note:
</strong> the specificity of the styles
2431 specified in an HTML
<code>style
</code> attribute is described in CSS
2432 2.1.
<a href=
"#refsCSS21">[CSS21]
</a>.
</p>
2434 <h2><a name=w3cselgrammar
>10. The grammar of Selectors
</a></h2>
2436 <h3><a name=grammar
>10.1. Grammar
</a></h3>
2438 <p>The grammar below defines the syntax of Selectors. It is globally
2439 LL(
1) and can be locally LL(
2) (but note that most UA's should not use
2440 it directly, since it doesn't express the parsing conventions). The
2441 format of the productions is optimized for human consumption and some
2442 shorthand notations beyond Yacc (see
<a href=
"#refsYACC">[YACC]
</a>)
2446 <li><b>*
</b>:
0 or more
2447 <li><b>+
</b>:
1 or more
2448 <li><b>?
</b>:
0 or
1
2449 <li><b>|
</b>: separates alternatives
2450 <li><b>[ ]
</b>: grouping
</li>
2453 <p>The productions are:
</p>
2455 <pre>selectors_group
2456 : selector [ COMMA S* selector ]*
2460 : simple_selector_sequence [ combinator simple_selector_sequence ]*
2464 /* combinators can be surrounded by white space */
2465 : PLUS S* | GREATER S* | TILDE S* | S+
2468 simple_selector_sequence
2469 : [ type_selector | universal ]
2470 [ HASH | class | attrib | pseudo | negation ]*
2471 | [ HASH | class | attrib | pseudo | negation ]+
2475 : [ namespace_prefix ]? element_name
2479 : [ IDENT | '*' ]? '|'
2487 : [ namespace_prefix ]? '*'
2495 : '[' S* [ namespace_prefix ]? IDENT S*
2501 DASHMATCH ] S* [ IDENT | STRING ] S*
2506 /* '::' starts a pseudo-element, ':' a pseudo-class */
2507 /* Exceptions: :first-line, :first-letter, :before and :after. */
2508 /* Note that pseudo-elements are restricted to one per selector and */
2509 /* occur only in the last simple_selector_sequence. */
2510 : ':' ':'? [ IDENT | functional_pseudo ]
2514 : FUNCTION S* expression ')'
2518 /* In CSS3, the expressions are identifiers, strings, */
2519 /* or of the form
"an+b" */
2520 : [ [ PLUS | '-' | DIMENSION | NUMBER | STRING | IDENT ] S* ]+
2524 : NOT S* negation_arg S* ')'
2528 : type_selector | universal | HASH | class | attrib | pseudo
2532 <h3><a name=lex
>10.2. Lexical scanner
</a></h3>
2534 <p>The following is the
<a name=x3
>tokenizer
</a>, written in Flex (see
2535 <a href=
"#refsFLEX">[FLEX]
</a>) notation. The tokenizer is
2536 case-insensitive.
</p>
2538 <p>The two occurrences of
"\377" represent the highest character
2539 number that current versions of Flex can deal with (decimal
255). They
2540 should be read as
"\4177777" (decimal
1114111), which is the highest
2541 possible code point in Unicode/ISO-
10646.
<a
2542 href=
"#refsUNICODE">[UNICODE]
</a></p>
2544 <pre>%option case-insensitive
2546 ident [-]?{nmstart}{nmchar}*
2548 nmstart [_a-z]|{nonascii}|{escape}
2550 unicode \\[
0-
9a-f]{
1,
6}(\r\n|[ \n\r\t\f])?
2551 escape {unicode}|\\[^\n\r\f0-
9a-f]
2552 nmchar [_a-z0-
9-]|{nonascii}|{escape}
2553 num [
0-
9]+|[
0-
9]*\.[
0-
9]+
2554 string {string1}|{string2}
2555 string1 \
"([^\n\r\f\\"]|\\{nl}|{nonascii}|{escape})*\
"
2556 string2 \'([^\n\r\f\\']|\\{nl}|{nonascii}|{escape})*\'
2557 invalid {invalid1}|{invalid2}
2558 invalid1 \"([^\n\r\f\\
"]|\\{nl}|{nonascii}|{escape})*
2559 invalid2 \'([^\n\r\f\\']|\\{nl}|{nonascii}|{escape})*
2565 [ \t\r\n\f]+ return S;
2567 "~=
" return INCLUDES;
2568 "|=
" return DASHMATCH;
2569 "^=
" return PREFIXMATCH;
2570 "$=
" return SUFFIXMATCH;
2571 "*=
" return SUBSTRINGMATCH;
2572 {ident} return IDENT;
2573 {string} return STRING;
2574 {ident}"(
" return FUNCTION;
2575 {num} return NUMBER;
2576 "#
"{name} return HASH;
2578 {w}">" return GREATER;
2579 {w}",
" return COMMA;
2580 {w}"~
" return TILDE;
2582 @{ident} return ATKEYWORD;
2583 {invalid} return INVALID;
2584 {num}% return PERCENTAGE;
2585 {num}{ident} return DIMENSION;
2586 "<!--
" return CDO;
2587 "--
>" return CDC;
2589 "url(
"{w}{string}{w}")
" return URI;
2590 "url(
"{w}([!#$%&*-~]|{nonascii}|{escape})*{w}")
" return URI;
2591 U\+[0-9a-f?]{1,6}(-[0-9a-f]{1,6})? return UNICODE_RANGE;
2593 \/\*[^*]*\*+([^/*][^*]*\*+)*\/ /* ignore comments */
2595 . return *yytext;</pre>
2599 <h2><a name=downlevel>11. Namespaces and down-level clients</a></h2>
2601 <p>An important issue is the interaction of CSS selectors with XML
2602 documents in web clients that were produced prior to this
2603 document. Unfortunately, due to the fact that namespaces must be
2604 matched based on the URI which identifies the namespace, not the
2605 namespace prefix, some mechanism is required to identify namespaces in
2606 CSS by their URI as well. Without such a mechanism, it is impossible
2607 to construct a CSS style sheet which will properly match selectors in
2608 all cases against a random set of XML documents. However, given
2609 complete knowledge of the XML document to which a style sheet is to be
2610 applied, and a limited use of namespaces within the XML document, it
2611 is possible to construct a style sheet in which selectors would match
2612 elements and attributes correctly.</p>
2614 <p>It should be noted that a down-level CSS client will (if it
2615 properly conforms to CSS forward compatible parsing rules) ignore all
2616 <code>@namespace</code> at-rules, as well as all style rules that make
2617 use of namespace qualified element type or attribute selectors. The
2618 syntax of delimiting namespace prefixes in CSS was deliberately chosen
2619 so that down-level CSS clients would ignore the style rules rather
2620 than possibly match them incorrectly.</p>
2622 <p>The use of default namespaces in CSS makes it possible to write
2623 element type selectors that will function in both namespace aware CSS
2624 clients as well as down-level clients. It should be noted that
2625 down-level clients may incorrectly match selectors against XML
2626 elements in other namespaces.</p>
2628 <p>The following are scenarios and examples in which it is possible to
2629 construct style sheets which would function properly in web clients
2630 that do not implement this proposal.</p>
2635 <p>The XML document does not use namespaces.</p>
2639 <li>In this case, it is obviously not necessary to declare or use
2640 namespaces in the style sheet. Standard CSS element type and
2641 attribute selectors will function adequately in a down-level
2644 <li>In a CSS namespace aware client, the default behavior of
2645 element selectors matching without regard to namespace will
2646 function properly against all elements, since no namespaces are
2647 present. However, the use of specific element type selectors that
2648 match only elements that have no namespace ("<code>|name
</code>")
2649 will guarantee that selectors will match only XML elements that do
2650 not have a declared namespace. </li>
2658 <p>The XML document defines a single, default namespace used
2659 throughout the document. No namespace prefixes are used in element
2664 <li>In this case, a down-level client will function as if
2665 namespaces were not used in the XML document at all. Standard CSS
2666 element type and attribute selectors will match against all
2675 <p>The XML document does <b>not</b> use a default namespace, all
2676 namespace prefixes used are known to the style sheet author, and
2677 there is a direct mapping between namespace prefixes and namespace
2678 URIs. (A given prefix may only be mapped to one namespace URI
2679 throughout the XML document; there may be multiple prefixes mapped
2680 to the same URI).</p>
2684 <li>In this case, the down-level client will view and match
2685 element type and attribute selectors based on their fully
2686 qualified name, not the local part as outlined in the <a
2687 href="#typenmsp
">Type selectors and Namespaces</a> section. CSS
2688 selectors may be declared using an escaped colon "<code>\:
</code>"
2689 to describe the fully qualified names, e.g.
2690 "<code>html\:h1
</code>" will match
2691 <code><html:h1></code>. Selectors using the qualified name
2692 will only match XML elements that use the same prefix. Other
2693 namespace prefixes used in the XML that are mapped to the same URI
2694 will not match as expected unless additional CSS style rules are
2695 declared for them.</li>
2697 <li>Note that selectors declared in this fashion will
2698 <em>only</em> match in down-level clients. A CSS namespace aware
2699 client will match element type and attribute selectors based on
2700 the name's local part. Selectors declared with the fully
2701 qualified name will not match (unless there is no namespace prefix
2702 in the fully qualified name).</li>
2710 <p>In other scenarios: when the namespace prefixes used in the XML are
2711 not known in advance by the style sheet author; or a combination of
2712 elements with no namespace are used in conjunction with elements using
2713 a default namespace; or the same namespace prefix is mapped to
2714 <em>different</em> namespace URIs within the same document, or in
2715 different documents; it is impossible to construct a CSS style sheet
2716 that will function properly against all elements in those documents,
2717 unless, the style sheet is written using a namespace URI syntax (as
2718 outlined in this document or similar) and the document is processed by
2719 a CSS and XML namespace aware client.</p>
2721 <h2><a name=profiling>12. Profiles</a></h2>
2723 <p>Each specification using Selectors must define the subset of W3C
2724 Selectors it allows and excludes, and describe the local meaning of
2725 all the components of that subset.</p>
2727 <p>Non normative examples:
2729 <div class="profile
">
2730 <table class="tprofile
">
2733 <th class="title
" colspan=2>Selectors profile</th></tr>
2735 <th>Specification</th>
2736 <td>CSS level 1</td></tr>
2739 <td>type selectors<br>class selectors<br>ID selectors<br>:link,
2740 :visited and :active pseudo-classes<br>descendant combinator
2741 <br>::first-line and ::first-letter pseudo-elements</td></tr>
2746 <p>universal selector<br>attribute selectors<br>:hover and :focus
2747 pseudo-classes<br>:target pseudo-class<br>:lang() pseudo-class<br>all UI
2748 element states pseudo-classes<br>all structural
2749 pseudo-classes<br>negation pseudo-class<br>all
2750 UI element fragments pseudo-elements<br>::before and ::after
2751 pseudo-elements<br>child combinators<br>sibling combinators
2753 <p>namespaces</td></tr>
2755 <th>Extra constraints</th>
2756 <td>only one class selector allowed per sequence of simple
2757 selectors</td></tr></tbody></table><br><br>
2758 <table class="tprofile
">
2761 <th class="title
" colspan=2>Selectors profile</th></tr>
2763 <th>Specification</th>
2764 <td>CSS level 2</td></tr>
2767 <td>type selectors<br>universal selector<br>attribute presence and
2768 values selectors<br>class selectors<br>ID selectors<br>:link, :visited,
2769 :active, :hover, :focus, :lang() and :first-child pseudo-classes
2770 <br>descendant combinator<br>child combinator<br>adjacent sibling
2771 combinator<br>::first-line and ::first-letter pseudo-elements<br>::before
2772 and ::after pseudo-elements</td></tr>
2777 <p>content selectors<br>substring matching attribute
2778 selectors<br>:target pseudo-classes<br>all UI element
2779 states pseudo-classes<br>all structural pseudo-classes other
2780 than :first-child<br>negation pseudo-class<br>all UI element
2781 fragments pseudo-elements<br>general sibling combinators
2783 <p>namespaces</td></tr>
2785 <th>Extra constraints</th>
2786 <td>more than one class selector per sequence of simple selectors (CSS1
2787 constraint) allowed</td></tr></tbody></table>
2789 <p>In CSS, selectors express pattern matching rules that determine which style
2790 rules apply to elements in the document tree.
2792 <p>The following selector (CSS level 2) will <b>match</b> all anchors <code>a</code>
2793 with attribute <code>name</code> set inside a section 1 header <code>h1</code>:
2794 <pre>h1 a[name]</pre>
2796 <p>All CSS declarations attached to such a selector are applied to elements
2799 <div class="profile
">
2800 <table class="tprofile
">
2803 <th class="title
" colspan=2>Selectors profile</th></tr>
2805 <th>Specification</th>
2812 <p>type selectors<br>universal selectors<br>attribute selectors<br>class
2813 selectors<br>ID selectors<br>all structural pseudo-classes<br>
2816 <p>namespaces</td></tr>
2819 <td>non-accepted pseudo-classes<br>pseudo-elements<br></td></tr>
2821 <th>Extra constraints</th>
2822 <td>some selectors and combinators are not allowed in fragment
2823 descriptions on the right side of STTS declarations.</td></tr></tbody></table>
2825 <input type="text
" name="test1
"/>
2826 <input type="text
" name="foo
"/>
2827 <input type="text
" name="foo
"/>
2828 <input type="text
" name="foo
"/>
2829 <input type="text
" name="foo
"/>
2830 <input type="text
" name="foo
"/>
2831 <input type="text
" name="foo
"/>
2832 <input type="text
" name="foo
"/>
2833 <input type="text
" name="foo
"/>
2834 <input type="text
" name="foo
"/>
2835 <input type="text
" name="foo
"/>
2836 <input type="text
" name="foo
"/>
2837 <input type="text
" name="foo
"/>
2838 <input type="text
" name="foo
"/>
2839 <input type="text
" name="foo
"/>
2840 <input type="text
" name="foo
"/>
2841 <input type="text
" name="foo
"/>
2842 <input type="text
" name="foo
"/>
2843 <input type="text
" name="foo
"/>
2844 <input type="text
" name="foo
"/>
2845 <input type="text
" name="foo
"/>
2846 <input type="text
" name="foo
"/>
2847 <input type="text
" name="foo
"/>
2848 <input type="text
" name="foo
"/>
2849 <input type="text
" name="foo
"/>
2852 <p>Selectors can be used in STTS 3 in two different
2855 <li>a selection mechanism equivalent to CSS selection mechanism: declarations
2856 attached to a given selector are applied to elements matching that selector,
2857 <li>fragment descriptions that appear on the right side of declarations.
2860 <h2><a name=Conformance></a>13. Conformance and requirements</h2>
2862 <p>This section defines conformance with the present specification only.
2864 <p>The inability of a user agent to implement part of this specification due to
2865 the limitations of a particular device (e.g., non interactive user agents will
2866 probably not implement dynamic pseudo-classes because they make no sense without
2867 interactivity) does not imply non-conformance.
2869 <p>All specifications reusing Selectors must contain a <a
2870 href="#profiling
">Profile</a> listing the
2871 subset of Selectors it accepts or excludes, and describing the constraints
2872 it adds to the current specification.
2874 <p>Invalidity is caused by a parsing error, e.g. an unrecognized token or a token
2875 which is not allowed at the current parsing point.
2877 <p>User agents must observe the rules for handling parsing errors:
2879 <li>a simple selector containing an undeclared namespace prefix is invalid</li>
2880 <li>a selector containing an invalid simple selector, an invalid combinator
2881 or an invalid token is invalid. </li>
2882 <li>a group of selectors containing an invalid selector is invalid.</li>
2885 <p class="foo test1 bar
">Specifications reusing Selectors must define how to handle parsing
2886 errors. (In the case of CSS, the entire rule in which the selector is
2887 used is dropped.)</p>
2889 <!-- Apparently all these references are out of date:
2890 <p>Implementations of this specification must behave as
2891 "recipients of text data
" as defined by <a href="#refsCWWW
">[CWWW]</a>
2892 when parsing selectors and attempting matches. (In particular,
2893 implementations must assume the data is normalized and must not
2894 normalize it.) Normative rules for matching strings are defined in
2895 <a href="#refsCWWW
">[CWWW]</a> and <a
2896 href="#refsUNICODE
">[UNICODE]</a> and apply to implementations of this
2897 specification.</p>-->
2899 <h2><a name=Tests></a>14. Tests</h2>
2901 <p>This specification has <a
2902 href="http://www.w3.org/Style/CSS/Test/CSS3/Selectors/current/
">a test
2903 suite</a> allowing user agents to verify their basic conformance to
2904 the specification. This test suite does not pretend to be exhaustive
2905 and does not cover all possible combined cases of Selectors.</p>
2907 <h2><a name=ACKS></a>15. Acknowledgements</h2>
2909 <p>The CSS working group would like to thank everyone who has sent
2910 comments on this specification over the years.</p>
2912 <p>The working group would like to extend special thanks to Donna
2913 McManus, Justin Baker, Joel Sklar, and Molly Ives Brower who perfermed
2914 the final editorial review.</p>
2916 <h2><a name=references>16. References</a></h2>
2921 <dd><a name=refsCSS1></a> Bert Bos, Håkon Wium Lie; "<cite>Cascading Style Sheets, level
1</cite>", W3C Recommendation, 17 Dec 1996, revised 11 Jan 1999
2922 <dd>(<code><a href="http://www.w3.org/TR/REC-CSS1
">http://www.w3.org/TR/REC-CSS1</a></code>)
2925 <dd><a name=refsCSS21></a> Bert Bos, Tantek Çelik, Ian Hickson, Håkon Wium Lie, editors; "<cite>Cascading Style Sheets, level
2 revision
1</cite>", W3C Working Draft, 13 June 2005
2926 <dd>(<code><a href="http://www.w3.org/TR/CSS21
">http://www.w3.org/TR/CSS21</a></code>)
2929 <dd><a name=refsCWWW></a> Martin J. Dürst, François Yergeau, Misha Wolf, Asmus Freytag, Tex Texin, editors; "<cite>Character Model for the World Wide Web
</cite>", W3C Recommendation, 15 February 2005
2930 <dd>(<code><a href="http://www.w3.org/TR/charmod/
">http://www.w3.org/TR/charmod/</a></code>)
2933 <dd><a name="refsFLEX
"></a> "<cite>Flex: The Lexical Scanner Generator
</cite>", Version 2.3.7, ISBN 1882114213
2936 <dd><a name="refsHTML4
"></a> Dave Ragget, Arnaud Le Hors, Ian Jacobs, editors; "<cite>HTML
4.01 Specification
</cite>", W3C Recommendation, 24 December 1999
2937 <dd>(<a href="http://www.w3.org/TR/html4/
"><code>http://www.w3.org/TR/html4/</code></a>)
2940 <dd><a name="refsMATH
"></a> Patrick Ion, Robert Miner, editors; "<cite>Mathematical Markup Language (MathML)
1.01</cite>", W3C Recommendation, revision of 7 July 1999
2941 <dd>(<code><a href="http://www.w3.org/TR/REC-MathML/
">http://www.w3.org/TR/REC-MathML/</a></code>)
2944 <dd><a name="refsRFC3066
"></a> H. Alvestrand; "<cite>Tags for the Identification of Languages
</cite>", Request for Comments 3066, January 2001
2945 <dd>(<a href="http://www.ietf.org/rfc/rfc3066.txt
"><code>http://www.ietf.org/rfc/rfc3066.txt</code></a>)
2948 <dd><a name=refsSTTS></a> Daniel Glazman; "<cite>Simple Tree Transformation Sheets
3</cite>", Electricité de France, submission to the W3C, 11 November 1998
2949 <dd>(<code><a href="http://www.w3.org/TR/NOTE-STTS3
">http://www.w3.org/TR/NOTE-STTS3</a></code>)
2952 <dd><a name="refsSVG
"></a> Jon Ferraiolo, 藤沢 淳, Dean Jackson, editors; "<cite>Scalable Vector Graphics (SVG)
1.1 Specification
</cite>", W3C Recommendation, 14 January 2003
2953 <dd>(<code><a href="http://www.w3.org/TR/SVG/
">http://www.w3.org/TR/SVG/</a></code>)
2956 <dd><a name="refsUNICODE
"></a> <cite><a
2957 href="http://www.unicode.org/versions/Unicode4.1
.0/
">The Unicode Standard, Version 4.1</a></cite>, The Unicode Consortium. Boston, MA, Addison-Wesley, March 2005. ISBN 0-321-18578-1, as amended by <a href="http://www.unicode.org/versions/Unicode4.0
.1/
">Unicode 4.0.1</a> and <a href="http://www.unicode.org/versions/Unicode4.1
.0/
">Unicode 4.1.0</a>.
2958 <dd>(<code><a href="http://www.unicode.org/versions/
">http://www.unicode.org/versions/</a></code>)</dd>
2961 <dd><a name="refsXML10
"></a> Tim Bray, Jean Paoli, C. M. Sperberg-McQueen, Eve Maler, François Yergeau, editors; "<cite>Extensible Markup Language (XML)
1.0 (Third Edition)
</cite>", W3C Recommendation, 4 February 2004
2962 <dd>(<a href="http://www.w3.org/TR/REC-xml/
"><code>http://www.w3.org/TR/REC-xml/</code></a>)
2965 <dd><a name="refsXMLNAMES
"></a> Tim Bray, Dave Hollander, Andrew Layman, editors; "<cite>Namespaces in XML
</cite>", W3C Recommendation, 14 January 1999
2966 <dd>(<a href="http://www.w3.org/TR/REC-xml-names/
"><code>http://www.w3.org/TR/REC-xml-names/</code></a>)
2969 <dd><a name="refsYACC
"></a> S. C. Johnson; "<cite>YACC
— Yet another compiler compiler
</cite>", Technical Report, Murray Hill, 1975