querySelectorAll vs matchesSelector

JavaScript performance comparison

Test case created by Joshua Peek

Preparation code

<div class=head><p><a href="http://www.w3.org/"><img height=48 alt=W3C
src="http://www.w3.org/Icons/w3c_home" width=72></a>
<h1><span class="modulename">Selectors</span></h1>
  <h2>W3C Candidate Recommendation 13 November 2001</h2>
  <dl>
    <dt>This version:
    <dd><a href="http://www.w3.org/TR/2001/CR-css3-selectors-20011113">
                 http://www.w3.org/TR/2001/CR-css3-selectors-20011113</a>
    <dt>Latest version:
    <dd><a href="http://www.w3.org/TR/css3-selectors">
                 http://www.w3.org/TR/css3-selectors</a>
    <dt>Previous version:
    <dd><a href="http://www.w3.org/TR/2001/WD-css3-selectors-20010126">
                 http://www.w3.org/TR/2001/WD-css3-selectors-20010126</a>
    <dt><a name=editors-list></a>Editors:
    <dd><a href="mailto:glazman@netscape.com">Daniel Glazman</a> (<span
     class=company><a
     href="http://www.netscape.com/">Netscape/AOL</a></span>)
    <dd><a href="mailto:tantekc@microsoft.com">Tantek &Ccedil;elik</a> (<span
 class=company><a href="http://www.microsoft.com/">Microsoft Corporation</a></span>)
    <dd><a href="mailto:ian@hixie.ch">Ian Hickson</a>
    <dd>Peter Linss (former editor, formerly of&nbsp;<span class=company><a
 href="http://www.netscape.com/">Netscape/AOL</a></span>)
    <dd>John Williams (former editor, <span class=company><a
 href="http://www.quark.com/">Quark, Inc.</a></span>)
  </dl>
<p class=copyright><a
href="http://www.w3.org/Consortium/Legal/ipr-notice-20000612#Copyright">Copyright</a>
&copy;2001 <a href="http://www.w3.org/"><abbr
title="World Wide Web Consortium">W3C</abbr></a><sup>&reg;</sup> (<a
href="http://www.lcs.mit.edu/"><abbr
title="Massachusetts Institute of Technology">MIT</abbr></a>, <a
href="http://www.inria.fr/"><abbr lang=fr
title="Institut National de Recherche en Informatique et Automatique">INRIA</abbr></a>,
<a href="http://www.keio.ac.jp/">Keio</a>), All Rights Reserved. W3C <a
href="http://www.w3.org/Consortium/Legal/ipr-notice-20000612#Legal_Disclaimer">liability</a>,
<a
href="http://www.w3.org/Consortium/Legal/ipr-notice-20000612#W3C_Trademarks">trademark</a>,
<a
href="http://www.w3.org/Consortium/Legal/copyright-documents-19990405">document
use</a> and <a
href="http://www.w3.org/Consortium/Legal/copyright-software-19980720">software
licensing</a> rules apply.
<hr title="Separator for header">
</div>

<h2><a name=abstract></a>Abstract</h2>
<p><acronym title="Cascading Style Sheets">CSS</acronym> (Cascading Style Sheets) is a language for describing the rendering of
  <acronym title="Hypertext Markup Language">HTML</acronym> and <acronym title="Extensible Markup Language">XML</acronym>
  documents on screen, on paper, in speech, etc. To bind style properties
  to elements in the document, CSS uses <em>selectors,</em> which are patterns
  that match one or more elements. This document describes the selectors that are proposed
  for CSS level 3. It includes and extends the selectors of CSS level 2.
<h2><a name=status></a>Status of this document</h2>
<p>This document is one of the "modules" of the upcoming CSS3 specification. It
  not only describes the selectors that already exist in <a
href="#CSS1"><abbr title="CSS level 1">CSS1</abbr></a> and <a
href="#CSS2"><abbr title="CSS level 2">CSS2</abbr></a>,
  but also proposes new selectors for <abbr title="CSS level 3">CSS3</abbr> as well as for
  other languages that may need them. The CSS Working Group doesn't expect that all
  implementations of CSS3 will have to implement all selectors. Instead,
  there will probably be a small number of variants of CSS3, so-called "profiles".
  For example, it may be that only a profile for non-interactive user agents
  will include all of the proposed selectors.
<p>This specification is being put forth as a <a href="http://www.w3.org/TR/#About">Candidate
  Recommendation</a> by the <a href="http://www.w3.org/Style/Group">CSS Working
  Group</a>. This document is a revision of the <a href="http://www.w3.org/TR/2001/WD-css3-selectors-20010126">Working
  Draft dated 2001 January 26</a>, and has incorporated suggestions received
  during last call review, comments, and further deliberations of the W3C CSS
  Working Group.
<p>The duration of Candidate Recommendation is expected to last approximately
  six months (ending <strong>May, 2002</strong>). All persons are encouraged
  to review and implement this specification and return comments to the (<a
href="http://lists.w3.org/Archives/Public/www-style/">archived</a>) public mailing
  list <a
href="http://www.w3.org/Mail/Lists.html#www-style">www-style</a> (see <a href="http://www.w3.org/Mail/Request">instructions</a>).
  W3C Members can also send comments directly to the CSS Working Group.
<p>Should this specification prove impossible to implement, the Working Group
  will return the document to Working Draft status and make necessary changes.
  Otherwise, the Working Group anticipates asking the W3C Director to advance
  this document to Proposed Recommendation.
<p>This is still a draft document and may be updated, replaced, or obsoleted by
  other documents at any time. It is inappropriate to cite a W3C Candidate Recommendation
  as other than &quot;work in progress.&quot; A list of current W3C working drafts
  can be found at <a href="http://www.w3.org/TR">http://www.w3.org/TR</a>.<br>
  <br>
  This document may be available in <a
href="http://www.w3.org/Style/css3-selectors-updates/translations">translation</a>.
  The English version of this specification is the only normative version.
<h2><a name=dependencies></a>Dependencies with other CSS3 Modules</h2>
<ul>
  <li>General Syntax
  <li>Value Assignment, Cascade and Inheritance
  <li>Generated Content / Markers
  <li>User Interface
</ul>
<div class=subtoc>
<h2><a name=contents>Table of contents</a></h2>
<ul class=toc>
  <li class=tocline2><a href="#context">1.
  Context</a>
  <ul>
    <li><a href="#changesFromCSS2">1.1
    Changes from CSS2</a> </li></ul>
  <li class=tocline2><a href="#selectors">2.
  Selectors</a>
  <li class=tocline2><a href="#casesens">3.
  Case sensitivity</a>
  <li class=tocline2><a
 href="#selector-syntax">4. Selector
  syntax</a>
  <li class=tocline2><a href="#grouping">5.
  Groups of selectors</a>
  <li class=tocline2><a
 href="#simple-selectors">6. Simple
  selectors</a>
  <ul class=toc>
    <li class=tocline3><a
   href="#type-selectors">6.1 Type
    selectors</a>
    <ul class=toc>
      <li class=tocline4><a
     href="#typenmsp">6.1.1 Type selectors
      and Namespaces</a> </li></ul>
    <li class=tocline3><a
   href="#universal-selector">6.2 Universal
    selector</a>
    <ul>
      <li><a href="#univnmsp">6.2.1
      Universal selector and Namespaces</a> </li></ul>
    <li class=tocline3><a
   href="#attribute-selectors">6.3
    Attribute selectors</a>
    <ul class=toc>
      <li class=tocline4><a
     href="#attribute-representation">6.3.1
      Representation of attributes and attributes values</a>
      <li><a
     href="#attribute-substrings">6.3.2
      Substring matching attribute selectors</a>
      <li class=tocline4><a
     href="#attrnmsp">6.3.3 Attribute
      selectors and Namespaces</a>
      <li class=tocline4><a
     href="#def-values">6.3.4 Default
      attribute values in DTDs</a> </li></ul>
    <li class=tocline3><a
   href="#class-html">6.4 Class
    selectors</a>
    <li class=tocline3><a
   href="#id-selectors">6.5 ID
    selectors</a>
    <li class=tocline3><a
   href="#pseudo-classes">6.6
    Pseudo-classes</a>
    <ul class=toc>
      <li class=tocline4><a
     href="#dynamic-pseudos">6.6.1 Dynamic
      pseudo-classes</a>
      <li class=tocline4><a
     href="#target-pseudo">6.6.2 The
      :target pseudo-class</a>
      <li class=tocline4><a
     href="#lang-pseudo">6.6.3 The :lang()
      pseudo-class</a>
      <li class=tocline4><a
     href="#UIstates">6.6.4 UI element
      states pseudo-classes</a>
      <li class=tocline4><a
     href="#structural-pseudos">6.6.5
      Structural pseudo-classes</a>
      <ul>
        <li><a href="#root-pseudo">:root
        pseudo-class</a>
        <li><a
       href="#nth-child-pseudo">:nth-child()
        pseudo-class</a>
        <li><a
       href="#nth-last-child-pseudo">:nth-last-child()</a>

        <li><a
       href="#nth-of-type-pseudo">:nth-of-type()
        pseudo-class</a>
        <li><a
       href="#nth-last-of-type-pseudo">:nth-last-of-type()</a>

        <li><a
       href="#first-child-pseudo">:first-child
        pseudo-class</a>
        <li><a
       href="#last-child-pseudo">:last-child
        pseudo-class</a>
        <li><a
       href="#first-of-type-pseudo">:first-of-type
        pseudo-class</a>
        <li><a
       href="#last-of-type-pseudo">:last-of-type
        pseudo-class</a>
        <li><a
       href="#only-child-pseudo">:only-child
        pseudo-class</a>
        <li><a
       href="#only-of-type-pseudo">:only-of-type
        pseudo-class</a>
        <li><a href="#empty-pseudo">:empty
        pseudo-class</a> </li></ul>
      <li class=tocline4><a
     href="#content-selectors">6.6.6
      Content pseudo-class</a>
      <li><a href="#negation">6.6.7 The
      negation pseudo-class</a> </li></ul></li></ul>
  <li><a href="#pseudo-elements">7.
  Pseudo-elements</a>
  <ul>
    <li><a href="#first-line">7.1 The
    :first-line pseudo-element</a>
    <li><a href="#first-letter">7.2 The
    :first-letter pseudo-element</a>
    <li><a href="#UIfragments">7.3 UI
    element fragments pseudo-elements</a>
    <li><a href="#gen-content">7.4 The
    :before and :after pseudo-elements</a> </li></ul>
  <li class=tocline2><a
 href="#combinators">8. Combinators</a>
  <ul class=toc>
    <li class=tocline3><a
   href="#descendant-combinators">8.1
    Descendant combinators</a>
    <li class=tocline3><a
   href="#child-combinators">8.2 Child
    combinators</a>
    <li class=tocline3><a
   href="#adjacent-combinators">8.3
    Adjacent sibling combinators</a>
    <ul class=toc>
      <li class=tocline4><a
     href="#adjacent-d-combinators">8.3.1
      Adjacent direct combinators</a>
      <li class=tocline4><a
     href="#adjacent-i-combinators">8.3.2
      Adjacent indirect combinators</a> </li></ul></li></ul>
  <li class=tocline2><a
 href="#specificity">9. Calculating a
  selector's specificity</a>
  <li class=tocline2><a
 href="#w3cselgrammar">10. The grammar of
  <span class="modulename">Selectors</span></a>
  <ul class=toc>
    <li class=tocline3><a
   href="#grammar">10.1 Grammar</a>
    <li class=tocline3><a href="#lex">10.2
    Lexical scanner</a> </li></ul>
  <li class=tocline2><a
 href="#downlevel">11. Namespaces and
  Down-Level clients</a>
  <li class=tocline2><a
 href="#profiling">12. Profiles</a>
  <li><a href="#Conformance">13. Conformance
  and Requirements</a>
  <li><a href="#Tests">14. Tests</a>
  <li><a href="#ACKS">15.
  Acknowledgements</a>
  <li class=tocline2><a
 href="#references">16. References</a> <!--<li class="tocline2"><a href="#changes">Changes from previous version</a>--></li></ul></div>
<h2><a name=context>1. Context</a></h2>
<p>Members of the CSS+FP Working Group proposed during the Clamart meeting to
modularize the CSS specification.
<p>This modularization, and the externalization of the general syntax of CSS
will reduce the size of the specification and allow new specifications
to use selectors and/or CSS general syntax. For instance, behaviors or tree
transformations.
<p>This specification contains its own <a
href="#Tests">test cases</a>, one test per concept introduced in this document.
  These tests are not full conformance tests but are intended to provide users
  with a way to check if a part of this specification is implemented <i>ad minima</i>
  or is not implemented at all.
<h3><a name=changesFromCSS2></a>1.1 Changes from CSS2</h3>
<p>The main differences between the selectors in CSS2 and those in
 <span class="modulename">Selectors</span> are:
<ul>
  <li>the list of basic definitions (selector, group of selectors, simple
  selector, etc.) has been clarified
  <li>an optional namespace component is now allowed in type element selectors,
  the universal selector and attribute selectors
  <li>a new combinator
  <li>new simple selectors including substring matching attribute selectors, and new
  pseudo-classes
  <li>new pseudo-elements, and introduction of the "::" convention for pseudo-elements
  <li>a rewriting of the selectors grammar
  <li>profiles to be added to specifications integrating <span class="modulename">Selectors</span> and
  defining the set of selectors which is actually supported by each
  specification
  <li><span class="modulename">Selectors</span> are now a CSS3 Module and an independent specification.
  Other  specifications can now refer to this document independently of CSS
  <li>the specification now contains its own test suite. </li>
</ul>
<h2><a name=selectors></a>2. Selectors</h2>
<p>A <span class="propernoun">Selector</span> represents a structure. This structure can be used
as a condition (e.g. in a CSS rule) that determines which elements
a selector matches in the document tree, or as a flat description of the
HTML or XML fragment corresponding to that structure.
<p><span class="propernoun">Selectors</span> may range from simple element names to rich contextual
representations.
<p>The following table summarizes <span class="propernoun">Selector</span> syntax:
<table class=selectorsreview width="100%" border=1>
  <tbody>
  <tr>
    <th class=pattern>Pattern</th>
    <th class=meaning>Meaning</th>
    <th class=described>Described in section</th>
    <th class=origin>First defined in CSS level</th></tr>
  <tr>
    <td class=pattern>*</td>
    <td class=meaning>any element</td>
    <td class=described><a
     href="#universal-selector">Universal
      selector</a></td>
    <td class=origin>2</td></tr>
  <tr>
    <td class=pattern>E</td>
    <td class=meaning>an element of type E</td>
    <td class=described><a
     href="#type-selectors">Type selector</a></td>
    <td class=origin>1</td></tr>
  <tr>
    <td class=pattern>E[foo]</td>
    <td class=meaning>an E element with a "foo" attribute</td>
    <td class=described><a
     href="#attribute-selectors">Attribute
      selectors</a></td>
    <td class=origin>2</td></tr>
  <tr>
    <td class=pattern>E[foo="bar"]</td>
    <td class=meaning>an E element whose "foo" attribute value is exactly
      equal to "bar"</td>
    <td class=described><a
     href="#attribute-selectors">Attribute
      selectors</a></td>
    <td class=origin>2</td></tr>
  <tr>
    <td class=pattern>E[foo~="bar"]</td>
    <td class=meaning>an E element whose "foo" attribute value is a list of
      space-separated values, one of which is exactly equal to "bar"</td>
    <td class=described><a
     href="#attribute-selectors">Attribute
      selectors</a></td>
    <td class=origin>2</td></tr>
  <tr>
    <td class=pattern>E[foo^="bar"]</td>
    <td class=meaning>an E element whose "foo" attribute value begins exactly
      with the string "bar"</td>
    <td class=described><a
     href="#attribute-selectors">Attribute
      selectors</a></td>
    <td class=origin>3</td></tr>
  <tr>
    <td class=pattern>E[foo$="bar"]</td>
    <td class=meaning>an E element whose "foo" attribute value ends exactly
      with the string "bar"</td>
    <td class=described><a
     href="#attribute-selectors">Attribute
      selectors</a></td>
    <td class=origin>3</td></tr>
  <tr>
    <td class=pattern>E[foo*="bar"]</td>
    <td class=meaning>an E element whose "foo" attribute value contains the
      substring "bar"</td>
    <td class=described><a
     href="#attribute-selectors">Attribute
      selectors</a></td>
    <td class=origin>3</td></tr>
  <tr>
    <td class=pattern>E[hreflang|="en"]</td>
    <td class=meaning>an E element whose "hreflang" attribute has a hyphen-separated
      list of values beginning (from the left) with "en"</td>
    <td class=described><a
     href="#attribute-selectors">Attribute
      selectors</a></td>
    <td class=origin>2</td></tr>
  <tr>
    <td class=pattern>E:root</td>
    <td class=meaning>an E element, root of the document</td>
    <td class=described><a
     href="#structural-pseudos">Structural
      pseudo-classes</a></td>
    <td class=origin>3</td></tr>
  <tr>
    <td class=pattern>E:nth-child(n)</td>
    <td class=meaning>an E element, the n-th child of its parent</td>
    <td class=described><a
     href="#structural-pseudos">Structural
      pseudo-classes</a></td>
    <td class=origin>3</td></tr>
  <tr>
    <td class=pattern>E:nth-last-child(n)</td>
    <td class=meaning>an E element, the n-th child of its parent, counting
      from the last one</td>
    <td class=described><a
     href="#structural-pseudos">Structural
      pseudo-classes</a></td>
    <td class=origin>3</td></tr>
  <tr>
    <td class=pattern>E:nth-of-type(n)</td>
    <td class=meaning>an E element, the n-th sibling of its type</td>
    <td class=described><a
     href="#structural-pseudos">Structural
      pseudo-classes</a></td>
    <td class=origin>3</td></tr>
  <tr>
    <td class=pattern>E:nth-last-of-type(n)</td>
    <td class=meaning>an E element, the n-th sibling of its type, counting
      from the last one</td>
    <td class=described><a
     href="#structural-pseudos">Structural
      pseudo-classes</a></td>
    <td class=origin>3</td></tr>
  <tr>
    <td class=pattern>E:first-child</td>
    <td class=meaning>an E element, first child of its parent</td>
    <td class=described><a
     href="#structural-pseudos">Structural
      pseudo-classes</a></td>
    <td class=origin>2</td></tr>
  <tr>
    <td class=pattern>E:last-child</td>
    <td class=meaning>an E element, last child of its parent</td>
    <td class=described><a
     href="#structural-pseudos">Structural
      pseudo-classes</a></td>
    <td class=origin>3</td></tr>
  <tr>
    <td class=pattern>E:first-of-type</td>
    <td class=meaning>an E element, first sibling of its type</td>
    <td class=described><a
     href="#structural-pseudos">Structural
      pseudo-classes</a></td>
    <td class=origin>3</td></tr>
  <tr>
    <td class=pattern>E:last-of-type</td>
    <td class=meaning>an E element, last sibling of its type</td>
    <td class=described><a
     href="#structural-pseudos">Structural
      pseudo-classes</a></td>
    <td class=origin>3</td></tr>
  <tr>
    <td class=pattern>E:only-child</td>
    <td class=meaning>an E element, only child of its parent</td>
    <td class=described><a
     href="#structural-pseudos">Structural
      pseudo-classes</a></td>
    <td class=origin>3</td></tr>
  <tr>
    <td class=pattern>E:only-of-type</td>
    <td class=meaning>an E element, only sibling of its type</td>
    <td class=described><a
     href="#structural-pseudos">Structural
      pseudo-classes</a></td>
    <td class=origin>3</td></tr>
  <tr>
    <td class=pattern>E:empty</td>
    <td class=meaning>an E element that has no children (including text
    nodes)</td>
    <td class=described><a
     href="#structural-pseudos">Structural
      pseudo-classes</a></td>
    <td class=origin>3</td></tr>
  <tr>
    <td class=pattern>E:link <br>E:visited</td>
    <td class=meaning>an E element being the source anchor of a hyperlink of
      which the target is not yet visited (:link) or already visited
    (:visited)</td>
    <td class=described><a
     href="#link">The link
      pseudo-classes</a></td>
    <td class=origin>1</td></tr>
  <tr>
    <td class=pattern>E:active <br>E:hover <br>E:focus</td>
    <td class=meaning>an E element during certain user actions</td>
    <td class=described><a
     href="#useraction-pseudos">The user
      action pseudo-classes</a></td>
    <td class=origin>1 and 2</td></tr>
  <tr>
    <td class=pattern>E:target</td>
    <td class=meaning>an E element being the target of the referring URI</td>
    <td class=described><a
     href="#target-pseudo">The target
      pseudo-class</a></td>
    <td class=origin>3</td></tr>
  <tr>
    <td class=pattern>E:lang(fr)</td>
    <td class=meaning>an element of type E in language "fr" (the document
      language specifies how language is determined)</td>
    <td class=described><a
     href="#lang-pseudo">The :lang()
      pseudo-class&nbsp;</a></td>
    <td class=origin>2</td></tr>
  <tr>
    <td class=pattern>E:enabled<br>E:disabled&nbsp;</td>
    <td class=meaning>a user interface element E which is enabled or
    disabled</td>
    <td class=described><a
     href="#UIstates">The UI element states
      pseudo-classes</a></td>
    <td class=origin>3</td></tr>
  <tr>
    <td class=pattern>E:checked<br>E:indeterminate&nbsp;</td>
    <td class=meaning>a user interface element E which is checked or in an
      indeterminate state (for instance a radio-button or checkbox)</td>
    <td class=described><a
     href="#UIstates">The UI element states
      pseudo-classes</a></td>
    <td class=origin>3</td></tr>
  <tr>
    <td class=pattern>E:contains("foo")</td>
    <td class=meaning>an E element containing the substring "foo" in its textual
      contents</td>
    <td class=described><a
     href="#content-selectors">Content
      pseudo-class</a></td>
    <td class=origin>3</td></tr>
  <tr>
    <td class=pattern>E::first-line</td>
    <td class=meaning>the first formatted line of an E element</td>
    <td class=described><a
     href="#first-line">The :first-line
      pseudo-element</a></td>
    <td class=origin>1</td></tr>
  <tr>
    <td class=pattern>E::first-letter</td>
    <td class=meaning>the first formatted letter of an E element</td>
    <td class=described><a
     href="#first-letter">The :first-letter
      pseudo-element</a></td>
    <td class=origin>1</td></tr>
  <tr>
    <td class=pattern>E::selection</td>
    <td class=meaning>the portion of an E element that is currently
      selected/highlighted by the user</td>
    <td class=described><a
     href="#UIfragments">The UI element
      fragments pseudo-elements</a></td>
    <td class=origin>3</td></tr>
  <tr>
    <td class=pattern>E::before</td>
    <td class=meaning>generated content before an E element</td>
    <td class=described><a
     href="#gen-content">The :before
      pseudo-element</a></td>
    <td class=origin>2</td></tr>
  <tr>
    <td class=pattern>E::after</td>
    <td class=meaning>generated content after an E element</td>
    <td class=described><a
     href="#gen-content">The :after
      pseudo-element</a></td>
    <td class=origin>2</td></tr>
  <tr>
    <td class=pattern>E.warning</td>
    <td class=meaning>an E element whose class is
"warning" (the document language specifies how class is determined).</td>
    <td class=described><a
     href="#class-html">Class
    selectors</a></td>
    <td class=origin>1</td></tr>
  <tr>
    <td class=pattern>E#myid</td>
    <td class=meaning>an E element with ID equal to "myid".</td>
    <td class=described><a
     href="#id-selectors">ID
    selectors</a></td>
    <td class=origin>1</td></tr>
  <tr>
    <td class=pattern>E:not(s)</td>
    <td class=meaning>an E element that does not match simple selector s</td>
    <td class=described><a
     href="#negation">Negation
      pseudo-class</a></td>
    <td class=origin>3</td></tr>
  <tr>
    <td class=pattern>E F</td>
    <td class=meaning>an F element descendant of an E element</td>
    <td class=described><a
     href="#descendant-combinators">Descendant
      combinator</a></td>
    <td class=origin>1</td></tr>
  <tr>
    <td class=pattern>E &gt; F</td>
    <td class=meaning>an F element child of an E element</td>
    <td class=described><a
     href="#child-combinators">Child
      combinator</a></td>
    <td class=origin>2</td></tr>
  <tr>
    <td class=pattern>E + F</td>
    <td class=meaning>an F element immediately preceded by an E element</td>
    <td class=described><a
     href="#adjacent-d-combinators">Direct
      adjacent combinator</a></td>
    <td class=origin>2</td></tr>
  <tr>
    <td class=pattern>E ~ F</td>
    <td class=meaning>an F element preceded by an E element</td>
    <td class=described><a
     href="#adjacent-i-combinators">Indirect
      adjacent combinator</a></td>
    <td class=origin>3</td></tr></tbody></table>
<p>The meaning of each selector is derived from the table above by
 prepending "matches" to the contents of each cell of the "Meaning" column.
<h2><a name=casesens>3. Case sensitivity</a></h2>
<p>The case-sensitivity of document language element names in selectors depends
on the document language. For example, in HTML, element names are
case-insensitive, but in XML they are case-sensitive.
<p>The case-sensitivity of attribute names and attribute values in attribute
selectors also depends on the document language.
<h2><a name=selector-syntax>4. Selector syntax</a></h2>
<p>A&nbsp;<dfn><a name=selector>selector</a></dfn> is a chain of one or more <a
href="#sequence">sequences of simple
selectors</a> separated by <a
href="#combinators">combinators</a>.
<p>A&nbsp;<dfn><a name=sequence>sequence of simple selectors</a></dfn> is a chain
  of <a
href="#simple-selectors-dfn">simple selectors</a> that are not separated by a
  <a
href="#combinators">combinator</a>. It always begins with a <a
href="#type-selectors">type selector</a> or a <a href="#universal-selector">universal
  selector</a>. No other type selector or universal selector is allowed in the
  sequence.
<p>A&nbsp;<dfn><a name=simple-selectors-dfn></a><a
href="#simple-selectors">simple selector</a></dfn> is either a <a
href="#type-selectors">type selector</a>, <a
href="#universal-selector">universal selector</a>, <a
href="#attribute-selectors">attribute selector</a>, <a href="#id-selectors">ID
  selector</a>, <a
href="#content-selectors">content selector</a>, or <a
href="#pseudo-classes">pseudo-class</a>. One <a
href="#pseudo-elements">pseudo-element</a> may be appended to the last sequence
  of simple selectors.
<p><dfn>Combinators</dfn> are: white space, &quot;greater-than sign&quot; (<code>&gt;</code>),
  &quot;plus sign&quot; (<code>+</code>) and &quot;tilde&quot; (<code>~</code>).
  White space may appear between a combinator and the simple selectors around
  it. <a name=whitespace></a>Only the characters "space" (Unicode code 32), "tab"
  (9), "line feed" (10), "carriage return" (13), and "form feed" (12) can occur
  in white space. Other space-like characters, such as "em-space" (8195) and "ideographic
  space" (12288), are never part of white space.
<p>The elements of the document tree represented by a selector are called <dfn><a name=subject></a>subjects
  of the selector</dfn>. A selector consisting of a single sequence of simple
  selectors represents any element satisfying its requirements. Prepending another
  sequence of simple selectors and a combinator to a sequence imposes additional
  matching constraints, so the subjects of a selector are always a subset of the
  elements represented by the rightmost sequence of simple selectors.
<p><strong><em>Note</em></strong><em>: an empty selector, containing no sequence
  of simple selectors and no combinator, is an <a href="#Conformance">invalid
  selector</a>.</em>
<h2><a name=grouping>5. Groups of selectors</a></h2>
<p>When several selectors share the same declarations, they may be grouped into
a comma-separated list.
<div class=example>CSS example(s):
<p>In this example, we condense three rules with identical declarations into
one. Thus, <pre>h1 { font-family: sans-serif }
h2 { font-family: sans-serif }
h3 { font-family: sans-serif }</pre>is equivalent to: <pre>h1, h2, h3 { font-family: sans-serif }</pre></div>
<p><b>Warning</b>: the equivalence is true in this example because all selectors
  are valid selectors. If just one of these selectors is invalid, the entire group
  of selectors is invalid thus invalidating the rule for all three heading elements,
  whereas only one of the three individual heading rules would be invalid.
 
<h2><a name=simple-selectors>6. Simple selectors</a></h2>
<h3><a name=type-selectors>6.1 Type selector</a></h3>
<p>A&nbsp;<dfn>type selector</dfn> is the name of a document language element
type. A type selector represents an instance of the element type in the document
tree.
<div class=example>Example:
  <p>The following selector represents an <code>h1</code> element in the document
tree: <pre>h1</pre></div>
<h4><a name=typenmsp>6.1.1 Type selectors and Namespaces</a></h4>
<p>Type selectors allow an optional namespace (<a href="#XMLNAMES">[XML-NAMES]</a>) component.
  A namespace prefix that has been previously declared
  may be prepended to the element name separated by the namespace separator
  &quot;vertical bar&quot; (<code>|</code>). The namespace component may be left
  empty to indicate that the selector is only to represent elements with no declared
  namespace. Furthermore, an asterisk may be used for the namespace prefix, indicating
  that the selector represents elements in any namespace (including elements
  with no namespace). Element type selectors that have no namespace component
  (no namespace separator), represent elements without regard
  to the element's namespace (equivalent to "<code>*|</code>") unless a default
  namespace has been declared. In that case, the selector will represent only
  elements in the default namespace.
<p>Note : a type selector containing a namespace prefix that has not been previously
 declared is an <a href="#Conformance">invalid</a> selector.
 The mechanism for declaring a namespace prefix is left up to the language
 implementing <span class="modulename">Selectors</span>.
 In CSS, such a mechanism is defined in the General Syntax module.

<!--<p>An alternative approach would be to define element type selectors that have
 no namespace component to match only elements that have no namespace (unless
 a default namespace has been declared in the CSS). This would make the selector
 "<code>h1</code>" equivalent to the selector "<code>|h1</code>" as opposed to
 "<code>*|h1</code>". The downside to this approach is that legacy style sheets
 (those written without any namespace constructs) will fail to match in all XML
 documents where namespaces are used throughout, e.g. all XHTML documents. -->
<p>It should be noted that if a namespace prefix used in a selector has not been
  previously declared, then the selector must be considered invalid and the entire
  style rule will be ignored in accordance with the <a href="#Conformance">standard
  error handling rules</a>.
<p>It should further be noted that in a namespace aware client, element type
selectors will only match against the <a
href="http://www.w3.org/TR/REC-xml-names/#NT-LocalPart">local part</a> of the
element's <a href="http://www.w3.org/TR/REC-xml-names/#ns-qualnames">qualified
name</a>. See <a href="#downlevel">below</a>
for notes about matching behaviors in down-level clients.
<p>In summary:
<dl>
  <dt><code>ns|E</code>
  <dd>elements with name E in namespace ns
  <dt><code>*|E</code>
  <dd>elements with name E in any namespace, including those without any
  declared namespace
  <dt><code>|E</code>
  <dd>elements with name E without any declared namespace
  <dt><code>E</code>
  <dd>if no default namespace has been specified, this is equivalent to *|E.
  Otherwise it is equivalent to ns|E where ns is the default namespace. </dl>
<div class=example>
<p>CSS examples:
  <pre>@namespace foo url(http://www.example.com);

foo|h1 { color: blue }

foo|* { color: yellow }

|h1 { color: red }

*|h1 { color: green }

h1 { color: green }</pre>
  <p>The first rule will match only <code>h1</code> elements in the "http://www.example.com"
    namespace.
  <p>The second rule will match all elements in the "http://www.example.com" namespace.
 
<p>The third rule will match only <code>h1</code> elements without any declared
namespace.
<p>The fourth rule will match <code>h1</code> elements in any namespace (including
those without any declared namespace).
<p>The last rule is equivalent to the fourth rule because no default namespace
has been defined.</div>
<h3><a name=universal-selector>6.2 Universal selector</a> </h3>
<p>The&nbsp;<dfn>universal selector</dfn>, written &quot;asterisk&quot; (<code>*</code>),
  represents the qualified name of any element type. It represents then any single
  element in the document tree in any namespace (including those without any declared
  namespace) if no default namespace has been specified. If a default namespace
  has been specified, see <a
href="#univnmsp">Universal selector and Namespaces</a> below.
<p>If the universal selector is not the only component of a sequence of simple
selectors, the <code>*</code> may be omitted. For example:
<div class=example>
<ul>
  <li><code>*[hreflang|=en]</code> and <code>[hreflang|=en]</code> are equivalent,
  <li><code>*.warning</code> and <code>.warning</code> are equivalent,
  <li><code>*#myid</code> and <code>#myid</code> are equivalent. </li></ul></div>
<p><b>Note</b>: it is recommended that the <code>*</code>, representing the
universal selector, not be omitted.
<h4><a name=univnmsp>6.2.1 Universal selector and Namespaces</a></h4>
<p>The universal selector allows an optional namespace component.
<dl>
  <dt><code>ns|*</code>
  <dd>all elements in namespace ns
  <dt><code>*|*</code>
  <dd>all elements
  <dt><code>|*</code>
  <dd>all elements without any declared namespace
  <dt><code>*</code>
  <dd>if no default namespace has been specified, this is equivalent to *|*.
  Otherwise it is equivalent to ns|* where ns is the default namespace. </dl>
<p>Note: a universal selector containing a namespace prefix that has not been
 previously declared is an <a href="#Conformance">invalid</a> selector.
 The mechanism for declaring a namespace prefix is left up to the language
 implementing <span class="modulename">Selectors</span>.
 In CSS, such a mechanism is defined in the General Syntax module.

<h3><a name=attribute-selectors>6.3 Attribute selectors</a></h3>
<p><span class="propernoun">Selectors</span> allow the representation of an element's attributes.

<h4><a name=attribute-representation>6.3.1 Attribute presence and values
selectors</a></h4>
<p>CSS2 introduced four attribute selectors:
<dl>
  <dt><code>[att]</code>
  <dd>Represents the <code>att</code> attribute, whatever the value of the
  attribute.
  <dt><code>[att=val]</code>
  <dd>Represents the <code>att</code> attribute with value exactly "val".
  <dt><code>[att~=val]</code>
  <dd>Represents the <code>att</code> attribute whose value is a space-separated list of words,
  one of which is exactly "val". If this selector is used, the
  words in the value must not contain spaces (since they are separated by
  spaces).
  <dt><code>[att|=val]</code>
  <dd>Represents the <code>att</code> attribute, its value either being exactly "val" or
  beginning with "val" immediately followed by "-".
  This is primarily intended to allow language subcode matches
  (e.g., the <code>hreflang</code> attribute on the <code>link</code> element in HTML)
  as described in RFC 3066 (<a class=noxref href="#rfc3066" rel=biblioentry>[RFC3066]</a>).
   Note: for <code>lang</code> (or <code>xml:lang</code>) language subcode matching,
   please see <a href="#lang-pseudo">the <code>:lang</code> pseudo-class</a>.
 
</dl>
<p>Attribute values must be identifiers or strings. The case-sensitivity of
attribute names and values in selectors depends on the document language.
<div class=example>Examples:
  <p>For example, the following attribute selector represents an <code>h1</code>
element that carries the <code>title</code> attribute, whatever its value: <pre>h1[title]</pre>
  <p>In the following example, the selector represents a <code>span</code> element
whose <code>class</code> attribute has exactly the value "example": <pre>span[class=example]</pre>
  Multiple attribute selectors can be used to represent several attributes of
  an element, or several conditions on the same attribute.
  <p>Here, the selector represents a <code>span</code> element whose <code>hello</code>
attribute has exactly the value "Cleveland" and whose <code>goodbye</code> attribute
has exactly the value "Columbus": <pre>span[hello="Cleveland"][goodbye="Columbus"]</pre>
  <p>The following selectors illustrate the differences between "=" and "~=".
    The first selector will represent, for example, the value "copyright copyleft
    copyeditor" on a <code>rel</code> attribute. The second selector will only
    represent an <code>a</code> element with an <code>href</code> attribute having
    the exact value "http://www.w3.org/".
  <pre>a[rel~="copyright"]
a[href="http://www.w3.org/"]</pre>
  <p>The following selector represents a <code>link</code> element whose
  <code>hreflang</code> attribute is exactly "fr".
  <pre>link[hreflang=fr]</pre>
  <p>The following selector represents a <code>link</code> element for which the
    values of the <code>hreflang</code> attribute begins with "en", including
    "en", "en-US", and "en-cockney":
  <pre>link[hreflang|="en"]</pre>
  <p>Similarly, the following selectors represents a <code>DIALOGUE</code> element
whenever it has one of two different values for an attribute <code>character</code>:
<pre>DIALOGUE[character=romeo]&nbsp;

DIALOGUE[character=juliet]</pre></div>
<h4><a name=attribute-substrings></a>6.3.2 Substring matching attribute
selectors</h4>
<p>Three additional attribute selectors are provided
 for matching substrings in the value of an attribute:
<dl>
  <dt><code>[att^=val]</code>
  <dd>Represents the <code>att</code> attribute whose value begins with
  the prefix "val"
  <dt><code>[att$=val]</code>
  <dd>Represents the <code>att</code> attribute whose value ends with the
  suffix "val"
  <dt><code>[att*=val]</code>
  <dd>Represents the <code>att</code> attribute whose value contains at least
  one instance of the substring "val" </dl>
<p>Attribute values must be identifiers or strings. The case-sensitivity of
attribute names in selectors depends on the document language.
<p>Example:
<p>The following selector represents an HTML <code>object</code>, referencing an
image:<pre>object[type^="image/"]
</pre>
<p>The following selector represents an HTML anchor <code>a</code> with an
  <code>href</code> attribute whose value ends with ".html".
<pre>a[href$=".html"]</pre>
<p>The following selector represents a HTML paragraph with a <code>title</code>
attribute whose value contains the substring "hello"<pre>p[title*="hello"] </pre>
<h4><a name=attrnmsp>6.3.3 Attribute selectors and Namespaces</a></h4>
<p>Attribute selectors allow an optional namespace component to the attribute
  name. A namespace prefix that has been previously declared may be prepended
  to the attribute name separated by the namespace separator
  &quot;vertical bar&quot; (<code>|</code>). In keeping with the Namespaces in
  the XML recommendation, default namespaces do not apply to attributes, therefore
  attribute selectors without a namespace component apply only to attributes that
  have no declared namespace (equivalent to "<code>|attr</code>"). An asterisk
  may be used for the namespace prefix indicating that the selector is to match
  all attribute names without regard to the attribute's namespace.
<p>Note : an attribute
 selector with an attribute name containing a namespace prefix that has
 not been previously declared is an <a href="#Conformance">invalid</a> selector.
 The mechanism for declaring a namespace prefix is left up to the language
 implementing <span class="modulename">Selectors</span>.
 In CSS, such a mechanism is defined in the General Syntax module.

<p>CSS examples:
<div class=example>
  <pre>@namespace foo "http://www.example.com";

[foo|att=val] { color: blue }

[*|att] { color: yellow }

[|att] { color: green }

[att] { color: green }</pre>
  The first rule will match only elements with the attribute <code>att</code>
  in the "http://www.example.com" namespace with the value "val".
  <p>The second rule will match only elements with the attribute <code>att</code>
regardless of the namespace of the attribute (including no declared namespace).
<p>The last two rules are equivalent and will match only elements with the
attribute <code>att</code> where the attribute is not declared to be in a
namespace.</div>
<h4><a name=def-values>6.3.4 Default attribute values in DTDs</a></h4>
<p>Attribute selectors represent explicitly set attribute values in the document
  tree. Default attribute values may be defined in a DTD or elsewhere.
  <span class="propernoun">Selectors</span> should be designed so that they work
  even if the default values are not included in the document tree.
<div class=example>Examples:
  <p>For example, consider an element <code>EXAMPLE</code> with an attribute
<code>notation</code> that has a default value of "decimal". The DTD fragment
might be <pre>&lt;!ATTLIST EXAMPLE notation (decimal,octal) "decimal"&gt;</pre>
  If the selectors represent an <code>EXAMPLE</code> element when the value of
  the attribute is explicitly set:
  <pre>EXAMPLE[notation=decimal]
EXAMPLE[notation=octal]</pre>
  then to represent only the case where this attribute is set by default, and
  not explicitly, the following selector might be used:
  <pre>EXAMPLE:not([notation])</pre>
</div>
<h3><a name=class-html>6.4 Class selectors</a></h3>
<p>Working with HTML, authors may use the period (<code>.</code>) notation as
  an alternative to the <code>~=</code> notation when representing the <code>class</code>
  attribute. Thus, for HTML, <code>div.value</code> and <code>div[class~=value]</code>
  have the same meaning. The attribute value must immediately follow the &quot;period&quot;
  (<code>.</code>). Note: UAs may apply selectors using the period (.) notation
  in XML documents if the UA has namespace specific knowledge that allows it to
  determine which attribute is the &quot;class&quot; attribute for the respective
  namespace. One such example of namespace specific knowledge is the prose in
  the specification for a particular namespace (e.g. SVG 1.0 [<a href="#SVG">SVG</a>]
  describes the <a href="http://www.w3.org/TR/2001/PR-SVG-20010719/styling.html#ClassAttribute">SVG
  &quot;class&quot; attribute</a> and how a UA should interpret it, and similarly
  MathML 1.01 [<a href="#MATH">MATH</a>] describes the <a href="http://www.w3.org/1999/07/REC-MathML-19990707/chapter2.html#sec2.3.4">MathML
  &quot;class&quot; attribute</a>.)
<p>
<div class=example>Examples:
  <p>For example, we can represent an arbitrary element with
<code>class~="pastoral"</code> as follows: <pre>*.pastoral</pre>or just <pre>.pastoral</pre>
  The following selector represents an <code>h1</code> element with <code>class~="pastoral"</code>:
  <pre>h1.pastoral</pre>
  <p>For example, the following selector represents a <code>p</code> element whose
<code>class</code> attribute has been assigned a list of space-separated values that
includes "pastoral" and "marine": <pre>p.pastoral.marine</pre>
<p>It is fully identical to:<pre>p.marine.pastoral</pre>
  <p>This selector represents for example a <code>p</code> with <code>class="pastoral
    blue aqua marine"</code> or <code>class="marine blue pastoral aqua" </code>but
    not <code>class="pastoral blue"</code>.
</div>
<h3><a name=id-selectors>6.5 ID selectors</a></h3>
<p>Document languages may contain attributes that are declared to be of type ID.
  What makes attributes of type ID special is that no two such attributes can
  have the same value in a document, regardless of the type of the elements that
  carry them; whatever the document language, an ID typed attribute can be used
  to uniquely identify its element. In HTML all ID attributes are named "id";
  XML applications may name ID attributes differently, but the same restriction
  applies.
<p>An ID typed attribute of a document language allows authors to assign an identifier
  to one element instance in the document tree. W3C ID selectors represent an
  element instance based on its identifier. An ID selector contains a &quot;number
  sign&quot; (#) immediately followed by the ID value.
<div class=example>Examples:
  <p>The following ID selector represents an <code>h1</code> element whose ID typed
    attribute has the value "chapter1":
  <pre>h1#chapter1</pre>
  <p>The following ID selector represents any element whose ID typed attribute
    has the value "chapter1":
  <pre>#chapter1</pre>
  The following selector represents any element whose ID typed attribute has the
  value "z98y".
  <pre>*#z98y</pre></div>
<div class=note><i><b>Note.</b> In XML 1.0 <a class=noxref
href="http://www.w3.org/TR/REC-CSS2/refs.html#ref-XML10"
rel=biblioentry>[XML10]</a>, the information about which attribute contains an
  element's IDs is contained in a DTD or a schema. When parsing XML, UAs do not
  always read the DTD, and thus may not know what the ID of an element is
  (though a UA may have namespace specific knowledge that allows it to determine
  which attribute is the ID attribute for that namespace). If
  a style sheet designer knows or suspects that a UA may not know what the ID of an
  element is, he should use normal attribute selectors instead:
  <code>[name=p371]</code> instead of <code>#p371</code>.
  Elements in XML 1.0 documents without a DTD do not have IDs at all.</i></div>
<h3><a name=pseudo-classes>6.6 Pseudo-classes</a></h3>
<p>The pseudo-class concept is introduced to permit selection based on information
  that lies outside of the document tree or that cannot be expressed using the
  other simple selectors.
<p>A pseudo-class always contains a &quot;colon&quot; (<code>:</code>) followed
  by the name of the pseudo-class and optionally by a value between parentheses.

<p>Pseudo-classes are allowed in all sequences of simple selectors contained in
  a selector. Pseudo-classes are allowed anywhere in sequences of simple selectors,
  after the leading type selector or universal selector (possibly omitted). Pseudo-class
  names are case-insensitive. Some pseudo-classes are mutually exclusive, while
  others can be applied simultaneously to the same element. Pseudo-classes may
  be dynamic, in the sense that an element may acquire or lose a pseudo-class
  while a user interacts with the document.
<h4><a name=dynamic-pseudos>6.6.1 Dynamic pseudo-classes</a></h4>
<p>Dynamic pseudo-classes classify elements on characteristics other than their
  name, attributes or content, in principle characteristics that cannot be deduced
  from the document tree.
<p>Dynamic pseudo-classes do not appear in the document source or document tree.

<h5>The&nbsp;<a name=link>link pseudo-classes: :link and :visited</a></h5>
<p>User agents commonly display unvisited links differently from previously
visited ones. <span class="modulename">Selectors</span> provides the pseudo-classes <code>:link</code> and
<code>:visited</code> to distinguish them:
<ul>
  <li>The <code>:link</code> pseudo-class applies for links that have not yet been
  visited.
  <li>The <code>:visited</code> pseudo-class applies once the link has been visited
  by the user. </li></ul>
<div class=note><i><b>Note.</b> After some amount of time, user agents may
choose to return a visited link to the (unvisited) ':link' state.</i></div>
<p>The two states are mutually exclusive.
<div class=example>Example:
  <p>The following selector represents links carrying class <code>external</code> and
already visited: <pre>a.external:visited</pre></div>
<h5>The&nbsp;<a name=useraction-pseudos>user action pseudo-classes :hover,
:active, and :focus</a></h5>
<p>Interactive user agents sometimes change the rendering in response to user
actions. <span class="modulename">Selectors</span> provides three pseudo-classes for the selection of an
element the user is acting on.
<ul>
  <li>The <code>:hover</code> pseudo-class applies while the user designates an
  element (with some pointing device), but does not activate it. For example, a
  visual user agent could apply this pseudo-class when the cursor (mouse
  pointer) hovers over a box generated by the element. User agents not
  supporting <a
 href="http://www.w3.org/TR/REC-CSS2/media.html#interactive-media-group">interactive
  media</a> do not have to support this pseudo-class. Some conforming user
  agents supporting <a
 href="http://www.w3.org/TR/REC-CSS2/media.html#interactive-media-group">interactive
  media</a> may not be able to support this pseudo-class (e.g., a pen device).
  <li>The <code>:active</code> pseudo-class applies while an element is being
  activated by the user. For example, between the times the user presses the
  mouse button and releases it.
  <li>The <code>:focus</code> pseudo-class applies while an element has the focus
  (accepts keyboard or mouse events, or other forms of input). </li></ul>
<p>There may be document language or implementation specific limits on which elements can become
<code>:active</code> or acquire <code>:focus</code>.
<!--
<p>Only elements whose 'user-input' property (see <a
href="#UI-WD">[UI]</a>) has the value of
"enabled" can become <code>:active</code> or acquire <code>:focus</code>. -->
<p>These pseudo-classes are not mutually exclusive. An element may match several
of them at the same time.
<div class=example>Examples:
  <pre>a:link&nbsp;&nbsp;&nbsp; /* unvisited links */
a:visited /* visited links&nbsp;&nbsp; */
a:hover&nbsp;&nbsp; /* user hovers&nbsp;&nbsp;&nbsp;&nbsp; */
a:active&nbsp; /* active links&nbsp;&nbsp;&nbsp; */</pre>
  <p>An example of combining dynamic pseudo-classes: <pre>a:focus
a:focus:hover</pre>
  <p>The last selector matches <code>a</code> elements that are in pseudo-class
    :focus and in pseudo-class :hover.
</div>
<div class=note><i><b>Note.</b> An element can be both ':visited' and ':active'
(or ':link' and ':active').</i></div>
<h4><a name=target-pseudo>6.6.2 The target pseudo-class :target</a></h4>
<p>Some URIs refer to a location within a resource. This kind of URI ends with
  a &quot;number sign&quot; (<code>#</code>) followed by an anchor identifier
  (called the fragment identifier).
<p>URIs with fragment identifiers link to a certain element within the document,
known as the target element. For instance, here is a URI pointing to an anchor
named section_2 in a HTML document:
<pre>http://example.com/html/top.html#section_2</pre>
<p>A target element can be represented by the <code>:target</code> pseudo-class:

<pre>p.note:target</pre>
<p>represents a <code>p</code> of class note that is the target element of the
  referring URI.
<div class=example>CSS example of use of the <code>:target</code> pseudo-class: <pre>*:target { color : red }

*:target::before { content : url(target.png) }</pre></div>
<h4><a name=lang-pseudo>6.6.3 The language pseudo-class :lang</a></h4>
<p>If the document language specifies how the human language of an element is
  determined, it is possible to write selectors that represent an element based
  on its language. For example, in HTML <a
href="#html40"
rel=biblioentry>[HTML4.01]</a>, the language is determined by a combination of
  the <code>lang</code> attribute, the <code>meta</code> element, and possibly
  by information from the protocol (such as HTTP headers). XML uses an attribute
  called <code>xml:lang</code>, and there may be other document language-specific
  methods for determining the language.
<p>The pseudo-class <code>:lang(C)</code> represents an element that is in language
  C. Here C is a language code as specified in HTML 4.01 <a
href="#html40"
rel=biblioentry>[HTML4.01]</a> and RFC 3066 <a
href="#rfc3066"
rel=biblioentry>[RFC3066]</a>.
<div class=example>Examples:
  <p>The two following selectors represent an HTML document that is in Belgian
    French or German. The two next selectors represent <code>q</code> quotations
    in an arbitrary element in Belgian French or German.
  <pre>html:lang(fr-be)
html:lang(de)
:lang(fr-be) &gt; q
:lang(de) &gt; q</pre>
</div>
<h4><a name=UIstates>6.6.4 The UI element states pseudo-classes</a></h4>
<h5><a name=enableddisabled>The :enabled and :disabled pseudo-classes</a></h5>
<p>The purpose of the <code>:enabled</code> pseudo-class is to allow authors to
  customize the look of user interface elements which are enabled - which the
  user can select/activate in some fashion (e.g. clicking on a button with a mouse).
  There is a need for such a pseudo-class because there is no way to programmatically
  specify the default appearance of say, an enabled <code>input</code> element
  without also specifying what it would look like when it was disabled.
<p>Similar to <code>:enabled</code>, <code>:disabled</code> allows the author to specify
precisely how a disabled or inactive user interface element should look.
<p>It should be noted that most elements will be neither enabled nor disabled.
An element is enabled if the user can either activate it or transfer the focus
to it. An element is disabled if it could be enabled, but the user cannot
presently activate it or transfer focus to it.
<h5><a name=checked>The :checked pseudo-class</a></h5>
<p><!--The <code>:checked</code> pseudo-class only applies to elements which are
'user-input: enabled' or 'user-input : disabled' (see [UI] for the 'user-input'
property). -->Radio and checkbox elements can be toggled by the user. Some menu
items are "checked" when the user selects them. When such elements are toggled
"on" the <code>:checked</code> pseudo-class applies. The <code>:checked</code>
pseudo-class initially applies to such elements that have the HTML4
<code>selected</code> attribute as described in <a
href="http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.2.1">Section
17.2.1 of HTML4</a>, but of course the user can toggle "off" such elements in
which case the <code>:checked</code> pseudo-class would no longer apply. While the
<code>:checked</code> pseudo-class is dynamic in nature, and is altered by user
action, since it can also be based on the presence of the semantic HTML4
<code>selected</code> attribute, it applies to all media.
<h5><a name=indeterminate>The :indeterminate pseudo-class</a></h5>
<p><!--The <code>:indeterminate</code> pseudo-class only applies to elements which are
'user-input: enabled' or 'user-input: disabled' (see <a
href="#UI-WD">[UI]</a> for the 'user-input'
property). -->Radio and checkbox elements can be toggled by the user, but are
sometimes in an indeterminate state, neither checked nor unchecked. This can be
due to an element attribute, or DOM manipulation. The <code>:indeterminate</code>
pseudo-class applies to such elements. While the <code>:indeterminate</code>
pseudo-class is dynamic in nature, and is altered by user action, since it can
also be based on the presence of an element attribute, it applies to all media.

<p>Components of a radio-group initialized with no pre-selected choice are an
example of :indeterminate state.
<h4><a name=structural-pseudos>6.6.5 Structural pseudo-classes</a></h4>
<p><span class="modulename">Selectors</span> introduces the concept of&nbsp;<dfn>structural
pseudo-classes</dfn> to permit selection based on extra information that lies in
the document tree but cannot be represented by other simple selectors or
combinators.
<p>Note that standalone PCDATA are not counted when calculating the position of
an element in the list of children of its parent. When calculating the position
of an element in the list of children of its parent, the index numbering starts
at 1.
<h5><a name=root-pseudo>:root pseudo-class</a></h5>
<p>The <code>:root</code> pseudo-class represents an element that is the root
  of the document. In HTML 4, this is the <code>HTML</code> element. In XML, it
  is whatever is appropriate for the DTD or schema and namespace for that XML
  document.
<h5><a name=nth-child-pseudo>:nth-child() pseudo-class</a></h5>
<p>The <code>:nth-child(an+b)</code> pseudo-class notation represents an element
  that has an+b-1 siblings <strong>before</strong> it in the document tree, for
  a given positive integer or zero value of n. In other words, this matches the
  bth child of an element after all the children have been split into groups of
  a elements each. For example, this allows the selectors to address every other
  row in a table, and could be used, for example, to alternate the color of paragraph
  text in a cycle of four. The a and b values must be zero, negative integers
  or positive integers. The index of the first child of an element is 1.
<p>In addition to this, <code>:nth-child()</code> can take 'odd' and 'even' for
argument. 'odd' has the same signification as 2n+1, and 'even' has the same
signification as 2n.
<div class=example>Examples:
<pre>tr:nth-child(2n+1) /* represents every odd row of a HTML table */
tr:nth-child(odd)  /* same */
tr:nth-child(2n)   /* represents every even row of a HTML table */
tr:nth-child(even) /* same */

/* Alternate paragraph colours in CSS */
p:nth-child(4n+1) { color: navy; }
p:nth-child(4n+2) { color: green; }
p:nth-child(4n+3) { color: maroon; }
p:nth-child(4n+4) { color: purple; }</pre>
</div>
<p>When a=0, no repeating is used, so for example <code>:nth-child(0n+5)</code>
matches only the fifth child. When a=0, the a part need not be included, so the
syntax simplifies to <code>:nth-child(b)</code> and the last example simplifies
to <code>:nth-child(5)</code>.
<div class=example>
<pre>foo:nth-child(0n+1)   /* represents an element foo, first child of its parent element */
foo:nth-child(1)      /* same */</pre>
</div>
<p>When a=1, the number may be omitted from the rule,
so the following examples are equivalent:
<div class=example>
<pre>bar:nth-child(1n+0)   /* represents all bar elements, specificity (0,1,1) */
bar:nth-child(n+0)    /* same */
bar:nth-child(n)      /* same */
bar                   /* same but lower specificity (0,0,1) */</pre>
</div>
<p>If b=0, then every a-th element is picked:
<div class=example>
<pre>tr:nth-child(2n) /* represents every even row of a HTML table */</pre>
</div>
<p>If both a and b are equal to zero, the pseudo-class represents no element in
the document tree.
<p>The value a can be negative, but only the positive values of an+b, for n&gt;=
  0, may represent an element in the document tree, of course:
<div class=example>
<pre>html|tr:nth-child(-n+6)  /* represents the 6 first rows of XHTML tables */</pre>
</div>
<h5><a name=nth-last-child-pseudo>:nth-last-child() pseudo-class</a></h5>
<p>The <code>:nth-last-child(an+b)</code> pseudo-class notation represents an
element that has an+b-1 siblings <strong>after</strong> it in the document tree,
for a given positive integer or zero value of n. See <code>:nth-child()</code>
pseudo-class for the syntax of its argument. It also accepts the 'even' and
'odd' values for argument.
<div class=example>Examples: <pre>tr:nth-last-child(-n+2)    /* represents the two last rows of a HTML table */

foo:nth-last-child(odd)    /* represents all odd foo elements in their parent element,
                              counting from the last one */</pre></div>
<h5><a name=nth-of-type-pseudo>:nth-of-type() pseudo-class</a></h5>
<p>The <code>:nth-of-type(an+b)</code> pseudo-class notation represents an element
that has an+b-1 siblings with the same element name <strong>before</strong> it
in the document tree, for a given zero or positive integer value of n. In other
words, this matches the bth child of that type after all the children of that
type have been split into groups of a elements each. See
<code>:nth-child()</code> pseudo-class for the syntax of its argument. It also
accepts the 'even' and 'odd' values for argument.
<div class=example>For example, this allows in CSS to alternate the position of
floated images: <pre>img:nth-of-type(2n+1) { float: right; }
img:nth-of-type(2n) { float: left; }
</pre></div>
<h5><a name=nth-last-of-type-pseudo>:nth-last-of-type() pseudo-class</a></h5>
<p>The <code>:nth-last-of-type(an+b)</code> pseudo-class notation represents an
element that has an+b-1 siblings with the same element name
<strong>after</strong> it in the document tree, for a given zero or positive
integer value of n. See <code>:nth-child()</code> pseudo-class for the syntax of
its argument. It also accepts the 'even' and 'odd' values for argument.
<div class=example>For example, to represent all <code>h2</code> children of a
XHTML <code>body</code> except the first and last, one would use the following
selector: <pre>body &gt; h2:nth-of-type(n+2):nth-last-of-type(n+2)</pre>
<p>In this case, one could also use <code>:not()</code>, although the selector
ends up being just as long:<pre>body &gt; h2:not(:first-of-type):not(:last-of-type) </pre></div>
<h5><a name=first-child-pseudo>:first-child pseudo-class</a></h5>
<p>Same as <code>:nth-child(1)</code>. The <code>:first-child</code> pseudo-class
represents an element that is the first child of some other element.
<div class=example>Examples:
  <p>In the following example, the selector represents a <code>p</code> element that
is the first child of a <code>div</code> element: <pre>div &gt; p:first-child</pre>This selector can represent the <code>p</code>
inside the <code>div</code> of the following fragment: <pre>&lt;p&gt; The last P before the note.&lt;/p&gt;
&lt;div class="note"&gt;
&nbsp;&nbsp; &lt;p&gt; The first P inside the note.&lt;/p&gt;
&lt;/div&gt;</pre>but cannot represent the second <code>p</code> in the following
fragment: <pre>&lt;p&gt; The last P before the note.&lt;/p&gt;
&lt;div class="note"&gt;
&nbsp;&nbsp; &lt;h2&gt;Note&lt;/h2&gt;
&nbsp;&nbsp; &lt;p&gt; The first P inside the note.&lt;/p&gt;
&lt;/div&gt;</pre>The following two selectors are equivalent: <pre>* &gt; a:first-child&nbsp;&nbsp; /* a is first child of any element */
a:first-child&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* Same */</pre></div>
<h5><a name=last-child-pseudo>:last-child pseudo-class</a></h5>
<p>Same as <code>:nth-last-child(1)</code>.The <code>:last-child</code> pseudo-class
represents an element that is the last child of some other element.
<p>The following selector represents a list item <code>li</code> that is the last
child of an ordered list <code>ol</code>.
<div class=example>Example:
<pre>ol &gt; li:last-child</pre></div>
<h5><a name=first-of-type-pseudo>:first-of-type pseudo-class</a></h5>
<p>Same as <code>:nth-of-type(1)</code>.The <code>:first-of-type</code> pseudo-class
represents an element that is the first sibling of its type in the list of
children of its parent element.
<div class=example>Example:
<p>The following selector represents a definition title <code>dt</code> inside a
definition list <code>dl</code>, this <code>dt</code> being the first of its type in
the list of children of its parent element. <pre>dl dt:first-of-type</pre>It is a valid description for the first two
<code>dt</code> in the following example but not for the third one: <pre>&lt;dl&gt;&lt;dt&gt;gigogne&lt;/dt&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;dd&gt;&lt;dl&gt;&lt;dt&gt;fus&amp;eacute;e&lt;/dt&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;dd&gt;multistage rocket&lt;/dd&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;dt&gt;table&lt;/dt&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;dd&gt;nest of tables&lt;/dd&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/dl&gt;&lt;/dd&gt;
&lt;/dl&gt;</pre></div>
<h5><a name=last-of-type-pseudo>:last-of-type pseudo-class</a></h5>
<p>Same as <code>:nth-last-of-type(1)</code>.The <code>:last-of-type</code>
pseudo-class represents an element that is the last sibling of its type in the
list of children of its parent element.
<div class=example>Example:
<p>The following selector represents the last data cell <code>td</code> of a table
row. <pre>tr &gt; td:last-of-type</pre></div>
<h5><a name=only-child-pseudo>:only-child pseudo-class</a></h5>
<p>Represents an element that has no siblings. Same as
<code>:first-child:last-child</code> or
<code>:nth-child(1):nth-last-child(1)</code>, but with a lower specificity.
<h5><a na
<script>
Benchmark.prototype.setup = function() {
    var match = document.querySelectorAll(".selectorsreview tr")[0]
};
</script>

Preparation code output

W3C

Selectors

W3C Candidate Recommendation 13 November 2001

This version:
http://www.w3.org/TR/2001/CR-css3-selectors-20011113
Latest version:
http://www.w3.org/TR/css3-selectors
Previous version:
http://www.w3.org/TR/2001/WD-css3-selectors-20010126
Editors:
Daniel Glazman (Netscape/AOL)
Tantek Çelik (Microsoft Corporation)
Ian Hickson
Peter Linss (former editor, formerly of Netscape/AOL)
John Williams (former editor, Quark, Inc.)

Abstract

CSS (Cascading Style Sheets) is a language for describing the rendering of HTML and XML documents on screen, on paper, in speech, etc. To bind style properties to elements in the document, CSS uses selectors, which are patterns that match one or more elements. This document describes the selectors that are proposed for CSS level 3. It includes and extends the selectors of CSS level 2.

Status of this document

This document is one of the "modules" of the upcoming CSS3 specification. It not only describes the selectors that already exist in CSS1 and CSS2, but also proposes new selectors for CSS3 as well as for other languages that may need them. The CSS Working Group doesn't expect that all implementations of CSS3 will have to implement all selectors. Instead, there will probably be a small number of variants of CSS3, so-called "profiles". For example, it may be that only a profile for non-interactive user agents will include all of the proposed selectors.

This specification is being put forth as a Candidate Recommendation by the CSS Working Group. This document is a revision of the Working Draft dated 2001 January 26, and has incorporated suggestions received during last call review, comments, and further deliberations of the W3C CSS Working Group.

The duration of Candidate Recommendation is expected to last approximately six months (ending May, 2002). All persons are encouraged to review and implement this specification and return comments to the (archived) public mailing list www-style (see instructions). W3C Members can also send comments directly to the CSS Working Group.

Should this specification prove impossible to implement, the Working Group will return the document to Working Draft status and make necessary changes. Otherwise, the Working Group anticipates asking the W3C Director to advance this document to Proposed Recommendation.

This is still a draft document and may be updated, replaced, or obsoleted by other documents at any time. It is inappropriate to cite a W3C Candidate Recommendation as other than "work in progress." A list of current W3C working drafts can be found at http://www.w3.org/TR.

This document may be available in translation. The English version of this specification is the only normative version.

Dependencies with other CSS3 Modules

  • General Syntax
  • Value Assignment, Cascade and Inheritance
  • Generated Content / Markers
  • User Interface

Table of contents

1. Context

Members of the CSS+FP Working Group proposed during the Clamart meeting to modularize the CSS specification.

This modularization, and the externalization of the general syntax of CSS will reduce the size of the specification and allow new specifications to use selectors and/or CSS general syntax. For instance, behaviors or tree transformations.

This specification contains its own test cases, one test per concept introduced in this document. These tests are not full conformance tests but are intended to provide users with a way to check if a part of this specification is implemented ad minima or is not implemented at all.

1.1 Changes from CSS2

The main differences between the selectors in CSS2 and those in Selectors are:

  • the list of basic definitions (selector, group of selectors, simple selector, etc.) has been clarified
  • an optional namespace component is now allowed in type element selectors, the universal selector and attribute selectors
  • a new combinator
  • new simple selectors including substring matching attribute selectors, and new pseudo-classes
  • new pseudo-elements, and introduction of the "::" convention for pseudo-elements
  • a rewriting of the selectors grammar
  • profiles to be added to specifications integrating Selectors and defining the set of selectors which is actually supported by each specification
  • Selectors are now a CSS3 Module and an independent specification. Other specifications can now refer to this document independently of CSS
  • the specification now contains its own test suite.

2. Selectors

A Selector represents a structure. This structure can be used as a condition (e.g. in a CSS rule) that determines which elements a selector matches in the document tree, or as a flat description of the HTML or XML fragment corresponding to that structure.

Selectors may range from simple element names to rich contextual representations.

The following table summarizes Selector syntax:

Pattern Meaning Described in section First defined in CSS level
* any element Universal selector 2
E an element of type E Type selector 1
E[foo] an E element with a "foo" attribute Attribute selectors 2
E[foo="bar"] an E element whose "foo" attribute value is exactly equal to "bar" Attribute selectors 2
E[foo~="bar"] an E element whose "foo" attribute value is a list of space-separated values, one of which is exactly equal to "bar" Attribute selectors 2
E[foo^="bar"] an E element whose "foo" attribute value begins exactly with the string "bar" Attribute selectors 3
E[foo$="bar"] an E element whose "foo" attribute value ends exactly with the string "bar" Attribute selectors 3
E[foo*="bar"] an E element whose "foo" attribute value contains the substring "bar" Attribute selectors 3
E[hreflang|="en"] an E element whose "hreflang" attribute has a hyphen-separated list of values beginning (from the left) with "en" Attribute selectors 2
E:root an E element, root of the document Structural pseudo-classes 3
E:nth-child(n) an E element, the n-th child of its parent Structural pseudo-classes 3
E:nth-last-child(n) an E element, the n-th child of its parent, counting from the last one Structural pseudo-classes 3
E:nth-of-type(n) an E element, the n-th sibling of its type Structural pseudo-classes 3
E:nth-last-of-type(n) an E element, the n-th sibling of its type, counting from the last one Structural pseudo-classes 3
E:first-child an E element, first child of its parent Structural pseudo-classes 2
E:last-child an E element, last child of its parent Structural pseudo-classes 3
E:first-of-type an E element, first sibling of its type Structural pseudo-classes 3
E:last-of-type an E element, last sibling of its type Structural pseudo-classes 3
E:only-child an E element, only child of its parent Structural pseudo-classes 3
E:only-of-type an E element, only sibling of its type Structural pseudo-classes 3
E:empty an E element that has no children (including text nodes) Structural pseudo-classes 3
E:link
E:visited
an E element being the source anchor of a hyperlink of which the target is not yet visited (:link) or already visited (:visited) The link pseudo-classes 1
E:active
E:hover
E:focus
an E element during certain user actions The user action pseudo-classes 1 and 2
E:target an E element being the target of the referring URI The target pseudo-class 3
E:lang(fr) an element of type E in language "fr" (the document language specifies how language is determined) The :lang() pseudo-class  2
E:enabled
E:disabled 
a user interface element E which is enabled or disabled The UI element states pseudo-classes 3
E:checked
E:indeterminate 
a user interface element E which is checked or in an indeterminate state (for instance a radio-button or checkbox) The UI element states pseudo-classes 3
E:contains("foo") an E element containing the substring "foo" in its textual contents Content pseudo-class 3
E::first-line the first formatted line of an E element The :first-line pseudo-element 1
E::first-letter the first formatted letter of an E element The :first-letter pseudo-element 1
E::selection the portion of an E element that is currently selected/highlighted by the user The UI element fragments pseudo-elements 3
E::before generated content before an E element The :before pseudo-element 2
E::after generated content after an E element The :after pseudo-element 2
E.warning an E element whose class is "warning" (the document language specifies how class is determined). Class selectors 1
E#myid an E element with ID equal to "myid". ID selectors 1
E:not(s) an E element that does not match simple selector s Negation pseudo-class 3
E F an F element descendant of an E element Descendant combinator 1
E > F an F element child of an E element Child combinator 2
E + F an F element immediately preceded by an E element Direct adjacent combinator 2
E ~ F an F element preceded by an E element Indirect adjacent combinator 3

The meaning of each selector is derived from the table above by prepending "matches" to the contents of each cell of the "Meaning" column.

3. Case sensitivity

The case-sensitivity of document language element names in selectors depends on the document language. For example, in HTML, element names are case-insensitive, but in XML they are case-sensitive.

The case-sensitivity of attribute names and attribute values in attribute selectors also depends on the document language.

4. Selector syntax

selector is a chain of one or more sequences of simple selectors separated by combinators.

sequence of simple selectors is a chain of simple selectors that are not separated by a combinator. It always begins with a type selector or a universal selector. No other type selector or universal selector is allowed in the sequence.

simple selector is either a type selector, universal selector, attribute selector, ID selector, content selector, or pseudo-class. One pseudo-element may be appended to the last sequence of simple selectors.

Combinators are: white space, "greater-than sign" (>), "plus sign" (+) and "tilde" (~). White space may appear between a combinator and the simple selectors around it. Only the characters "space" (Unicode code 32), "tab" (9), "line feed" (10), "carriage return" (13), and "form feed" (12) can occur in white space. Other space-like characters, such as "em-space" (8195) and "ideographic space" (12288), are never part of white space.

The elements of the document tree represented by a selector are called subjects of the selector. A selector consisting of a single sequence of simple selectors represents any element satisfying its requirements. Prepending another sequence of simple selectors and a combinator to a sequence imposes additional matching constraints, so the subjects of a selector are always a subset of the elements represented by the rightmost sequence of simple selectors.

Note: an empty selector, containing no sequence of simple selectors and no combinator, is an invalid selector.

5. Groups of selectors

When several selectors share the same declarations, they may be grouped into a comma-separated list.

CSS example(s):

In this example, we condense three rules with identical declarations into one. Thus,

h1 { font-family: sans-serif }
h2 { font-family: sans-serif }
h3 { font-family: sans-serif }
is equivalent to:
h1, h2, h3 { font-family: sans-serif }

Warning: the equivalence is true in this example because all selectors are valid selectors. If just one of these selectors is invalid, the entire group of selectors is invalid thus invalidating the rule for all three heading elements, whereas only one of the three individual heading rules would be invalid.

6. Simple selectors

6.1 Type selector

type selector is the name of a document language element type. A type selector represents an instance of the element type in the document tree.

Example:

The following selector represents an h1 element in the document tree:

h1

6.1.1 Type selectors and Namespaces

Type selectors allow an optional namespace ([XML-NAMES]) component. A namespace prefix that has been previously declared may be prepended to the element name separated by the namespace separator "vertical bar" (|). The namespace component may be left empty to indicate that the selector is only to represent elements with no declared namespace. Furthermore, an asterisk may be used for the namespace prefix, indicating that the selector represents elements in any namespace (including elements with no namespace). Element type selectors that have no namespace component (no namespace separator), represent elements without regard to the element's namespace (equivalent to "*|") unless a default namespace has been declared. In that case, the selector will represent only elements in the default namespace.

Note : a type selector containing a namespace prefix that has not been previously declared is an invalid selector. The mechanism for declaring a namespace prefix is left up to the language implementing Selectors. In CSS, such a mechanism is defined in the General Syntax module.

It should be noted that if a namespace prefix used in a selector has not been previously declared, then the selector must be considered invalid and the entire style rule will be ignored in accordance with the standard error handling rules.

It should further be noted that in a namespace aware client, element type selectors will only match against the local part of the element's qualified name. See below for notes about matching behaviors in down-level clients.

In summary:

ns|E
elements with name E in namespace ns
*|E
elements with name E in any namespace, including those without any declared namespace
|E
elements with name E without any declared namespace
E
if no default namespace has been specified, this is equivalent to *|E. Otherwise it is equivalent to ns|E where ns is the default namespace.

CSS examples:

@namespace foo url(http://www.example.com);

foo|h1 { color: blue }

foo|* { color: yellow }

|h1 { color: red }

*|h1 { color: green }

h1 { color: green }

The first rule will match only h1 elements in the "http://www.example.com" namespace.

The second rule will match all elements in the "http://www.example.com" namespace.

The third rule will match only h1 elements without any declared namespace.

The fourth rule will match h1 elements in any namespace (including those without any declared namespace).

The last rule is equivalent to the fourth rule because no default namespace has been defined.

6.2 Universal selector

The universal selector, written "asterisk" (*), represents the qualified name of any element type. It represents then any single element in the document tree in any namespace (including those without any declared namespace) if no default namespace has been specified. If a default namespace has been specified, see Universal selector and Namespaces below.

If the universal selector is not the only component of a sequence of simple selectors, the * may be omitted. For example:

  • *[hreflang|=en] and [hreflang|=en] are equivalent,
  • *.warning and .warning are equivalent,
  • *#myid and #myid are equivalent.

Note: it is recommended that the *, representing the universal selector, not be omitted.

6.2.1 Universal selector and Namespaces

The universal selector allows an optional namespace component.

ns|*
all elements in namespace ns
*|*
all elements
|*
all elements without any declared namespace
*
if no default namespace has been specified, this is equivalent to *|*. Otherwise it is equivalent to ns|* where ns is the default namespace.

Note: a universal selector containing a namespace prefix that has not been previously declared is an invalid selector. The mechanism for declaring a namespace prefix is left up to the language implementing Selectors. In CSS, such a mechanism is defined in the General Syntax module.

6.3 Attribute selectors

Selectors allow the representation of an element's attributes.

6.3.1 Attribute presence and values selectors

CSS2 introduced four attribute selectors:

[att]
Represents the att attribute, whatever the value of the attribute.
[att=val]
Represents the att attribute with value exactly "val".
[att~=val]
Represents the att attribute whose value is a space-separated list of words, one of which is exactly "val". If this selector is used, the words in the value must not contain spaces (since they are separated by spaces).
[att|=val]
Represents the att attribute, its value either being exactly "val" or beginning with "val" immediately followed by "-". This is primarily intended to allow language subcode matches (e.g., the hreflang attribute on the link element in HTML) as described in RFC 3066 ([RFC3066]). Note: for lang (or xml:lang) language subcode matching, please see the :lang pseudo-class.

Attribute values must be identifiers or strings. The case-sensitivity of attribute names and values in selectors depends on the document language.

Examples:

For example, the following attribute selector represents an h1 element that carries the title attribute, whatever its value:

h1[title]

In the following example, the selector represents a span element whose class attribute has exactly the value "example":

span[class=example]
Multiple attribute selectors can be used to represent several attributes of an element, or several conditions on the same attribute.

Here, the selector represents a span element whose hello attribute has exactly the value "Cleveland" and whose goodbye attribute has exactly the value "Columbus":

span[hello="Cleveland"][goodbye="Columbus"]

The following selectors illustrate the differences between "=" and "~=". The first selector will represent, for example, the value "copyright copyleft copyeditor" on a rel attribute. The second selector will only represent an a element with an href attribute having the exact value "http://www.w3.org/".

a[rel~="copyright"]
a[href="http://www.w3.org/"]

The following selector represents a link element whose hreflang attribute is exactly "fr".

link[hreflang=fr]

The following selector represents a link element for which the values of the hreflang attribute begins with "en", including "en", "en-US", and "en-cockney":

link[hreflang|="en"]

Similarly, the following selectors represents a DIALOGUE element whenever it has one of two different values for an attribute character:

DIALOGUE[character=romeo] 

DIALOGUE[character=juliet]

6.3.2 Substring matching attribute selectors

Three additional attribute selectors are provided for matching substrings in the value of an attribute:

[att^=val]
Represents the att attribute whose value begins with the prefix "val"
[att$=val]
Represents the att attribute whose value ends with the suffix "val"
[att*=val]
Represents the att attribute whose value contains at least one instance of the substring "val"

Attribute values must be identifiers or strings. The case-sensitivity of attribute names in selectors depends on the document language.

Example:

The following selector represents an HTML object, referencing an image:

object[type^="image/"]

The following selector represents an HTML anchor a with an href attribute whose value ends with ".html".

a[href$=".html"]

The following selector represents a HTML paragraph with a title attribute whose value contains the substring "hello"

p[title*="hello"] 

6.3.3 Attribute selectors and Namespaces

Attribute selectors allow an optional namespace component to the attribute name. A namespace prefix that has been previously declared may be prepended to the attribute name separated by the namespace separator "vertical bar" (|). In keeping with the Namespaces in the XML recommendation, default namespaces do not apply to attributes, therefore attribute selectors without a namespace component apply only to attributes that have no declared namespace (equivalent to "|attr"). An asterisk may be used for the namespace prefix indicating that the selector is to match all attribute names without regard to the attribute's namespace.

Note : an attribute selector with an attribute name containing a namespace prefix that has not been previously declared is an invalid selector. The mechanism for declaring a namespace prefix is left up to the language implementing Selectors. In CSS, such a mechanism is defined in the General Syntax module.

CSS examples:

@namespace foo "http://www.example.com";

[foo|att=val] { color: blue }

[*|att] { color: yellow }

[|att] { color: green }

[att] { color: green }
The first rule will match only elements with the attribute att in the "http://www.example.com" namespace with the value "val".

The second rule will match only elements with the attribute att regardless of the namespace of the attribute (including no declared namespace).

The last two rules are equivalent and will match only elements with the attribute att where the attribute is not declared to be in a namespace.

6.3.4 Default attribute values in DTDs

Attribute selectors represent explicitly set attribute values in the document tree. Default attribute values may be defined in a DTD or elsewhere. Selectors should be designed so that they work even if the default values are not included in the document tree.

Examples:

For example, consider an element EXAMPLE with an attribute notation that has a default value of "decimal". The DTD fragment might be

<!ATTLIST EXAMPLE notation (decimal,octal) "decimal">
If the selectors represent an EXAMPLE element when the value of the attribute is explicitly set:
EXAMPLE[notation=decimal]
EXAMPLE[notation=octal]
then to represent only the case where this attribute is set by default, and not explicitly, the following selector might be used:
EXAMPLE:not([notation])

6.4 Class selectors

Working with HTML, authors may use the period (.) notation as an alternative to the ~= notation when representing the class attribute. Thus, for HTML, div.value and div[class~=value] have the same meaning. The attribute value must immediately follow the "period" (.). Note: UAs may apply selectors using the period (.) notation in XML documents if the UA has namespace specific knowledge that allows it to determine which attribute is the "class" attribute for the respective namespace. One such example of namespace specific knowledge is the prose in the specification for a particular namespace (e.g. SVG 1.0 [SVG] describes the SVG "class" attribute and how a UA should interpret it, and similarly MathML 1.01 [MATH] describes the MathML "class" attribute.)

Examples:

For example, we can represent an arbitrary element with class~="pastoral" as follows:

*.pastoral
or just
.pastoral
The following selector represents an h1 element with class~="pastoral":
h1.pastoral

For example, the following selector represents a p element whose class attribute has been assigned a list of space-separated values that includes "pastoral" and "marine":

p.pastoral.marine

It is fully identical to:

p.marine.pastoral

This selector represents for example a p with class="pastoral blue aqua marine" or class="marine blue pastoral aqua" but not class="pastoral blue".

6.5 ID selectors

Document languages may contain attributes that are declared to be of type ID. What makes attributes of type ID special is that no two such attributes can have the same value in a document, regardless of the type of the elements that carry them; whatever the document language, an ID typed attribute can be used to uniquely identify its element. In HTML all ID attributes are named "id"; XML applications may name ID attributes differently, but the same restriction applies.

An ID typed attribute of a document language allows authors to assign an identifier to one element instance in the document tree. W3C ID selectors represent an element instance based on its identifier. An ID selector contains a "number sign" (#) immediately followed by the ID value.

Examples:

The following ID selector represents an h1 element whose ID typed attribute has the value "chapter1":

h1#chapter1

The following ID selector represents any element whose ID typed attribute has the value "chapter1":

#chapter1
The following selector represents any element whose ID typed attribute has the value "z98y".
*#z98y
Note. In XML 1.0 [XML10], the information about which attribute contains an element's IDs is contained in a DTD or a schema. When parsing XML, UAs do not always read the DTD, and thus may not know what the ID of an element is (though a UA may have namespace specific knowledge that allows it to determine which attribute is the ID attribute for that namespace). If a style sheet designer knows or suspects that a UA may not know what the ID of an element is, he should use normal attribute selectors instead: [name=p371] instead of #p371. Elements in XML 1.0 documents without a DTD do not have IDs at all.

6.6 Pseudo-classes

The pseudo-class concept is introduced to permit selection based on information that lies outside of the document tree or that cannot be expressed using the other simple selectors.

A pseudo-class always contains a "colon" (:) followed by the name of the pseudo-class and optionally by a value between parentheses.

Pseudo-classes are allowed in all sequences of simple selectors contained in a selector. Pseudo-classes are allowed anywhere in sequences of simple selectors, after the leading type selector or universal selector (possibly omitted). Pseudo-class names are case-insensitive. Some pseudo-classes are mutually exclusive, while others can be applied simultaneously to the same element. Pseudo-classes may be dynamic, in the sense that an element may acquire or lose a pseudo-class while a user interacts with the document.

6.6.1 Dynamic pseudo-classes

Dynamic pseudo-classes classify elements on characteristics other than their name, attributes or content, in principle characteristics that cannot be deduced from the document tree.

Dynamic pseudo-classes do not appear in the document source or document tree.

The link pseudo-classes: :link and :visited

User agents commonly display unvisited links differently from previously visited ones. Selectors provides the pseudo-classes :link and :visited to distinguish them:

  • The :link pseudo-class applies for links that have not yet been visited.
  • The :visited pseudo-class applies once the link has been visited by the user.
Note. After some amount of time, user agents may choose to return a visited link to the (unvisited) ':link' state.

The two states are mutually exclusive.

Example:

The following selector represents links carrying class external and already visited:

a.external:visited
The user action pseudo-classes :hover, :active, and :focus

Interactive user agents sometimes change the rendering in response to user actions. Selectors provides three pseudo-classes for the selection of an element the user is acting on.

  • The :hover pseudo-class applies while the user designates an element (with some pointing device), but does not activate it. For example, a visual user agent could apply this pseudo-class when the cursor (mouse pointer) hovers over a box generated by the element. User agents not supporting interactive media do not have to support this pseudo-class. Some conforming user agents supporting interactive media may not be able to support this pseudo-class (e.g., a pen device).
  • The :active pseudo-class applies while an element is being activated by the user. For example, between the times the user presses the mouse button and releases it.
  • The :focus pseudo-class applies while an element has the focus (accepts keyboard or mouse events, or other forms of input).

There may be document language or implementation specific limits on which elements can become :active or acquire :focus.

These pseudo-classes are not mutually exclusive. An element may match several of them at the same time.

Examples:
a:link    /* unvisited links */
a:visited /* visited links   */
a:hover   /* user hovers     */
a:active  /* active links    */

An example of combining dynamic pseudo-classes:

a:focus
a:focus:hover

The last selector matches a elements that are in pseudo-class :focus and in pseudo-class :hover.

Note. An element can be both ':visited' and ':active' (or ':link' and ':active').

6.6.2 The target pseudo-class :target

Some URIs refer to a location within a resource. This kind of URI ends with a "number sign" (#) followed by an anchor identifier (called the fragment identifier).

URIs with fragment identifiers link to a certain element within the document, known as the target element. For instance, here is a URI pointing to an anchor named section_2 in a HTML document:

http://example.com/html/top.html#section_2

A target element can be represented by the :target pseudo-class:

p.note:target

represents a p of class note that is the target element of the referring URI.

CSS example of use of the :target pseudo-class:
*:target { color : red }

*:target::before { content : url(target.png) }

6.6.3 The language pseudo-class :lang

If the document language specifies how the human language of an element is determined, it is possible to write selectors that represent an element based on its language. For example, in HTML [HTML4.01], the language is determined by a combination of the lang attribute, the meta element, and possibly by information from the protocol (such as HTTP headers). XML uses an attribute called xml:lang, and there may be other document language-specific methods for determining the language.

The pseudo-class :lang(C) represents an element that is in language C. Here C is a language code as specified in HTML 4.01 [HTML4.01] and RFC 3066 [RFC3066].

Examples:

The two following selectors represent an HTML document that is in Belgian French or German. The two next selectors represent q quotations in an arbitrary element in Belgian French or German.

html:lang(fr-be)
html:lang(de)
:lang(fr-be) > q
:lang(de) > q

6.6.4 The UI element states pseudo-classes

The :enabled and :disabled pseudo-classes

The purpose of the :enabled pseudo-class is to allow authors to customize the look of user interface elements which are enabled - which the user can select/activate in some fashion (e.g. clicking on a button with a mouse). There is a need for such a pseudo-class because there is no way to programmatically specify the default appearance of say, an enabled input element without also specifying what it would look like when it was disabled.

Similar to :enabled, :disabled allows the author to specify precisely how a disabled or inactive user interface element should look.

It should be noted that most elements will be neither enabled nor disabled. An element is enabled if the user can either activate it or transfer the focus to it. An element is disabled if it could be enabled, but the user cannot presently activate it or transfer focus to it.

The :checked pseudo-class

Radio and checkbox elements can be toggled by the user. Some menu items are "checked" when the user selects them. When such elements are toggled "on" the :checked pseudo-class applies. The :checked pseudo-class initially applies to such elements that have the HTML4 selected attribute as described in Section 17.2.1 of HTML4, but of course the user can toggle "off" such elements in which case the :checked pseudo-class would no longer apply. While the :checked pseudo-class is dynamic in nature, and is altered by user action, since it can also be based on the presence of the semantic HTML4 selected attribute, it applies to all media.

The :indeterminate pseudo-class

Radio and checkbox elements can be toggled by the user, but are sometimes in an indeterminate state, neither checked nor unchecked. This can be due to an element attribute, or DOM manipulation. The :indeterminate pseudo-class applies to such elements. While the :indeterminate pseudo-class is dynamic in nature, and is altered by user action, since it can also be based on the presence of an element attribute, it applies to all media.

Components of a radio-group initialized with no pre-selected choice are an example of :indeterminate state.

6.6.5 Structural pseudo-classes

Selectors introduces the concept of structural pseudo-classes to permit selection based on extra information that lies in the document tree but cannot be represented by other simple selectors or combinators.

Note that standalone PCDATA are not counted when calculating the position of an element in the list of children of its parent. When calculating the position of an element in the list of children of its parent, the index numbering starts at 1.

:root pseudo-class

The :root pseudo-class represents an element that is the root of the document. In HTML 4, this is the HTML element. In XML, it is whatever is appropriate for the DTD or schema and namespace for that XML document.

:nth-child() pseudo-class

The :nth-child(an+b) pseudo-class notation represents an element that has an+b-1 siblings before it in the document tree, for a given positive integer or zero value of n. In other words, this matches the bth child of an element after all the children have been split into groups of a elements each. For example, this allows the selectors to address every other row in a table, and could be used, for example, to alternate the color of paragraph text in a cycle of four. The a and b values must be zero, negative integers or positive integers. The index of the first child of an element is 1.

In addition to this, :nth-child() can take 'odd' and 'even' for argument. 'odd' has the same signification as 2n+1, and 'even' has the same signification as 2n.

Examples:
tr:nth-child(2n+1) /* represents every odd row of a HTML table */
tr:nth-child(odd)  /* same */
tr:nth-child(2n)   /* represents every even row of a HTML table */
tr:nth-child(even) /* same */

/* Alternate paragraph colours in CSS */
p:nth-child(4n+1) { color: navy; }
p:nth-child(4n+2) { color: green; }
p:nth-child(4n+3) { color: maroon; }
p:nth-child(4n+4) { color: purple; }

When a=0, no repeating is used, so for example :nth-child(0n+5) matches only the fifth child. When a=0, the a part need not be included, so the syntax simplifies to :nth-child(b) and the last example simplifies to :nth-child(5).

foo:nth-child(0n+1)   /* represents an element foo, first child of its parent element */
foo:nth-child(1)      /* same */

When a=1, the number may be omitted from the rule, so the following examples are equivalent:

bar:nth-child(1n+0)   /* represents all bar elements, specificity (0,1,1) */
bar:nth-child(n+0)    /* same */
bar:nth-child(n)      /* same */
bar                   /* same but lower specificity (0,0,1) */

If b=0, then every a-th element is picked:

tr:nth-child(2n) /* represents every even row of a HTML table */

If both a and b are equal to zero, the pseudo-class represents no element in the document tree.

The value a can be negative, but only the positive values of an+b, for n>= 0, may represent an element in the document tree, of course:

html|tr:nth-child(-n+6)  /* represents the 6 first rows of XHTML tables */
:nth-last-child() pseudo-class

The :nth-last-child(an+b) pseudo-class notation represents an element that has an+b-1 siblings after it in the document tree, for a given positive integer or zero value of n. See :nth-child() pseudo-class for the syntax of its argument. It also accepts the 'even' and 'odd' values for argument.

Examples:
tr:nth-last-child(-n+2)    /* represents the two last rows of a HTML table */

foo:nth-last-child(odd)    /* represents all odd foo elements in their parent element,
                              counting from the last one */
:nth-of-type() pseudo-class

The :nth-of-type(an+b) pseudo-class notation represents an element that has an+b-1 siblings with the same element name before it in the document tree, for a given zero or positive integer value of n. In other words, this matches the bth child of that type after all the children of that type have been split into groups of a elements each. See :nth-child() pseudo-class for the syntax of its argument. It also accepts the 'even' and 'odd' values for argument.

For example, this allows in CSS to alternate the position of floated images:
img:nth-of-type(2n+1) { float: right; }
img:nth-of-type(2n) { float: left; }
:nth-last-of-type() pseudo-class

The :nth-last-of-type(an+b) pseudo-class notation represents an element that has an+b-1 siblings with the same element name after it in the document tree, for a given zero or positive integer value of n. See :nth-child() pseudo-class for the syntax of its argument. It also accepts the 'even' and 'odd' values for argument.

For example, to represent all h2 children of a XHTML body except the first and last, one would use the following selector:
body > h2:nth-of-type(n+2):nth-last-of-type(n+2)

In this case, one could also use :not(), although the selector ends up being just as long:

body > h2:not(:first-of-type):not(:last-of-type) 
:first-child pseudo-class

Same as :nth-child(1). The :first-child pseudo-class represents an element that is the first child of some other element.

Examples:

In the following example, the selector represents a p element that is the first child of a div element:

div > p:first-child
This selector can represent the p inside the div of the following fragment:
<p> The last P before the note.</p>
<div class="note">
   <p> The first P inside the note.</p>
</div>
but cannot represent the second p in the following fragment:
<p> The last P before the note.</p>
<div class="note">
   <h2>Note</h2>
   <p> The first P inside the note.</p>
</div>
The following two selectors are equivalent:
* > a:first-child   /* a is first child of any element */
a:first-child       /* Same */
:last-child pseudo-class

Same as :nth-last-child(1).The :last-child pseudo-class represents an element that is the last child of some other element.

The following selector represents a list item li that is the last child of an ordered list ol.

Example:
ol > li:last-child
:first-of-type pseudo-class

Same as :nth-of-type(1).The :first-of-type pseudo-class represents an element that is the first sibling of its type in the list of children of its parent element.

Example:

The following selector represents a definition title dt inside a definition list dl, this dt being the first of its type in the list of children of its parent element.

dl dt:first-of-type
It is a valid description for the first two dt in the following example but not for the third one:
<dl><dt>gigogne</dt>
        <dd><dl><dt>fus&eacute;e</dt>
                    <dd>multistage rocket</dd>
                <dt>table</dt>
                    <dd>nest of tables</dd>
            </dl></dd>
</dl>
:last-of-type pseudo-class

Same as :nth-last-of-type(1).The :last-of-type pseudo-class represents an element that is the last sibling of its type in the list of children of its parent element.

Example:

The following selector represents the last data cell td of a table row.

tr > td:last-of-type
:only-child pseudo-class

Represents an element that has no siblings. Same as :first-child:last-child or :nth-child(1):nth-last-child(1), but with a lower specificity.

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
querySelectorAll
document.querySelectorAll(".selectorsreview tr")
pending…
matchesSelector
match.webkitMatchesSelector(".selectorsreview tr")
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment