c3c916f44de6297fb22b8ef68e59ef0c983ceb22.svn-base 114 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888
  1. <!DOCTYPE html PUBLIC '-//W3C//DTD HTML 4.01//EN' 'http://www.w3.org/TR/html4/strict.dtd'>
  2. <html lang="en">
  3. <head>
  4. <title>Selectors</title>
  5. <link href="default.css" type="text/css" rel="stylesheet">
  6. <link href="http://www.w3.org/StyleSheets/TR/W3C-WD.css" type="text/css" rel="stylesheet">
  7. <script src="jquery-1.2.3.pack.js"></script>
  8. <script language="JavaScript" type="text/JavaScript">
  9. $(document).ready(function(){
  10. console.log($('a').size());
  11. /*
  12. $('div,p,a').each(
  13. function(idx) {
  14. console.log(this.href);
  15. }
  16. );
  17. */
  18. });
  19. </script>
  20. </head>
  21. <body>
  22. <div class="head">
  23. <p id="p1"><a id="a1" href="http://www.w3.org/"><img height=48 alt=W3C src="http://www.w3.org/Icons/w3c_home" width=72></a>
  24. <h1 id="title">Selectors</h1>
  25. <h2>W3C Working Draft 15 December 2005</h2>
  26. <dl>
  27. <dt>This version:
  28. <dd><a id="a2" href="http://www.w3.org/TR/2005/WD-css3-selectors-20051215">
  29. http://www.w3.org/TR/2005/WD-css3-selectors-20051215</a>
  30. <dt>Latest version:
  31. <dd><a id="a3" href="http://www.w3.org/TR/css3-selectors">
  32. http://www.w3.org/TR/css3-selectors</a>
  33. <dt>Previous version:
  34. <dd><a id="a4" href="http://www.w3.org/TR/2001/CR-css3-selectors-20011113">
  35. http://www.w3.org/TR/2001/CR-css3-selectors-20011113</a>
  36. <dt><a id="a5" name=editors-list></a>Editors:
  37. <dd class="vcard"><span class="fn">Daniel Glazman</span> (Invited Expert)</dd>
  38. <dd class="vcard"><a id="a6" lang="tr" class="url fn" href="http://www.tantek.com/">Tantek &Ccedil;elik</a> (Invited Expert)
  39. <dd class="vcard"><a id="a7" href="mailto:ian@hixie.ch" class="url fn">Ian Hickson</a> (<span
  40. class="company"><a id="a8" href="http://www.google.com/">Google</a></span>)
  41. <dd class="vcard"><span class="fn">Peter Linss</span> (former editor, <span class="company"><a
  42. id="a9" href="http://www.netscape.com/">Netscape/AOL</a></span>)
  43. <dd class="vcard"><span class="fn">John Williams</span> (former editor, <span class="company"><a
  44. id="a10" href="http://www.quark.com/">Quark, Inc.</a></span>)
  45. </dl>
  46. <p id="p2" class="copyright"><a
  47. id="a11" href="http://www.w3.org/Consortium/Legal/ipr-notice#Copyright">
  48. Copyright</a> &copy; 2005 <a id="a12" href="http://www.w3.org/"><abbr
  49. title="World Wide Web Consortium">W3C</abbr></a><sup>&reg;</sup>
  50. (<a id="a13" href="http://www.csail.mit.edu/"><abbr title="Massachusetts
  51. Institute of Technology">MIT</abbr></a>, <a
  52. href="http://www.ercim.org/"><acronym title="European Research
  53. Consortium for Informatics and Mathematics">ERCIM</acronym></a>, <a
  54. href="http://www.keio.ac.jp/">Keio</a>), All Rights Reserved. W3C
  55. <a
  56. href="http://www.w3.org/Consortium/Legal/ipr-notice#Legal_Disclaimer">liability</a>,
  57. <a
  58. href="http://www.w3.org/Consortium/Legal/ipr-notice#W3C_Trademarks">trademark</a>,
  59. <a
  60. href="http://www.w3.org/Consortium/Legal/copyright-documents">document
  61. use</a> rules apply.
  62. <hr title="Separator for header">
  63. </div>
  64. <h2><a name=abstract></a>Abstract</h2>
  65. <p id="p3"><em>Selectors</em> are patterns that match against elements in a
  66. tree. Selectors have been optimized for use with HTML and XML, and
  67. are designed to be usable in performance-critical code.</p>
  68. <p id="p4"><acronym title="Cascading Style Sheets">CSS</acronym> (Cascading
  69. Style Sheets) is a language for describing the rendering of <acronym
  70. title="Hypertext Markup Language">HTML</acronym> and <acronym
  71. title="Extensible Markup Language">XML</acronym> documents on
  72. screen, on paper, in speech, etc. CSS uses Selectors for binding
  73. style properties to elements in the document. This document
  74. describes extensions to the selectors defined in CSS level 2. These
  75. extended selectors will be used by CSS level 3.
  76. <p id="p5">Selectors define the following function:</p>
  77. <pre>expression &#x2217; element &rarr; boolean</pre>
  78. <p id="p6">That is, given an element and a selector, this specification
  79. defines whether that element matches the selector.</p>
  80. <p>These expressions can also be used, for instance, to select a set
  81. of elements, or a single element from a set of elements, by
  82. evaluating the expression across all the elements in a
  83. subtree. <acronym title="Simple Tree Transformation
  84. Sheets">STTS</acronym> (Simple Tree Transformation Sheets), a
  85. language for transforming XML trees, uses this mechanism. <a href="#refsSTTS">[STTS]</a></p>
  86. <h2><a name=status></a>Status of this document</h2>
  87. <p><em>This section describes the status of this document at the
  88. time of its publication. Other documents may supersede this
  89. document. A list of current W3C publications and the latest revision
  90. of this technical report can be found in the <a
  91. href="http://www.w3.org/TR/">W3C technical reports index at
  92. http://www.w3.org/TR/.</a></em></p>
  93. <p>This document describes the selectors that already exist in <a
  94. href="#refsCSS1"><abbr title="CSS level 1">CSS1</abbr></a> and <a
  95. href="#refsCSS21"><abbr title="CSS level 2">CSS2</abbr></a>, and
  96. also proposes new selectors for <abbr title="CSS level
  97. 3">CSS3</abbr> and other languages that may need them.</p>
  98. <p>The CSS Working Group doesn't expect that all implementations of
  99. CSS3 will have to implement all selectors. Instead, there will
  100. probably be a small number of variants of CSS3, called profiles. For
  101. example, it may be that only a profile for interactive user agents
  102. will include all of the selectors.</p>
  103. <p>This specification is a last call working draft for the the <a
  104. href="http://www.w3.org/Style/CSS/members">CSS Working Group</a>
  105. (<a href="/Style/">Style Activity</a>). This
  106. document is a revision of the <a
  107. href="http://www.w3.org/TR/2001/CR-css3-selectors-20011113/">Candidate
  108. Recommendation dated 2001 November 13</a>, and has incorporated
  109. implementation feedback received in the past few years. It is
  110. expected that this last call will proceed straight to Proposed
  111. Recommendation stage since it is believed that interoperability will
  112. be demonstrable.</p>
  113. <p>All persons are encouraged to review and implement this
  114. specification and return comments to the (<a
  115. href="http://lists.w3.org/Archives/Public/www-style/">archived</a>)
  116. public mailing list <a
  117. href="http://www.w3.org/Mail/Lists.html#www-style">www-style</a>
  118. (see <a href="http://www.w3.org/Mail/Request">instructions</a>). W3C
  119. Members can also send comments directly to the CSS Working
  120. Group.
  121. The deadline for comments is 14 January 2006.</p>
  122. <p>This is still a draft document and may be updated, replaced, or
  123. obsoleted by other documents at any time. It is inappropriate to
  124. cite a W3C Working Draft as other than &quot;work in progress&quot;.
  125. <p>This document may be available in <a
  126. href="http://www.w3.org/Style/css3-selectors-updates/translations">translation</a>.
  127. The English version of this specification is the only normative
  128. version.
  129. <div class="subtoc">
  130. <h2><a name=contents>Table of contents</a></h2>
  131. <ul class="toc">
  132. <li class="tocline2"><a href="#context">1. Introduction</a>
  133. <ul>
  134. <li><a href="#dependencies">1.1. Dependencies</a> </li>
  135. <li><a href="#terminology">1.2. Terminology</a> </li>
  136. <li><a href="#changesFromCSS2">1.3. Changes from CSS2</a> </li>
  137. </ul>
  138. <li class="tocline2"><a href="#selectors">2. Selectors</a>
  139. <li class="tocline2"><a href="#casesens">3. Case sensitivity</a>
  140. <li class="tocline2"><a href="#selector-syntax">4. Selector syntax</a>
  141. <li class="tocline2"><a href="#grouping">5. Groups of selectors</a>
  142. <li class="tocline2"><a href="#simple-selectors">6. Simple selectors</a>
  143. <ul class="toc">
  144. <li class="tocline3"><a href="#type-selectors">6.1. Type selectors</a>
  145. <ul class="toc">
  146. <li class="tocline4"><a href="#typenmsp">6.1.1. Type selectors and namespaces</a></li>
  147. </ul>
  148. <li class="tocline3"><a href="#universal-selector">6.2. Universal selector</a>
  149. <ul>
  150. <li><a href="#univnmsp">6.2.1. Universal selector and namespaces</a></li>
  151. </ul>
  152. <li class="tocline3"><a href="#attribute-selectors">6.3. Attribute selectors</a>
  153. <ul class="toc">
  154. <li class="tocline4"><a href="#attribute-representation">6.3.1. Representation of attributes and attributes values</a>
  155. <li><a href="#attribute-substrings">6.3.2. Substring matching attribute selectors</a>
  156. <li class="tocline4"><a href="#attrnmsp">6.3.3. Attribute selectors and namespaces</a>
  157. <li class="tocline4"><a href="#def-values">6.3.4. Default attribute values in DTDs</a></li>
  158. </ul>
  159. <li class="tocline3"><a href="#class-html">6.4. Class selectors</a>
  160. <li class="tocline3"><a href="#id-selectors">6.5. ID selectors</a>
  161. <li class="tocline3"><a href="#pseudo-classes">6.6. Pseudo-classes</a>
  162. <ul class="toc">
  163. <li class="tocline4"><a href="#dynamic-pseudos">6.6.1. Dynamic pseudo-classes</a>
  164. <li class="tocline4"><a href="#target-pseudo">6.6.2. The :target pseudo-class</a>
  165. <li class="tocline4"><a href="#lang-pseudo">6.6.3. The :lang() pseudo-class</a>
  166. <li class="tocline4"><a href="#UIstates">6.6.4. UI element states pseudo-classes</a>
  167. <li class="tocline4"><a href="#structural-pseudos">6.6.5. Structural pseudo-classes</a>
  168. <ul>
  169. <li><a href="#root-pseudo">:root pseudo-class</a>
  170. <li><a href="#nth-child-pseudo">:nth-child() pseudo-class</a>
  171. <li><a href="#nth-last-child-pseudo">:nth-last-child()</a>
  172. <li><a href="#nth-of-type-pseudo">:nth-of-type() pseudo-class</a>
  173. <li><a href="#nth-last-of-type-pseudo">:nth-last-of-type()</a>
  174. <li><a href="#first-child-pseudo">:first-child pseudo-class</a>
  175. <li><a href="#last-child-pseudo">:last-child pseudo-class</a>
  176. <li><a href="#first-of-type-pseudo">:first-of-type pseudo-class</a>
  177. <li><a href="#last-of-type-pseudo">:last-of-type pseudo-class</a>
  178. <li><a href="#only-child-pseudo">:only-child pseudo-class</a>
  179. <li><a href="#only-of-type-pseudo">:only-of-type pseudo-class</a>
  180. <li><a href="#empty-pseudo">:empty pseudo-class</a></li>
  181. </ul>
  182. <li class="tocline4"><a href="#negation">6.6.7. The negation pseudo-class</a></li>
  183. </ul>
  184. </li>
  185. </ul>
  186. <li><a href="#pseudo-elements">7. Pseudo-elements</a>
  187. <ul>
  188. <li><a href="#first-line">7.1. The ::first-line pseudo-element</a>
  189. <li><a href="#first-letter">7.2. The ::first-letter pseudo-element</a>
  190. <li><a href="#UIfragments">7.3. The ::selection pseudo-element</a>
  191. <li><a href="#gen-content">7.4. The ::before and ::after pseudo-elements</a></li>
  192. </ul>
  193. <li class="tocline2"><a href="#combinators">8. Combinators</a>
  194. <ul class="toc">
  195. <li class="tocline3"><a href="#descendant-combinators">8.1. Descendant combinators</a>
  196. <li class="tocline3"><a href="#child-combinators">8.2. Child combinators</a>
  197. <li class="tocline3"><a href="#sibling-combinators">8.3. Sibling combinators</a>
  198. <ul class="toc">
  199. <li class="tocline4"><a href="#adjacent-sibling-combinators">8.3.1. Adjacent sibling combinator</a>
  200. <li class="tocline4"><a href="#general-sibling-combinators">8.3.2. General sibling combinator</a></li>
  201. </ul>
  202. </li>
  203. </ul>
  204. <li class="tocline2"><a href="#specificity">9. Calculating a selector's specificity</a>
  205. <li class="tocline2"><a href="#w3cselgrammar">10. The grammar of Selectors</a>
  206. <ul class="toc">
  207. <li class="tocline3"><a href="#grammar">10.1. Grammar</a>
  208. <li class="tocline3"><a href="#lex">10.2. Lexical scanner</a></li>
  209. </ul>
  210. <li class="tocline2"><a href="#downlevel">11. Namespaces and down-level clients</a>
  211. <li class="tocline2"><a href="#profiling">12. Profiles</a>
  212. <li><a href="#Conformance">13. Conformance and requirements</a>
  213. <li><a href="#Tests">14. Tests</a>
  214. <li><a href="#ACKS">15. Acknowledgements</a>
  215. <li class="tocline2"><a href="#references">16. References</a>
  216. </ul>
  217. </div>
  218. <h2><a name=context>1. Introduction</a></h2>
  219. <h3><a name=dependencies></a>1.1. Dependencies</h3>
  220. <p>Some features of this specification are specific to CSS, or have
  221. particular limitations or rules specific to CSS. In this
  222. specification, these have been described in terms of CSS2.1. <a
  223. href="#refsCSS21">[CSS21]</a></p>
  224. <h3><a name=terminology></a>1.2. Terminology</h3>
  225. <p>All of the text of this specification is normative except
  226. examples, notes, and sections explicitly marked as
  227. non-normative.</p>
  228. <h3><a name=changesFromCSS2></a>1.3. Changes from CSS2</h3>
  229. <p><em>This section is non-normative.</em></p>
  230. <p>The main differences between the selectors in CSS2 and those in
  231. Selectors are:
  232. <ul>
  233. <li>the list of basic definitions (selector, group of selectors,
  234. simple selector, etc.) has been changed; in particular, what was
  235. referred to in CSS2 as a simple selector is now called a sequence
  236. of simple selectors, and the term "simple selector" is now used for
  237. the components of this sequence</li>
  238. <li>an optional namespace component is now allowed in type element
  239. selectors, the universal selector and attribute selectors</li>
  240. <li>a <a href="#general-sibling-combinators">new combinator</a> has been introduced</li>
  241. <li>new simple selectors including substring matching attribute
  242. selectors, and new pseudo-classes</li>
  243. <li>new pseudo-elements, and introduction of the "::" convention
  244. for pseudo-elements</li>
  245. <li>the grammar has been rewritten</li>
  246. <li>profiles to be added to specifications integrating Selectors
  247. and defining the set of selectors which is actually supported by
  248. each specification</li>
  249. <li>Selectors are now a CSS3 Module and an independent
  250. specification; other specifications can now refer to this document
  251. independently of CSS</li>
  252. <li>the specification now has its own test suite</li>
  253. </ul>
  254. <h2><a name=selectors></a>2. Selectors</h2>
  255. <p><em>This section is non-normative, as it merely summarizes the
  256. following sections.</em></p>
  257. <p>A Selector represents a structure. This structure can be used as a
  258. condition (e.g. in a CSS rule) that determines which elements a
  259. selector matches in the document tree, or as a flat description of the
  260. HTML or XML fragment corresponding to that structure.</p>
  261. <p>Selectors may range from simple element names to rich contextual
  262. representations.</p>
  263. <p>The following table summarizes the Selector syntax:</p>
  264. <table class="selectorsReview">
  265. <thead>
  266. <tr>
  267. <th class="pattern">Pattern</th>
  268. <th class="meaning">Meaning</th>
  269. <th class="described">Described in section</th>
  270. <th class="origin">First defined in CSS level</th></tr>
  271. <tbody>
  272. <tr>
  273. <td class="pattern">*</td>
  274. <td class="meaning">any element</td>
  275. <td class="described"><a
  276. href="#universal-selector">Universal
  277. selector</a></td>
  278. <td class="origin">2</td></tr>
  279. <tr>
  280. <td class="pattern">E</td>
  281. <td class="meaning">an element of type E</td>
  282. <td class="described"><a
  283. href="#type-selectors">Type selector</a></td>
  284. <td class="origin">1</td></tr>
  285. <tr>
  286. <td class="pattern">E[foo]</td>
  287. <td class="meaning">an E element with a "foo" attribute</td>
  288. <td class="described"><a
  289. href="#attribute-selectors">Attribute
  290. selectors</a></td>
  291. <td class="origin">2</td></tr>
  292. <tr>
  293. <td class="pattern">E[foo="bar"]</td>
  294. <td class="meaning">an E element whose "foo" attribute value is exactly
  295. equal to "bar"</td>
  296. <td class="described"><a
  297. href="#attribute-selectors">Attribute
  298. selectors</a></td>
  299. <td class="origin">2</td></tr>
  300. <tr>
  301. <td class="pattern">E[foo~="bar"]</td>
  302. <td class="meaning">an E element whose "foo" attribute value is a list of
  303. space-separated values, one of which is exactly equal to "bar"</td>
  304. <td class="described"><a
  305. href="#attribute-selectors">Attribute
  306. selectors</a></td>
  307. <td class="origin">2</td></tr>
  308. <tr>
  309. <td class="pattern">E[foo^="bar"]</td>
  310. <td class="meaning">an E element whose "foo" attribute value begins exactly
  311. with the string "bar"</td>
  312. <td class="described"><a
  313. href="#attribute-selectors">Attribute
  314. selectors</a></td>
  315. <td class="origin">3</td></tr>
  316. <tr>
  317. <td class="pattern">E[foo$="bar"]</td>
  318. <td class="meaning">an E element whose "foo" attribute value ends exactly
  319. with the string "bar"</td>
  320. <td class="described"><a
  321. href="#attribute-selectors">Attribute
  322. selectors</a></td>
  323. <td class="origin">3</td></tr>
  324. <tr>
  325. <td class="pattern">E[foo*="bar"]</td>
  326. <td class="meaning">an E element whose "foo" attribute value contains the
  327. substring "bar"</td>
  328. <td class="described"><a
  329. href="#attribute-selectors">Attribute
  330. selectors</a></td>
  331. <td class="origin">3</td></tr>
  332. <tr>
  333. <td class="pattern">E[hreflang|="en"]</td>
  334. <td class="meaning">an E element whose "hreflang" attribute has a hyphen-separated
  335. list of values beginning (from the left) with "en"</td>
  336. <td class="described"><a
  337. href="#attribute-selectors">Attribute
  338. selectors</a></td>
  339. <td class="origin">2</td></tr>
  340. <tr>
  341. <td class="pattern">E:root</td>
  342. <td class="meaning">an E element, root of the document</td>
  343. <td class="described"><a
  344. href="#structural-pseudos">Structural
  345. pseudo-classes</a></td>
  346. <td class="origin">3</td></tr>
  347. <tr>
  348. <td class="pattern">E:nth-child(n)</td>
  349. <td class="meaning">an E element, the n-th child of its parent</td>
  350. <td class="described"><a
  351. href="#structural-pseudos">Structural
  352. pseudo-classes</a></td>
  353. <td class="origin">3</td></tr>
  354. <tr>
  355. <td class="pattern">E:nth-last-child(n)</td>
  356. <td class="meaning">an E element, the n-th child of its parent, counting
  357. from the last one</td>
  358. <td class="described"><a
  359. href="#structural-pseudos">Structural
  360. pseudo-classes</a></td>
  361. <td class="origin">3</td></tr>
  362. <tr>
  363. <td class="pattern">E:nth-of-type(n)</td>
  364. <td class="meaning">an E element, the n-th sibling of its type</td>
  365. <td class="described"><a
  366. href="#structural-pseudos">Structural
  367. pseudo-classes</a></td>
  368. <td class="origin">3</td></tr>
  369. <tr>
  370. <td class="pattern">E:nth-last-of-type(n)</td>
  371. <td class="meaning">an E element, the n-th sibling of its type, counting
  372. from the last one</td>
  373. <td class="described"><a
  374. href="#structural-pseudos">Structural
  375. pseudo-classes</a></td>
  376. <td class="origin">3</td></tr>
  377. <tr>
  378. <td class="pattern">E:first-child</td>
  379. <td class="meaning">an E element, first child of its parent</td>
  380. <td class="described"><a
  381. href="#structural-pseudos">Structural
  382. pseudo-classes</a></td>
  383. <td class="origin">2</td></tr>
  384. <tr>
  385. <td class="pattern">E:last-child</td>
  386. <td class="meaning">an E element, last child of its parent</td>
  387. <td class="described"><a
  388. href="#structural-pseudos">Structural
  389. pseudo-classes</a></td>
  390. <td class="origin">3</td></tr>
  391. <tr>
  392. <td class="pattern">E:first-of-type</td>
  393. <td class="meaning">an E element, first sibling of its type</td>
  394. <td class="described"><a
  395. href="#structural-pseudos">Structural
  396. pseudo-classes</a></td>
  397. <td class="origin">3</td></tr>
  398. <tr>
  399. <td class="pattern">E:last-of-type</td>
  400. <td class="meaning">an E element, last sibling of its type</td>
  401. <td class="described"><a
  402. href="#structural-pseudos">Structural
  403. pseudo-classes</a></td>
  404. <td class="origin">3</td></tr>
  405. <tr>
  406. <td class="pattern">E:only-child</td>
  407. <td class="meaning">an E element, only child of its parent</td>
  408. <td class="described"><a
  409. href="#structural-pseudos">Structural
  410. pseudo-classes</a></td>
  411. <td class="origin">3</td></tr>
  412. <tr>
  413. <td class="pattern">E:only-of-type</td>
  414. <td class="meaning">an E element, only sibling of its type</td>
  415. <td class="described"><a
  416. href="#structural-pseudos">Structural
  417. pseudo-classes</a></td>
  418. <td class="origin">3</td></tr>
  419. <tr>
  420. <td class="pattern">E:empty</td>
  421. <td class="meaning">an E element that has no children (including text
  422. nodes)</td>
  423. <td class="described"><a
  424. href="#structural-pseudos">Structural
  425. pseudo-classes</a></td>
  426. <td class="origin">3</td></tr>
  427. <tr>
  428. <td class="pattern">E:link<br>E:visited</td>
  429. <td class="meaning">an E element being the source anchor of a hyperlink of
  430. which the target is not yet visited (:link) or already visited
  431. (:visited)</td>
  432. <td class="described"><a
  433. href="#link">The link
  434. pseudo-classes</a></td>
  435. <td class="origin">1</td></tr>
  436. <tr>
  437. <td class="pattern">E:active<br>E:hover<br>E:focus</td>
  438. <td class="meaning">an E element during certain user actions</td>
  439. <td class="described"><a
  440. href="#useraction-pseudos">The user
  441. action pseudo-classes</a></td>
  442. <td class="origin">1 and 2</td></tr>
  443. <tr>
  444. <td class="pattern">E:target</td>
  445. <td class="meaning">an E element being the target of the referring URI</td>
  446. <td class="described"><a
  447. href="#target-pseudo">The target
  448. pseudo-class</a></td>
  449. <td class="origin">3</td></tr>
  450. <tr>
  451. <td class="pattern">E:lang(fr)</td>
  452. <td class="meaning">an element of type E in language "fr" (the document
  453. language specifies how language is determined)</td>
  454. <td class="described"><a
  455. href="#lang-pseudo">The :lang()
  456. pseudo-class</a></td>
  457. <td class="origin">2</td></tr>
  458. <tr>
  459. <td class="pattern">E:enabled<br>E:disabled</td>
  460. <td class="meaning">a user interface element E which is enabled or
  461. disabled</td>
  462. <td class="described"><a
  463. href="#UIstates">The UI element states
  464. pseudo-classes</a></td>
  465. <td class="origin">3</td></tr>
  466. <tr>
  467. <td class="pattern">E:checked<!--<br>E:indeterminate--></td>
  468. <td class="meaning">a user interface element E which is checked<!-- or in an
  469. indeterminate state--> (for instance a radio-button or checkbox)</td>
  470. <td class="described"><a
  471. href="#UIstates">The UI element states
  472. pseudo-classes</a></td>
  473. <td class="origin">3</td></tr>
  474. <tr>
  475. <td class="pattern">E::first-line</td>
  476. <td class="meaning">the first formatted line of an E element</td>
  477. <td class="described"><a
  478. href="#first-line">The ::first-line
  479. pseudo-element</a></td>
  480. <td class="origin">1</td></tr>
  481. <tr>
  482. <td class="pattern">E::first-letter</td>
  483. <td class="meaning">the first formatted letter of an E element</td>
  484. <td class="described"><a
  485. href="#first-letter">The ::first-letter
  486. pseudo-element</a></td>
  487. <td class="origin">1</td></tr>
  488. <tr>
  489. <td class="pattern">E::selection</td>
  490. <td class="meaning">the portion of an E element that is currently
  491. selected/highlighted by the user</td>
  492. <td class="described"><a
  493. href="#UIfragments">The UI element
  494. fragments pseudo-elements</a></td>
  495. <td class="origin">3</td></tr>
  496. <tr>
  497. <td class="pattern">E::before</td>
  498. <td class="meaning">generated content before an E element</td>
  499. <td class="described"><a
  500. href="#gen-content">The ::before
  501. pseudo-element</a></td>
  502. <td class="origin">2</td></tr>
  503. <tr>
  504. <td class="pattern">E::after</td>
  505. <td class="meaning">generated content after an E element</td>
  506. <td class="described"><a
  507. href="#gen-content">The ::after
  508. pseudo-element</a></td>
  509. <td class="origin">2</td></tr>
  510. <tr>
  511. <td class="pattern">E.warning</td>
  512. <td class="meaning">an E element whose class is
  513. "warning" (the document language specifies how class is determined).</td>
  514. <td class="described"><a
  515. href="#class-html">Class
  516. selectors</a></td>
  517. <td class="origin">1</td></tr>
  518. <tr>
  519. <td class="pattern">E#myid</td>
  520. <td class="meaning">an E element with ID equal to "myid".</td>
  521. <td class="described"><a
  522. href="#id-selectors">ID
  523. selectors</a></td>
  524. <td class="origin">1</td></tr>
  525. <tr>
  526. <td class="pattern">E:not(s)</td>
  527. <td class="meaning">an E element that does not match simple selector s</td>
  528. <td class="described"><a
  529. href="#negation">Negation
  530. pseudo-class</a></td>
  531. <td class="origin">3</td></tr>
  532. <tr>
  533. <td class="pattern">E F</td>
  534. <td class="meaning">an F element descendant of an E element</td>
  535. <td class="described"><a
  536. href="#descendant-combinators">Descendant
  537. combinator</a></td>
  538. <td class="origin">1</td></tr>
  539. <tr>
  540. <td class="pattern">E &gt; F</td>
  541. <td class="meaning">an F element child of an E element</td>
  542. <td class="described"><a
  543. href="#child-combinators">Child
  544. combinator</a></td>
  545. <td class="origin">2</td></tr>
  546. <tr>
  547. <td class="pattern">E + F</td>
  548. <td class="meaning">an F element immediately preceded by an E element</td>
  549. <td class="described"><a
  550. href="#adjacent-sibling-combinators">Adjacent sibling combinator</a></td>
  551. <td class="origin">2</td></tr>
  552. <tr>
  553. <td class="pattern">E ~ F</td>
  554. <td class="meaning">an F element preceded by an E element</td>
  555. <td class="described"><a
  556. href="#general-sibling-combinators">General sibling combinator</a></td>
  557. <td class="origin">3</td></tr></tbody></table>
  558. <p>The meaning of each selector is derived from the table above by
  559. prepending "matches" to the contents of each cell in the "Meaning"
  560. column.</p>
  561. <h2><a name=casesens>3. Case sensitivity</a></h2>
  562. <p>The case sensitivity of document language element names, attribute
  563. names, and attribute values in selectors depends on the document
  564. language. For example, in HTML, element names are case-insensitive,
  565. but in XML, they are case-sensitive.</p>
  566. <h2><a name=selector-syntax>4. Selector syntax</a></h2>
  567. <p>A <dfn><a name=selector>selector</a></dfn> is a chain of one
  568. or more <a href="#sequence">sequences of simple selectors</a>
  569. separated by <a href="#combinators">combinators</a>.</p>
  570. <p>A <dfn><a name=sequence>sequence of simple selectors</a></dfn>
  571. is a chain of <a href="#simple-selectors-dfn">simple selectors</a>
  572. that are not separated by a <a href="#combinators">combinator</a>. It
  573. always begins with a <a href="#type-selectors">type selector</a> or a
  574. <a href="#universal-selector">universal selector</a>. No other type
  575. selector or universal selector is allowed in the sequence.</p>
  576. <p>A <dfn><a name=simple-selectors-dfn></a><a
  577. href="#simple-selectors">simple selector</a></dfn> is either a <a
  578. href="#type-selectors">type selector</a>, <a
  579. href="#universal-selector">universal selector</a>, <a
  580. href="#attribute-selectors">attribute selector</a>, <a
  581. href="#class-html">class selector</a>, <a
  582. href="#id-selectors">ID selector</a>, <a
  583. href="#content-selectors">content selector</a>, or <a
  584. href="#pseudo-classes">pseudo-class</a>. One <a
  585. href="#pseudo-elements">pseudo-element</a> may be appended to the last
  586. sequence of simple selectors.</p>
  587. <p><dfn>Combinators</dfn> are: white space, &quot;greater-than
  588. sign&quot; (U+003E, <code>&gt;</code>), &quot;plus sign&quot; (U+002B,
  589. <code>+</code>) and &quot;tilde&quot; (U+007E, <code>~</code>). White
  590. space may appear between a combinator and the simple selectors around
  591. it. <a name=whitespace></a>Only the characters "space" (U+0020), "tab"
  592. (U+0009), "line feed" (U+000A), "carriage return" (U+000D), and "form
  593. feed" (U+000C) can occur in white space. Other space-like characters,
  594. such as "em-space" (U+2003) and "ideographic space" (U+3000), are
  595. never part of white space.</p>
  596. <p>The elements of a document tree that are represented by a selector
  597. are the <dfn><a name=subject></a>subjects of the selector</dfn>. A
  598. selector consisting of a single sequence of simple selectors
  599. represents any element satisfying its requirements. Prepending another
  600. sequence of simple selectors and a combinator to a sequence imposes
  601. additional matching constraints, so the subjects of a selector are
  602. always a subset of the elements represented by the last sequence of
  603. simple selectors.</p>
  604. <p>An empty selector, containing no sequence of simple selectors and
  605. no pseudo-element, is an <a href="#Conformance">invalid
  606. selector</a>.</p>
  607. <h2><a name=grouping>5. Groups of selectors</a></h2>
  608. <p>When several selectors share the same declarations, they may be
  609. grouped into a comma-separated list. (A comma is U+002C.)</p>
  610. <div class="example">
  611. <p>CSS examples:</p>
  612. <p>In this example, we condense three rules with identical
  613. declarations into one. Thus,</p>
  614. <pre>h1 { font-family: sans-serif }
  615. h2 { font-family: sans-serif }
  616. h3 { font-family: sans-serif }</pre>
  617. <p>is equivalent to:</p>
  618. <pre>h1, h2, h3 { font-family: sans-serif }</pre>
  619. </div>
  620. <p><strong>Warning</strong>: the equivalence is true in this example
  621. because all the selectors are valid selectors. If just one of these
  622. selectors were invalid, the entire group of selectors would be
  623. invalid. This would invalidate the rule for all three heading
  624. elements, whereas in the former case only one of the three individual
  625. heading rules would be invalidated.</p>
  626. <h2><a name=simple-selectors>6. Simple selectors</a></h2>
  627. <h3><a name=type-selectors>6.1. Type selector</a></h3>
  628. <p>A <dfn>type selector</dfn> is the name of a document language
  629. element type. A type selector represents an instance of the element
  630. type in the document tree.</p>
  631. <div class="example">
  632. <p>Example:</p>
  633. <p>The following selector represents an <code>h1</code> element in the document tree:</p>
  634. <pre>h1</pre>
  635. </div>
  636. <h4><a name=typenmsp>6.1.1. Type selectors and namespaces</a></h4>
  637. <p>Type selectors allow an optional namespace (<a
  638. href="#refsXMLNAMES">[XMLNAMES]</a>) component. A namespace prefix
  639. that has been previously declared may be prepended to the element name
  640. separated by the namespace separator &quot;vertical bar&quot;
  641. (U+007C, <code>|</code>).</p>
  642. <p>The namespace component may be left empty to indicate that the
  643. selector is only to represent elements with no declared namespace.</p>
  644. <p>An asterisk may be used for the namespace prefix, indicating that
  645. the selector represents elements in any namespace (including elements
  646. with no namespace).</p>
  647. <p>Element type selectors that have no namespace component (no
  648. namespace separator), represent elements without regard to the
  649. element's namespace (equivalent to "<code>*|</code>") unless a default
  650. namespace has been declared. If a default namespace has been declared,
  651. the selector will represent only elements in the default
  652. namespace.</p>
  653. <p>A type selector containing a namespace prefix that has not been
  654. previously declared is an <a href="#Conformance">invalid</a> selector.
  655. The mechanism for declaring a namespace prefix is left up to the
  656. language implementing Selectors. In CSS, such a mechanism is defined
  657. in the General Syntax module.</p>
  658. <p>In a namespace-aware client, element type selectors will only match
  659. against the <a
  660. href="http://www.w3.org/TR/REC-xml-names/#NT-LocalPart">local part</a>
  661. of the element's <a
  662. href="http://www.w3.org/TR/REC-xml-names/#ns-qualnames">qualified
  663. name</a>. See <a href="#downlevel">below</a> for notes about matching
  664. behaviors in down-level clients.</p>
  665. <p>In summary:</p>
  666. <dl>
  667. <dt><code>ns|E</code></dt>
  668. <dd>elements with name E in namespace ns</dd>
  669. <dt><code>*|E</code></dt>
  670. <dd>elements with name E in any namespace, including those without any
  671. declared namespace</dd>
  672. <dt><code>|E</code></dt>
  673. <dd>elements with name E without any declared namespace</dd>
  674. <dt><code>E</code></dt>
  675. <dd>if no default namespace has been specified, this is equivalent to *|E.
  676. Otherwise it is equivalent to ns|E where ns is the default namespace.</dd>
  677. </dl>
  678. <div class="example">
  679. <p>CSS examples:</p>
  680. <pre>@namespace foo url(http://www.example.com);
  681. foo|h1 { color: blue }
  682. foo|* { color: yellow }
  683. |h1 { color: red }
  684. *|h1 { color: green }
  685. h1 { color: green }</pre>
  686. <p>The first rule will match only <code>h1</code> elements in the
  687. "http://www.example.com" namespace.</p>
  688. <p>The second rule will match all elements in the
  689. "http://www.example.com" namespace.</p>
  690. <p>The third rule will match only <code>h1</code> elements without
  691. any declared namespace.</p>
  692. <p>The fourth rule will match <code>h1</code> elements in any
  693. namespace (including those without any declared namespace).</p>
  694. <p>The last rule is equivalent to the fourth rule because no default
  695. namespace has been defined.</p>
  696. </div>
  697. <h3><a name=universal-selector>6.2. Universal selector</a> </h3>
  698. <p>The <dfn>universal selector</dfn>, written &quot;asterisk&quot;
  699. (<code>*</code>), represents the qualified name of any element
  700. type. It represents any single element in the document tree in any
  701. namespace (including those without any declared namespace) if no
  702. default namespace has been specified. If a default namespace has been
  703. specified, see <a href="#univnmsp">Universal selector and
  704. Namespaces</a> below.</p>
  705. <p>If the universal selector is not the only component of a sequence
  706. of simple selectors, the <code>*</code> may be omitted.</p>
  707. <div class="example">
  708. <p>Examples:</p>
  709. <ul>
  710. <li><code>*[hreflang|=en]</code> and <code>[hreflang|=en]</code> are equivalent,</li>
  711. <li><code>*.warning</code> and <code>.warning</code> are equivalent,</li>
  712. <li><code>*#myid</code> and <code>#myid</code> are equivalent.</li>
  713. </ul>
  714. </div>
  715. <p class="note"><strong>Note:</strong> it is recommended that the
  716. <code>*</code>, representing the universal selector, not be
  717. omitted.</p>
  718. <h4><a name=univnmsp>6.2.1. Universal selector and namespaces</a></h4>
  719. <p>The universal selector allows an optional namespace component. It
  720. is used as follows:</p>
  721. <dl>
  722. <dt><code>ns|*</code></dt>
  723. <dd>all elements in namespace ns</dd>
  724. <dt><code>*|*</code></dt>
  725. <dd>all elements</dd>
  726. <dt><code>|*</code></dt>
  727. <dd>all elements without any declared namespace</dd>
  728. <dt><code>*</code></dt>
  729. <dd>if no default namespace has been specified, this is equivalent to *|*.
  730. Otherwise it is equivalent to ns|* where ns is the default namespace.</dd>
  731. </dl>
  732. <p>A universal selector containing a namespace prefix that has not
  733. been previously declared is an <a href="#Conformance">invalid</a>
  734. selector. The mechanism for declaring a namespace prefix is left up
  735. to the language implementing Selectors. In CSS, such a mechanism is
  736. defined in the General Syntax module.</p>
  737. <h3><a name=attribute-selectors>6.3. Attribute selectors</a></h3>
  738. <p>Selectors allow the representation of an element's attributes. When
  739. a selector is used as an expression to match against an element,
  740. attribute selectors must be considered to match an element if that
  741. element has an attribute that matches the attribute represented by the
  742. attribute selector.</p>
  743. <h4><a name=attribute-representation>6.3.1. Attribute presence and values
  744. selectors</a></h4>
  745. <p>CSS2 introduced four attribute selectors:</p>
  746. <dl>
  747. <dt><code>[att]</code>
  748. <dd>Represents an element with the <code>att</code> attribute, whatever the value of
  749. the attribute.</dd>
  750. <dt><code>[att=val]</code></dt>
  751. <dd>Represents an element with the <code>att</code> attribute whose value is exactly
  752. "val".</dd>
  753. <dt><code>[att~=val]</code></dt>
  754. <dd>Represents an element with the <code>att</code> attribute whose value is a <a
  755. href="#whitespace">whitespace</a>-separated list of words, one of
  756. which is exactly "val". If "val" contains whitespace, it will never
  757. represent anything (since the words are <em>separated</em> by
  758. spaces).</dd>
  759. <dt><code>[att|=val]</code>
  760. <dd>Represents an element with the <code>att</code> attribute, its value either
  761. being exactly "val" or beginning with "val" immediately followed by
  762. "-" (U+002D). This is primarily intended to allow language subcode
  763. matches (e.g., the <code>hreflang</code> attribute on the
  764. <code>link</code> element in HTML) as described in RFC 3066 (<a
  765. href="#refsRFC3066">[RFC3066]</a>). For <code>lang</code> (or
  766. <code>xml:lang</code>) language subcode matching, please see <a
  767. href="#lang-pseudo">the <code>:lang</code> pseudo-class</a>.</dd>
  768. </dl>
  769. <p>Attribute values must be identifiers or strings. The
  770. case-sensitivity of attribute names and values in selectors depends on
  771. the document language.</p>
  772. <div class="example">
  773. <p>Examples:</p>
  774. <p>The following attribute selector represents an <code>h1</code>
  775. element that carries the <code>title</code> attribute, whatever its
  776. value:</p>
  777. <pre>h1[title]</pre>
  778. <p>In the following example, the selector represents a
  779. <code>span</code> element whose <code>class</code> attribute has
  780. exactly the value "example":</p>
  781. <pre>span[class="example"]</pre>
  782. <p>Multiple attribute selectors can be used to represent several
  783. attributes of an element, or several conditions on the same
  784. attribute. Here, the selector represents a <code>span</code> element
  785. whose <code>hello</code> attribute has exactly the value "Cleveland"
  786. and whose <code>goodbye</code> attribute has exactly the value
  787. "Columbus":</p>
  788. <pre>span[hello="Cleveland"][goodbye="Columbus"]</pre>
  789. <p>The following selectors illustrate the differences between "="
  790. and "~=". The first selector will represent, for example, the value
  791. "copyright copyleft copyeditor" on a <code>rel</code> attribute. The
  792. second selector will only represent an <code>a</code> element with
  793. an <code>href</code> attribute having the exact value
  794. "http://www.w3.org/".</p>
  795. <pre>a[rel~="copyright"]
  796. a[href="http://www.w3.org/"]</pre>
  797. <p>The following selector represents a <code>link</code> element
  798. whose <code>hreflang</code> attribute is exactly "fr".</p>
  799. <pre>link[hreflang=fr]</pre>
  800. <p>The following selector represents a <code>link</code> element for
  801. which the values of the <code>hreflang</code> attribute begins with
  802. "en", including "en", "en-US", and "en-cockney":</p>
  803. <pre>link[hreflang|="en"]</pre>
  804. <p>Similarly, the following selectors represents a
  805. <code>DIALOGUE</code> element whenever it has one of two different
  806. values for an attribute <code>character</code>:</p>
  807. <pre>DIALOGUE[character=romeo]
  808. DIALOGUE[character=juliet]</pre>
  809. </div>
  810. <h4><a name=attribute-substrings></a>6.3.2. Substring matching attribute
  811. selectors</h4>
  812. <p>Three additional attribute selectors are provided for matching
  813. substrings in the value of an attribute:</p>
  814. <dl>
  815. <dt><code>[att^=val]</code></dt>
  816. <dd>Represents an element with the <code>att</code> attribute whose value begins
  817. with the prefix "val".</dd>
  818. <dt><code>[att$=val]</code>
  819. <dd>Represents an element with the <code>att</code> attribute whose value ends with
  820. the suffix "val".</dd>
  821. <dt><code>[att*=val]</code>
  822. <dd>Represents an element with the <code>att</code> attribute whose value contains
  823. at least one instance of the substring "val".</dd>
  824. </dl>
  825. <p>Attribute values must be identifiers or strings. The
  826. case-sensitivity of attribute names in selectors depends on the
  827. document language.</p>
  828. <div class="example">
  829. <p>Examples:</p>
  830. <p>The following selector represents an HTML <code>object</code>, referencing an
  831. image:</p>
  832. <pre>object[type^="image/"]</pre>
  833. <p>The following selector represents an HTML anchor <code>a</code> with an
  834. <code>href</code> attribute whose value ends with ".html".</p>
  835. <pre>a[href$=".html"]</pre>
  836. <p>The following selector represents an HTML paragraph with a <code>title</code>
  837. attribute whose value contains the substring "hello"</p>
  838. <pre>p[title*="hello"]</pre>
  839. </div>
  840. <h4><a name=attrnmsp>6.3.3. Attribute selectors and namespaces</a></h4>
  841. <p>Attribute selectors allow an optional namespace component to the
  842. attribute name. A namespace prefix that has been previously declared
  843. may be prepended to the attribute name separated by the namespace
  844. separator &quot;vertical bar&quot; (<code>|</code>). In keeping with
  845. the Namespaces in the XML recommendation, default namespaces do not
  846. apply to attributes, therefore attribute selectors without a namespace
  847. component apply only to attributes that have no declared namespace
  848. (equivalent to "<code>|attr</code>"). An asterisk may be used for the
  849. namespace prefix indicating that the selector is to match all
  850. attribute names without regard to the attribute's namespace.
  851. <p>An attribute selector with an attribute name containing a namespace
  852. prefix that has not been previously declared is an <a
  853. href="#Conformance">invalid</a> selector. The mechanism for declaring
  854. a namespace prefix is left up to the language implementing Selectors.
  855. In CSS, such a mechanism is defined in the General Syntax module.
  856. <div class="example">
  857. <p>CSS examples:</p>
  858. <pre>@namespace foo "http://www.example.com";
  859. [foo|att=val] { color: blue }
  860. [*|att] { color: yellow }
  861. [|att] { color: green }
  862. [att] { color: green }</pre>
  863. <p>The first rule will match only elements with the attribute
  864. <code>att</code> in the "http://www.example.com" namespace with the
  865. value "val".</p>
  866. <p>The second rule will match only elements with the attribute
  867. <code>att</code> regardless of the namespace of the attribute
  868. (including no declared namespace).</p>
  869. <p>The last two rules are equivalent and will match only elements
  870. with the attribute <code>att</code> where the attribute is not
  871. declared to be in a namespace.</p>
  872. </div>
  873. <h4><a name=def-values>6.3.4. Default attribute values in DTDs</a></h4>
  874. <p>Attribute selectors represent explicitly set attribute values in
  875. the document tree. Default attribute values may be defined in a DTD or
  876. elsewhere, but cannot always be selected by attribute
  877. selectors. Selectors should be designed so that they work even if the
  878. default values are not included in the document tree.</p>
  879. <p>More precisely, a UA is <em>not</em> required to read an "external
  880. subset" of the DTD but <em>is</em> required to look for default
  881. attribute values in the document's "internal subset." (See <a
  882. href="#refsXML10">[XML10]</a> for definitions of these subsets.)</p>
  883. <p>A UA that recognizes an XML namespace <a
  884. href="#refsXMLNAMES">[XMLNAMES]</a> is not required to use its
  885. knowledge of that namespace to treat default attribute values as if
  886. they were present in the document. (For example, an XHTML UA is not
  887. required to use its built-in knowledge of the XHTML DTD.)</p>
  888. <p class="note"><strong>Note:</strong> Typically, implementations
  889. choose to ignore external subsets.</p>
  890. <div class="example">
  891. <p>Example:</p>
  892. <p>Consider an element EXAMPLE with an attribute "notation" that has a
  893. default value of "decimal". The DTD fragment might be</p>
  894. <pre class="dtd-example">&lt;!ATTLIST EXAMPLE notation (decimal,octal) "decimal"></pre>
  895. <p>If the style sheet contains the rules</p>
  896. <pre>EXAMPLE[notation=decimal] { /*... default property settings ...*/ }
  897. EXAMPLE[notation=octal] { /*... other settings...*/ }</pre>
  898. <p>the first rule will not match elements whose "notation" attribute
  899. is set by default, i.e. not set explicitly. To catch all cases, the
  900. attribute selector for the default value must be dropped:</p>
  901. <pre>EXAMPLE { /*... default property settings ...*/ }
  902. EXAMPLE[notation=octal] { /*... other settings...*/ }</pre>
  903. <p>Here, because the selector <code>EXAMPLE[notation=octal]</code> is
  904. more specific than the tag
  905. selector alone, the style declarations in the second rule will override
  906. those in the first for elements that have a "notation" attribute value
  907. of "octal". Care has to be taken that all property declarations that
  908. are to apply only to the default case are overridden in the non-default
  909. cases' style rules.</p>
  910. </div>
  911. <h3><a name=class-html>6.4. Class selectors</a></h3>
  912. <p>Working with HTML, authors may use the period (U+002E,
  913. <code>.</code>) notation as an alternative to the <code>~=</code>
  914. notation when representing the <code>class</code> attribute. Thus, for
  915. HTML, <code>div.value</code> and <code>div[class~=value]</code> have
  916. the same meaning. The attribute value must immediately follow the
  917. &quot;period&quot; (<code>.</code>).</p>
  918. <p>UAs may apply selectors using the period (.) notation in XML
  919. documents if the UA has namespace-specific knowledge that allows it to
  920. determine which attribute is the &quot;class&quot; attribute for the
  921. respective namespace. One such example of namespace-specific knowledge
  922. is the prose in the specification for a particular namespace (e.g. SVG
  923. 1.0 <a href="#refsSVG">[SVG]</a> describes the <a
  924. href="http://www.w3.org/TR/2001/PR-SVG-20010719/styling.html#ClassAttribute">SVG
  925. &quot;class&quot; attribute</a> and how a UA should interpret it, and
  926. similarly MathML 1.01 <a href="#refsMATH">[MATH]</a> describes the <a
  927. href="http://www.w3.org/1999/07/REC-MathML-19990707/chapter2.html#sec2.3.4">MathML
  928. &quot;class&quot; attribute</a>.)</p>
  929. <div class="example">
  930. <p>CSS examples:</p>
  931. <p>We can assign style information to all elements with
  932. <code>class~="pastoral"</code> as follows:</p>
  933. <pre>*.pastoral { color: green } /* all elements with class~=pastoral */</pre>
  934. <p>or just</p>
  935. <pre>.pastoral { color: green } /* all elements with class~=pastoral */</pre>
  936. <p>The following assigns style only to H1 elements with
  937. <code>class~="pastoral"</code>:</p>
  938. <pre>H1.pastoral { color: green } /* H1 elements with class~=pastoral */</pre>
  939. <p>Given these rules, the first H1 instance below would not have
  940. green text, while the second would:</p>
  941. <pre>&lt;H1&gt;Not green&lt;/H1&gt;
  942. &lt;H1 class="pastoral"&gt;Very green&lt;/H1&gt;</pre>
  943. </div>
  944. <p>To represent a subset of "class" values, each value must be preceded
  945. by a ".", in any order.</P>
  946. <div class="example">
  947. <p>CSS example:</p>
  948. <p>The following rule matches any P element whose "class" attribute
  949. has been assigned a list of <a
  950. href="#whitespace">whitespace</a>-separated values that includes
  951. "pastoral" and "marine":</p>
  952. <pre>p.pastoral.marine { color: green }</pre>
  953. <p>This rule matches when <code>class="pastoral blue aqua
  954. marine"</code> but does not match for <code>class="pastoral
  955. blue"</code>.</p>
  956. </div>
  957. <p class="note"><strong>Note:</strong> Because CSS gives considerable
  958. power to the "class" attribute, authors could conceivably design their
  959. own "document language" based on elements with almost no associated
  960. presentation (such as DIV and SPAN in HTML) and assigning style
  961. information through the "class" attribute. Authors should avoid this
  962. practice since the structural elements of a document language often
  963. have recognized and accepted meanings and author-defined classes may
  964. not.</p>
  965. <p class="note"><strong>Note:</strong> If an element has multiple
  966. class attributes, their values must be concatenated with spaces
  967. between the values before searching for the class. As of this time the
  968. working group is not aware of any manner in which this situation can
  969. be reached, however, so this behavior is explicitly non-normative in
  970. this specification.</p>
  971. <h3><a name=id-selectors>6.5. ID selectors</a></h3>
  972. <p>Document languages may contain attributes that are declared to be
  973. of type ID. What makes attributes of type ID special is that no two
  974. such attributes can have the same value in a document, regardless of
  975. the type of the elements that carry them; whatever the document
  976. language, an ID typed attribute can be used to uniquely identify its
  977. element. In HTML all ID attributes are named "id"; XML applications
  978. may name ID attributes differently, but the same restriction
  979. applies.</p>
  980. <p>An ID-typed attribute of a document language allows authors to
  981. assign an identifier to one element instance in the document tree. W3C
  982. ID selectors represent an element instance based on its identifier. An
  983. ID selector contains a &quot;number sign&quot; (U+0023,
  984. <code>#</code>) immediately followed by the ID value, which must be an
  985. identifier.</p>
  986. <p>Selectors does not specify how a UA knows the ID-typed attribute of
  987. an element. The UA may, e.g., read a document's DTD, have the
  988. information hard-coded or ask the user.
  989. <div class="example">
  990. <p>Examples:</p>
  991. <p>The following ID selector represents an <code>h1</code> element
  992. whose ID-typed attribute has the value "chapter1":</p>
  993. <pre>h1#chapter1</pre>
  994. <p>The following ID selector represents any element whose ID-typed
  995. attribute has the value "chapter1":</p>
  996. <pre>#chapter1</pre>
  997. <p>The following selector represents any element whose ID-typed
  998. attribute has the value "z98y".</p>
  999. <pre>*#z98y</pre>
  1000. </div>
  1001. <p class="note"><strong>Note.</strong> In XML 1.0 <a
  1002. href="#refsXML10">[XML10]</a>, the information about which attribute
  1003. contains an element's IDs is contained in a DTD or a schema. When
  1004. parsing XML, UAs do not always read the DTD, and thus may not know
  1005. what the ID of an element is (though a UA may have namespace-specific
  1006. knowledge that allows it to determine which attribute is the ID
  1007. attribute for that namespace). If a style sheet designer knows or
  1008. suspects that a UA may not know what the ID of an element is, he
  1009. should use normal attribute selectors instead:
  1010. <code>[name=p371]</code> instead of <code>#p371</code>. Elements in
  1011. XML 1.0 documents without a DTD do not have IDs at all.</p>
  1012. <p>If an element has multiple ID attributes, all of them must be
  1013. treated as IDs for that element for the purposes of the ID
  1014. selector. Such a situation could be reached using mixtures of xml:id,
  1015. DOM3 Core, XML DTDs, and namespace-specific knowledge.</p>
  1016. <h3><a name=pseudo-classes>6.6. Pseudo-classes</a></h3>
  1017. <p>The pseudo-class concept is introduced to permit selection based on
  1018. information that lies outside of the document tree or that cannot be
  1019. expressed using the other simple selectors.</p>
  1020. <p>A pseudo-class always consists of a &quot;colon&quot;
  1021. (<code>:</code>) followed by the name of the pseudo-class and
  1022. optionally by a value between parentheses.</p>
  1023. <p>Pseudo-classes are allowed in all sequences of simple selectors
  1024. contained in a selector. Pseudo-classes are allowed anywhere in
  1025. sequences of simple selectors, after the leading type selector or
  1026. universal selector (possibly omitted). Pseudo-class names are
  1027. case-insensitive. Some pseudo-classes are mutually exclusive, while
  1028. others can be applied simultaneously to the same
  1029. element. Pseudo-classes may be dynamic, in the sense that an element
  1030. may acquire or lose a pseudo-class while a user interacts with the
  1031. document.</p>
  1032. <h4><a name=dynamic-pseudos>6.6.1. Dynamic pseudo-classes</a></h4>
  1033. <p>Dynamic pseudo-classes classify elements on characteristics other
  1034. than their name, attributes, or content, in principle characteristics
  1035. that cannot be deduced from the document tree.</p>
  1036. <p>Dynamic pseudo-classes do not appear in the document source or
  1037. document tree.</p>
  1038. <h5>The <a name=link>link pseudo-classes: :link and :visited</a></h5>
  1039. <p>User agents commonly display unvisited links differently from
  1040. previously visited ones. Selectors
  1041. provides the pseudo-classes <code>:link</code> and
  1042. <code>:visited</code> to distinguish them:</p>
  1043. <ul>
  1044. <li>The <code>:link</code> pseudo-class applies to links that have
  1045. not yet been visited.</li>
  1046. <li>The <code>:visited</code> pseudo-class applies once the link has
  1047. been visited by the user. </li>
  1048. </ul>
  1049. <p>After some amount of time, user agents may choose to return a
  1050. visited link to the (unvisited) ':link' state.</p>
  1051. <p>The two states are mutually exclusive.</p>
  1052. <div class="example">
  1053. <p>Example:</p>
  1054. <p>The following selector represents links carrying class
  1055. <code>external</code> and already visited:</p>
  1056. <pre>a.external:visited</pre>
  1057. </div>
  1058. <p class="note"><strong>Note:</strong> It is possible for style sheet
  1059. authors to abuse the :link and :visited pseudo-classes to determine
  1060. which sites a user has visited without the user's consent.
  1061. <p>UAs may therefore treat all links as unvisited links, or implement
  1062. other measures to preserve the user's privacy while rendering visited
  1063. and unvisited links differently.</p>
  1064. <h5>The <a name=useraction-pseudos>user action pseudo-classes
  1065. :hover, :active, and :focus</a></h5>
  1066. <p>Interactive user agents sometimes change the rendering in response
  1067. to user actions. Selectors provides
  1068. three pseudo-classes for the selection of an element the user is
  1069. acting on.</p>
  1070. <ul>
  1071. <li>The <code>:hover</code> pseudo-class applies while the user
  1072. designates an element with a pointing device, but does not activate
  1073. it. For example, a visual user agent could apply this pseudo-class
  1074. when the cursor (mouse pointer) hovers over a box generated by the
  1075. element. User agents not that do not support <a
  1076. href="http://www.w3.org/TR/REC-CSS2/media.html#interactive-media-group">interactive
  1077. media</a> do not have to support this pseudo-class. Some conforming
  1078. user agents that support <a
  1079. href="http://www.w3.org/TR/REC-CSS2/media.html#interactive-media-group">interactive
  1080. media</a> may not be able to support this pseudo-class (e.g., a pen
  1081. device that does not detect hovering).</li>
  1082. <li>The <code>:active</code> pseudo-class applies while an element
  1083. is being activated by the user. For example, between the times the
  1084. user presses the mouse button and releases it.</li>
  1085. <li>The <code>:focus</code> pseudo-class applies while an element
  1086. has the focus (accepts keyboard or mouse events, or other forms of
  1087. input). </li>
  1088. </ul>
  1089. <p>There may be document language or implementation specific limits on
  1090. which elements can become <code>:active</code> or acquire
  1091. <code>:focus</code>.</p>
  1092. <p>These pseudo-classes are not mutually exclusive. An element may
  1093. match several pseudo-classes at the same time.</p>
  1094. <p>Selectors doesn't define if the parent of an element that is
  1095. ':active' or ':hover' is also in that state.</p>
  1096. <div class="example">
  1097. <p>Examples:</p>
  1098. <pre>a:link /* unvisited links */
  1099. a:visited /* visited links */
  1100. a:hover /* user hovers */
  1101. a:active /* active links */</pre>
  1102. <p>An example of combining dynamic pseudo-classes:</p>
  1103. <pre>a:focus
  1104. a:focus:hover</pre>
  1105. <p>The last selector matches <code>a</code> elements that are in
  1106. the pseudo-class :focus and in the pseudo-class :hover.</p>
  1107. </div>
  1108. <p class="note"><strong>Note:</strong> An element can be both ':visited'
  1109. and ':active' (or ':link' and ':active').</p>
  1110. <h4><a name=target-pseudo>6.6.2. The target pseudo-class :target</a></h4>
  1111. <p>Some URIs refer to a location within a resource. This kind of URI
  1112. ends with a &quot;number sign&quot; (#) followed by an anchor
  1113. identifier (called the fragment identifier).</p>
  1114. <p>URIs with fragment identifiers link to a certain element within the
  1115. document, known as the target element. For instance, here is a URI
  1116. pointing to an anchor named <code>section_2</code> in an HTML
  1117. document:</p>
  1118. <pre>http://example.com/html/top.html#section_2</pre>
  1119. <p>A target element can be represented by the <code>:target</code>
  1120. pseudo-class. If the document's URI has no fragment identifier, then
  1121. the document has no target element.</p>
  1122. <div class="example">
  1123. <p>Example:</p>
  1124. <pre>p.note:target</pre>
  1125. <p>This selector represents a <code>p</code> element of class
  1126. <code>note</code> that is the target element of the referring
  1127. URI.</p>
  1128. </div>
  1129. <div class="example">
  1130. <p>CSS example:</p>
  1131. <p>Here, the <code>:target</code> pseudo-class is used to make the
  1132. target element red and place an image before it, if there is one:</p>
  1133. <pre>*:target { color : red }
  1134. *:target::before { content : url(target.png) }</pre>
  1135. </div>
  1136. <h4><a name=lang-pseudo>6.6.3. The language pseudo-class :lang</a></h4>
  1137. <p>If the document language specifies how the human language of an
  1138. element is determined, it is possible to write selectors that
  1139. represent an element based on its language. For example, in HTML <a
  1140. href="#refsHTML4">[HTML4]</a>, the language is determined by a
  1141. combination of the <code>lang</code> attribute, the <code>meta</code>
  1142. element, and possibly by information from the protocol (such as HTTP
  1143. headers). XML uses an attribute called <code>xml:lang</code>, and
  1144. there may be other document language-specific methods for determining
  1145. the language.</p>
  1146. <p>The pseudo-class <code>:lang(C)</code> represents an element that
  1147. is in language C. Whether an element is represented by a
  1148. <code>:lang()</code> selector is based solely on the identifier C
  1149. being either equal to, or a hyphen-separated substring of, the
  1150. element's language value, in the same way as if performed by the <a
  1151. href="#attribute-representation">'|='</a> operator in attribute
  1152. selectors. The identifier C does not have to be a valid language
  1153. name.</p>
  1154. <p>C must not be empty. (If it is, the selector is invalid.)</p>
  1155. <p class="note"><strong>Note:</strong> It is recommended that
  1156. documents and protocols indicate language using codes from RFC 3066 <a
  1157. href="#refsRFC3066">[RFC3066]</a> or its successor, and by means of
  1158. "xml:lang" attributes in the case of XML-based documents <a
  1159. href="#refsXML10">[XML10]</a>. See <a
  1160. href="http://www.w3.org/International/questions/qa-lang-2or3.html">
  1161. "FAQ: Two-letter or three-letter language codes."</a></p>
  1162. <div class="example">
  1163. <p>Examples:</p>
  1164. <p>The two following selectors represent an HTML document that is in
  1165. Belgian, French, or German. The two next selectors represent
  1166. <code>q</code> quotations in an arbitrary element in Belgian, French,
  1167. or German.</p>
  1168. <pre>html:lang(fr-be)
  1169. html:lang(de)
  1170. :lang(fr-be) &gt; q
  1171. :lang(de) &gt; q</pre>
  1172. </div>
  1173. <h4><a name=UIstates>6.6.4. The UI element states pseudo-classes</a></h4>
  1174. <h5><a name=enableddisabled>The :enabled and :disabled pseudo-classes</a></h5>
  1175. <p>The <code>:enabled</code> pseudo-class allows authors to customize
  1176. the look of user interface elements that are enabled &mdash; which the
  1177. user can select or activate in some fashion (e.g. clicking on a button
  1178. with a mouse). There is a need for such a pseudo-class because there
  1179. is no way to programmatically specify the default appearance of say,
  1180. an enabled <code>input</code> element without also specifying what it
  1181. would look like when it was disabled.</p>
  1182. <p>Similar to <code>:enabled</code>, <code>:disabled</code> allows the
  1183. author to specify precisely how a disabled or inactive user interface
  1184. element should look.</p>
  1185. <p>Most elements will be neither enabled nor disabled. An element is
  1186. enabled if the user can either activate it or transfer the focus to
  1187. it. An element is disabled if it could be enabled, but the user cannot
  1188. presently activate it or transfer focus to it.</p>
  1189. <h5><a name=checked>The :checked pseudo-class</a></h5>
  1190. <p>Radio and checkbox elements can be toggled by the user. Some menu
  1191. items are "checked" when the user selects them. When such elements are
  1192. toggled "on" the <code>:checked</code> pseudo-class applies. The
  1193. <code>:checked</code> pseudo-class initially applies to such elements
  1194. that have the HTML4 <code>selected</code> and <code>checked</code>
  1195. attributes as described in <a
  1196. href="http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.2.1">Section
  1197. 17.2.1 of HTML4</a>, but of course the user can toggle "off" such
  1198. elements in which case the <code>:checked</code> pseudo-class would no
  1199. longer apply. While the <code>:checked</code> pseudo-class is dynamic
  1200. in nature, and is altered by user action, since it can also be based
  1201. on the presence of the semantic HTML4 <code>selected</code> and
  1202. <code>checked</code> attributes, it applies to all media.
  1203. <h5><a name=indeterminate>The :indeterminate pseudo-class</a></h5>
  1204. <div class="note">
  1205. <p>Radio and checkbox elements can be toggled by the user, but are
  1206. sometimes in an indeterminate state, neither checked nor unchecked.
  1207. This can be due to an element attribute, or DOM manipulation.</p>
  1208. <p>A future version of this specification may introduce an
  1209. <code>:indeterminate</code> pseudo-class that applies to such elements.
  1210. <!--While the <code>:indeterminate</code> pseudo-class is dynamic in
  1211. nature, and is altered by user action, since it can also be based on
  1212. the presence of an element attribute, it applies to all media.</p>
  1213. <p>Components of a radio-group initialized with no pre-selected choice
  1214. are an example of :indeterminate state.--></p>
  1215. </div>
  1216. <h4><a name=structural-pseudos>6.6.5. Structural pseudo-classes</a></h4>
  1217. <p>Selectors introduces the concept of <dfn>structural
  1218. pseudo-classes</dfn> to permit selection based on extra information that lies in
  1219. the document tree but cannot be represented by other simple selectors or
  1220. combinators.
  1221. <p>Note that standalone pieces of PCDATA (text nodes in the DOM) are
  1222. not counted when calculating the position of an element in the list of
  1223. children of its parent. When calculating the position of an element in
  1224. the list of children of its parent, the index numbering starts at 1.
  1225. <h5><a name=root-pseudo>:root pseudo-class</a></h5>
  1226. <p>The <code>:root</code> pseudo-class represents an element that is
  1227. the root of the document. In HTML 4, this is always the
  1228. <code>HTML</code> element.
  1229. <h5><a name=nth-child-pseudo>:nth-child() pseudo-class</a></h5>
  1230. <p>The
  1231. <code>:nth-child(<var>a</var><code>n</code>+<var>b</var>)</code>
  1232. pseudo-class notation represents an element that has
  1233. <var>a</var><code>n</code>+<var>b</var>-1 siblings
  1234. <strong>before</strong> it in the document tree, for a given positive
  1235. integer or zero value of <code>n</code>, and has a parent element. In
  1236. other words, this matches the <var>b</var>th child of an element after
  1237. all the children have been split into groups of <var>a</var> elements
  1238. each. For example, this allows the selectors to address every other
  1239. row in a table, and could be used to alternate the color
  1240. of paragraph text in a cycle of four. The <var>a</var> and
  1241. <var>b</var> values must be zero, negative integers or positive
  1242. integers. The index of the first child of an element is 1.
  1243. <p>In addition to this, <code>:nth-child()</code> can take
  1244. '<code>odd</code>' and '<code>even</code>' as arguments instead.
  1245. '<code>odd</code>' has the same signification as <code>2n+1</code>,
  1246. and '<code>even</code>' has the same signification as <code>2n</code>.
  1247. <div class="example">
  1248. <p>Examples:</p>
  1249. <pre>tr:nth-child(2n+1) /* represents every odd row of an HTML table */
  1250. tr:nth-child(odd) /* same */
  1251. tr:nth-child(2n) /* represents every even row of an HTML table */
  1252. tr:nth-child(even) /* same */
  1253. /* Alternate paragraph colours in CSS */
  1254. p:nth-child(4n+1) { color: navy; }
  1255. p:nth-child(4n+2) { color: green; }
  1256. p:nth-child(4n+3) { color: maroon; }
  1257. p:nth-child(4n+4) { color: purple; }</pre>
  1258. </div>
  1259. <p>When <var>a</var>=0, no repeating is used, so for example
  1260. <code>:nth-child(0n+5)</code> matches only the fifth child. When
  1261. <var>a</var>=0, the <var>a</var><code>n</code> part need not be
  1262. included, so the syntax simplifies to
  1263. <code>:nth-child(<var>b</var>)</code> and the last example simplifies
  1264. to <code>:nth-child(5)</code>.
  1265. <div class="example">
  1266. <p>Examples:</p>
  1267. <pre>foo:nth-child(0n+1) /* represents an element foo, first child of its parent element */
  1268. foo:nth-child(1) /* same */</pre>
  1269. </div>
  1270. <p>When <var>a</var>=1, the number may be omitted from the rule.
  1271. <div class="example">
  1272. <p>Examples:</p>
  1273. <p>The following selectors are therefore equivalent:</p>
  1274. <pre>bar:nth-child(1n+0) /* represents all bar elements, specificity (0,1,1) */
  1275. bar:nth-child(n+0) /* same */
  1276. bar:nth-child(n) /* same */
  1277. bar /* same but lower specificity (0,0,1) */</pre>
  1278. </div>
  1279. <p>If <var>b</var>=0, then every <var>a</var>th element is picked. In
  1280. such a case, the <var>b</var> part may be omitted.
  1281. <div class="example">
  1282. <p>Examples:</p>
  1283. <pre>tr:nth-child(2n+0) /* represents every even row of an HTML table */
  1284. tr:nth-child(2n) /* same */</pre>
  1285. </div>
  1286. <p>If both <var>a</var> and <var>b</var> are equal to zero, the
  1287. pseudo-class represents no element in the document tree.</p>
  1288. <p>The value <var>a</var> can be negative, but only the positive
  1289. values of <var>a</var><code>n</code>+<var>b</var>, for
  1290. <code>n</code>&ge;0, may represent an element in the document
  1291. tree.</p>
  1292. <div class="example">
  1293. <p>Example:</p>
  1294. <pre>html|tr:nth-child(-n+6) /* represents the 6 first rows of XHTML tables */</pre>
  1295. </div>
  1296. <p>When the value <var>b</var> is negative, the "+" character in the
  1297. expression must be removed (it is effectively replaced by the "-"
  1298. character indicating the negative value of <var>b</var>).</p>
  1299. <div class="example">
  1300. <p>Examples:</p>
  1301. <pre>:nth-child(10n-1) /* represents the 9th, 19th, 29th, etc, element */
  1302. :nth-child(10n+9) /* Same */
  1303. :nth-child(10n+-1) /* Syntactically invalid, and would be ignored */</pre>
  1304. </div>
  1305. <h5><a name=nth-last-child-pseudo>:nth-last-child() pseudo-class</a></h5>
  1306. <p>The <code>:nth-last-child(<var>a</var>n+<var>b</var>)</code>
  1307. pseudo-class notation represents an element that has
  1308. <var>a</var><code>n</code>+<var>b</var>-1 siblings
  1309. <strong>after</strong> it in the document tree, for a given positive
  1310. integer or zero value of <code>n</code>, and has a parent element. See
  1311. <code>:nth-child()</code> pseudo-class for the syntax of its argument.
  1312. It also accepts the '<code>even</code>' and '<code>odd</code>' values
  1313. as arguments.
  1314. <div class="example">
  1315. <p>Examples:</p>
  1316. <pre>tr:nth-last-child(-n+2) /* represents the two last rows of an HTML table */
  1317. foo:nth-last-child(odd) /* represents all odd foo elements in their parent element,
  1318. counting from the last one */</pre>
  1319. </div>
  1320. <h5><a name=nth-of-type-pseudo>:nth-of-type() pseudo-class</a></h5>
  1321. <p>The <code>:nth-of-type(<var>a</var>n+<var>b</var>)</code>
  1322. pseudo-class notation represents an element that has
  1323. <var>a</var><code>n</code>+<var>b</var>-1 siblings with the same
  1324. element name <strong>before</strong> it in the document tree, for a
  1325. given zero or positive integer value of <code>n</code>, and has a
  1326. parent element. In other words, this matches the <var>b</var>th child
  1327. of that type after all the children of that type have been split into
  1328. groups of a elements each. See <code>:nth-child()</code> pseudo-class
  1329. for the syntax of its argument. It also accepts the
  1330. '<code>even</code>' and '<code>odd</code>' values.
  1331. <div class="example">
  1332. <p>CSS example:</p>
  1333. <p>This allows an author to alternate the position of floated images:</p>
  1334. <pre>img:nth-of-type(2n+1) { float: right; }
  1335. img:nth-of-type(2n) { float: left; }</pre>
  1336. </div>
  1337. <h5><a name=nth-last-of-type-pseudo>:nth-last-of-type() pseudo-class</a></h5>
  1338. <p>The <code>:nth-last-of-type(<var>a</var>n+<var>b</var>)</code>
  1339. pseudo-class notation represents an element that has
  1340. <var>a</var><code>n</code>+<var>b</var>-1 siblings with the same
  1341. element name <strong>after</strong> it in the document tree, for a
  1342. given zero or positive integer value of <code>n</code>, and has a
  1343. parent element. See <code>:nth-child()</code> pseudo-class for the
  1344. syntax of its argument. It also accepts the '<code>even</code>' and '<code>odd</code>' values.
  1345. <div class="example">
  1346. <p>Example:</p>
  1347. <p>To represent all <code>h2</code> children of an XHTML
  1348. <code>body</code> except the first and last, one could use the
  1349. following selector:</p>
  1350. <pre>body &gt; h2:nth-of-type(n+2):nth-last-of-type(n+2)</pre>
  1351. <p>In this case, one could also use <code>:not()</code>, although the
  1352. selector ends up being just as long:</p>
  1353. <pre>body &gt; h2:not(:first-of-type):not(:last-of-type)</pre>
  1354. </div>
  1355. <h5><a name=first-child-pseudo>:first-child pseudo-class</a></h5>
  1356. <p>Same as <code>:nth-child(1)</code>. The <code>:first-child</code> pseudo-class
  1357. represents an element that is the first child of some other element.
  1358. <div class="example">
  1359. <p>Examples:</p>
  1360. <p>The following selector represents a <code>p</code> element that is
  1361. the first child of a <code>div</code> element:</p>
  1362. <pre>div &gt; p:first-child</pre>
  1363. <p>This selector can represent the <code>p</code> inside the
  1364. <code>div</code> of the following fragment:</p>
  1365. <pre>&lt;p&gt; The last P before the note.&lt;/p&gt;
  1366. &lt;div class="note"&gt;
  1367. &lt;p&gt; The first P inside the note.&lt;/p&gt;
  1368. &lt;/div&gt;</pre>but cannot represent the second <code>p</code> in the following
  1369. fragment:
  1370. <pre>&lt;p&gt; The last P before the note.&lt;/p&gt;
  1371. &lt;div class="note"&gt;
  1372. &lt;h2&gt; Note &lt;/h2&gt;
  1373. &lt;p&gt; The first P inside the note.&lt;/p&gt;
  1374. &lt;/div&gt;</pre>
  1375. <p>The following two selectors are usually equivalent:</p>
  1376. <pre>* &gt; a:first-child /* a is first child of any element */
  1377. a:first-child /* Same (assuming a is not the root element) */</pre>
  1378. </div>
  1379. <h5><a name=last-child-pseudo>:last-child pseudo-class</a></h5>
  1380. <p>Same as <code>:nth-last-child(1)</code>. The <code>:last-child</code> pseudo-class
  1381. represents an element that is the last child of some other element.
  1382. <div class="example">
  1383. <p>Example:</p>
  1384. <p>The following selector represents a list item <code>li</code> that
  1385. is the last child of an ordered list <code>ol</code>.
  1386. <pre>ol &gt; li:last-child</pre>
  1387. </div>
  1388. <h5><a name=first-of-type-pseudo>:first-of-type pseudo-class</a></h5>
  1389. <p>Same as <code>:nth-of-type(1)</code>. The <code>:first-of-type</code> pseudo-class
  1390. represents an element that is the first sibling of its type in the list of
  1391. children of its parent element.
  1392. <div class="example">
  1393. <p>Example:</p>
  1394. <p>The following selector represents a definition title
  1395. <code>dt</code> inside a definition list <code>dl</code>, this
  1396. <code>dt</code> being the first of its type in the list of children of
  1397. its parent element.</p>
  1398. <pre>dl dt:first-of-type</pre>
  1399. <p>It is a valid description for the first two <code>dt</code>
  1400. elements in the following example but not for the third one:</p>
  1401. <pre>&lt;dl&gt;
  1402. &lt;dt&gt;gigogne&lt;/dt&gt;
  1403. &lt;dd&gt;
  1404. &lt;dl&gt;
  1405. &lt;dt&gt;fus&eacute;e&lt;/dt&gt;
  1406. &lt;dd&gt;multistage rocket&lt;/dd&gt;
  1407. &lt;dt&gt;table&lt;/dt&gt;
  1408. &lt;dd&gt;nest of tables&lt;/dd&gt;
  1409. &lt;/dl&gt;
  1410. &lt;/dd&gt;
  1411. &lt;/dl&gt;</pre>
  1412. </div>
  1413. <h5><a name=last-of-type-pseudo>:last-of-type pseudo-class</a></h5>
  1414. <p>Same as <code>:nth-last-of-type(1)</code>. The
  1415. <code>:last-of-type</code> pseudo-class represents an element that is
  1416. the last sibling of its type in the list of children of its parent
  1417. element.</p>
  1418. <div class="example">
  1419. <p>Example:</p>
  1420. <p>The following selector represents the last data cell
  1421. <code>td</code> of a table row.</p>
  1422. <pre>tr &gt; td:last-of-type</pre>
  1423. </div>
  1424. <h5><a name=only-child-pseudo>:only-child pseudo-class</a></h5>
  1425. <p>Represents an element that has a parent element and whose parent
  1426. element has no other element children. Same as
  1427. <code>:first-child:last-child</code> or
  1428. <code>:nth-child(1):nth-last-child(1)</code>, but with a lower
  1429. specificity.</p>
  1430. <h5><a name=only-of-type-pseudo>:only-of-type pseudo-class</a></h5>
  1431. <p>Represents an element that has a parent element and whose parent
  1432. element has no other element children with the same element name. Same
  1433. as <code>:first-of-type:last-of-type</code> or
  1434. <code>:nth-of-type(1):nth-last-of-type(1)</code>, but with a lower
  1435. specificity.</p>
  1436. <h5><a name=empty-pseudo></a>:empty pseudo-class</h5>
  1437. <p>The <code>:empty</code> pseudo-class represents an element that has
  1438. no children at all. In terms of the DOM, only element nodes and text
  1439. nodes (including CDATA nodes and entity references) whose data has a
  1440. non-zero length must be considered as affecting emptiness; comments,
  1441. PIs, and other nodes must not affect whether an element is considered
  1442. empty or not.</p>
  1443. <div class="example">
  1444. <p>Examples:</p>
  1445. <p><code>p:empty</code> is a valid representation of the following fragment:</p>
  1446. <pre>&lt;p&gt;&lt;/p&gt;</pre>
  1447. <p><code>foo:empty</code> is not a valid representation for the
  1448. following fragments:</p>
  1449. <pre>&lt;foo&gt;bar&lt;/foo&gt;</pre>
  1450. <pre>&lt;foo&gt;&lt;bar&gt;bla&lt;/bar&gt;&lt;/foo&gt;</pre>
  1451. <pre>&lt;foo&gt;this is not &lt;bar&gt;:empty&lt;/bar&gt;&lt;/foo&gt;</pre>
  1452. </div>
  1453. <h4><a name=content-selectors>6.6.6. Blank</a></h4> <!-- It's the Return of Appendix H!!! Run away! -->
  1454. <p>This section intentionally left blank.</p>
  1455. <!-- (used to be :contains()) -->
  1456. <h4><a name=negation></a>6.6.7. The negation pseudo-class</h4>
  1457. <p>The negation pseudo-class, <code>:not(<var>X</var>)</code>, is a
  1458. functional notation taking a <a href="#simple-selectors-dfn">simple
  1459. selector</a> (excluding the negation pseudo-class itself and
  1460. pseudo-elements) as an argument. It represents an element that is not
  1461. represented by the argument.
  1462. <!-- pseudo-elements are not simple selectors, so the above paragraph
  1463. may be a bit confusing -->
  1464. <div class="example">
  1465. <p>Examples:</p>
  1466. <p>The following CSS selector matches all <code>button</code>
  1467. elements in an HTML document that are not disabled.</p>
  1468. <pre>button:not([DISABLED])</pre>
  1469. <p>The following selector represents all but <code>FOO</code>
  1470. elements.</p>
  1471. <pre>*:not(FOO)</pre>
  1472. <p>The following group of selectors represents all HTML elements
  1473. except links.</p>
  1474. <pre>html|*:not(:link):not(:visited)</pre>
  1475. </div>
  1476. <p>Default namespace declarations do not affect the argument of the
  1477. negation pseudo-class unless the argument is a universal selector or a
  1478. type selector.</p>
  1479. <div class="example">
  1480. <p>Examples:</p>
  1481. <p>Assuming that the default namespace is bound to
  1482. "http://example.com/", the following selector represents all
  1483. elements that are not in that namespace:</p>
  1484. <pre>*|*:not(*)</pre>
  1485. <p>The following CSS selector matches any element being hovered,
  1486. regardless of its namespace. In particular, it is not limited to
  1487. only matching elements in the default namespace that are not being
  1488. hovered, and elements not in the default namespace don't match the
  1489. rule when they <em>are</em> being hovered.</p>
  1490. <pre>*|*:not(:hover)</pre>
  1491. </div>
  1492. <p class="note"><strong>Note</strong>: the :not() pseudo allows
  1493. useless selectors to be written. For instance <code>:not(*|*)</code>,
  1494. which represents no element at all, or <code>foo:not(bar)</code>,
  1495. which is equivalent to <code>foo</code> but with a higher
  1496. specificity.</p>
  1497. <h3><a name=pseudo-elements>7. Pseudo-elements</a></h3>
  1498. <p>Pseudo-elements create abstractions about the document tree beyond
  1499. those specified by the document language. For instance, document
  1500. languages do not offer mechanisms to access the first letter or first
  1501. line of an element's content. Pseudo-elements allow designers to refer
  1502. to this otherwise inaccessible information. Pseudo-elements may also
  1503. provide designers a way to refer to content that does not exist in the
  1504. source document (e.g., the <code>::before</code> and
  1505. <code>::after</code> pseudo-elements give access to generated
  1506. content).</p>
  1507. <p>A pseudo-element is made of two colons (<code>::</code>) followed
  1508. by the name of the pseudo-element.</p>
  1509. <p>This <code>::</code> notation is introduced by the current document
  1510. in order to establish a discrimination between pseudo-classes and
  1511. pseudo-elements. For compatibility with existing style sheets, user
  1512. agents must also accept the previous one-colon notation for
  1513. pseudo-elements introduced in CSS levels 1 and 2 (namely,
  1514. <code>:first-line</code>, <code>:first-letter</code>,
  1515. <code>:before</code> and <code>:after</code>). This compatibility is
  1516. not allowed for the new pseudo-elements introduced in CSS level 3.</p>
  1517. <p>Only one pseudo-element may appear per selector, and if present it
  1518. must appear after the sequence of simple selectors that represents the
  1519. <a href="#subject">subjects</a> of the selector. <span class="note">A
  1520. future version of this specification may allow multiple
  1521. pesudo-elements per selector.</span></p>
  1522. <h4><a name=first-line>7.1. The ::first-line pseudo-element</a></h4>
  1523. <p>The <code>::first-line</code> pseudo-element describes the contents
  1524. of the first formatted line of an element.
  1525. <div class="example">
  1526. <p>CSS example:</p>
  1527. <pre>p::first-line { text-transform: uppercase }</pre>
  1528. <p>The above rule means "change the letters of the first line of every
  1529. paragraph to uppercase".</p>
  1530. </div>
  1531. <p>The selector <code>p::first-line</code> does not match any real
  1532. HTML element. It does match a pseudo-element that conforming user
  1533. agents will insert at the beginning of every paragraph.</p>
  1534. <p>Note that the length of the first line depends on a number of
  1535. factors, including the width of the page, the font size, etc. Thus,
  1536. an ordinary HTML paragraph such as:</p>
  1537. <pre>
  1538. &lt;P&gt;This is a somewhat long HTML
  1539. paragraph that will be broken into several
  1540. lines. The first line will be identified
  1541. by a fictional tag sequence. The other lines
  1542. will be treated as ordinary lines in the
  1543. paragraph.&lt;/P&gt;
  1544. </pre>
  1545. <p>the lines of which happen to be broken as follows:
  1546. <pre>
  1547. THIS IS A SOMEWHAT LONG HTML PARAGRAPH THAT
  1548. will be broken into several lines. The first
  1549. line will be identified by a fictional tag
  1550. sequence. The other lines will be treated as
  1551. ordinary lines in the paragraph.
  1552. </pre>
  1553. <p>This paragraph might be "rewritten" by user agents to include the
  1554. <em>fictional tag sequence</em> for <code>::first-line</code>. This
  1555. fictional tag sequence helps to show how properties are inherited.</p>
  1556. <pre>
  1557. &lt;P&gt;<b>&lt;P::first-line&gt;</b> This is a somewhat long HTML
  1558. paragraph that <b>&lt;/P::first-line&gt;</b> will be broken into several
  1559. lines. The first line will be identified
  1560. by a fictional tag sequence. The other lines
  1561. will be treated as ordinary lines in the
  1562. paragraph.&lt;/P&gt;
  1563. </pre>
  1564. <p>If a pseudo-element breaks up a real element, the desired effect
  1565. can often be described by a fictional tag sequence that closes and
  1566. then re-opens the element. Thus, if we mark up the previous paragraph
  1567. with a <code>span</code> element:</p>
  1568. <pre>
  1569. &lt;P&gt;<b>&lt;SPAN class="test"&gt;</b> This is a somewhat long HTML
  1570. paragraph that will be broken into several
  1571. lines.<b>&lt;/SPAN&gt;</b> The first line will be identified
  1572. by a fictional tag sequence. The other lines
  1573. will be treated as ordinary lines in the
  1574. paragraph.&lt;/P&gt;
  1575. </pre>
  1576. <p>the user agent could simulate start and end tags for
  1577. <code>span</code> when inserting the fictional tag sequence for
  1578. <code>::first-line</code>.
  1579. <pre>
  1580. &lt;P&gt;&lt;P::first-line&gt;<b>&lt;SPAN class="test"&gt;</b> This is a
  1581. somewhat long HTML
  1582. paragraph that will <b>&lt;/SPAN&gt;</b>&lt;/P::first-line&gt;<b>&lt;SPAN class="test"&gt;</b> be
  1583. broken into several
  1584. lines.<b>&lt;/SPAN&gt;</b> The first line will be identified
  1585. by a fictional tag sequence. The other lines
  1586. will be treated as ordinary lines in the
  1587. paragraph.&lt;/P&gt;
  1588. </pre>
  1589. <p>In CSS, the <code>::first-line</code> pseudo-element can only be
  1590. attached to a block-level element, an inline-block, a table-caption,
  1591. or a table-cell.</p>
  1592. <p><a name="first-formatted-line"></a>The "first formatted line" of an
  1593. element may occur inside a
  1594. block-level descendant in the same flow (i.e., a block-level
  1595. descendant that is not positioned and not a float). E.g., the first
  1596. line of the <code>div</code> in <code>&lt;DIV>&lt;P>This
  1597. line...&lt;/P>&lt/DIV></code> is the first line of the <code>p</code> (assuming
  1598. that both <code>p</code> and <code>div</code> are block-level).
  1599. <p>The first line of a table-cell or inline-block cannot be the first
  1600. formatted line of an ancestor element. Thus, in <code>&lt;DIV&gt;&lt;P
  1601. STYLE="display: inline-block">Hello&lt;BR&gt;Goodbye&lt;/P&gt;
  1602. etcetera&lt;/DIV&gt;</code> the first formatted line of the
  1603. <code>div</code> is not the line "Hello".
  1604. <p class="note">Note that the first line of the <code>p</code> in this
  1605. fragment: <code>&lt;p&gt&lt;br&gt;First...</code> doesn't contain any
  1606. letters (assuming the default style for <code>br</code> in HTML
  1607. 4). The word "First" is not on the first formatted line.
  1608. <p>A UA should act as if the fictional start tags of the
  1609. <code>::first-line</code> pseudo-elements were nested just inside the
  1610. innermost enclosing block-level element. (Since CSS1 and CSS2 were
  1611. silent on this case, authors should not rely on this behavior.) Here
  1612. is an example. The fictional tag sequence for</p>
  1613. <pre>
  1614. &lt;DIV>
  1615. &lt;P>First paragraph&lt;/P>
  1616. &lt;P>Second paragraph&lt;/P>
  1617. &lt;/DIV>
  1618. </pre>
  1619. <p>is</p>
  1620. <pre>
  1621. &lt;DIV>
  1622. &lt;P>&lt;DIV::first-line>&lt;P::first-line>First paragraph&lt;/P::first-line>&lt;/DIV::first-line>&lt;/P>
  1623. &lt;P>&lt;P::first-line>Second paragraph&lt;/P::first-line>&lt;/P>
  1624. &lt;/DIV>
  1625. </pre>
  1626. <p>The <code>::first-line</code> pseudo-element is similar to an
  1627. inline-level element, but with certain restrictions. In CSS, the
  1628. following properties apply to a <code>::first-line</code>
  1629. pseudo-element: font properties, color property, background
  1630. properties, 'word-spacing', 'letter-spacing', 'text-decoration',
  1631. 'vertical-align', 'text-transform', 'line-height'. UAs may apply other
  1632. properties as well.</p>
  1633. <h4><a name=first-letter>7.2. The ::first-letter pseudo-element</a></h4>
  1634. <p>The <code>::first-letter</code> pseudo-element represents the first
  1635. letter of the first line of a block, if it is not preceded by any
  1636. other content (such as images or inline tables) on its line. The
  1637. ::first-letter pseudo-element may be used for "initial caps" and "drop
  1638. caps", which are common typographical effects. This type of initial
  1639. letter is similar to an inline-level element if its 'float' property
  1640. is 'none'; otherwise, it is similar to a floated element.</p>
  1641. <p>In CSS, these are the properties that apply to <code>::first-letter</code>
  1642. pseudo-elements: font properties, 'text-decoration', 'text-transform',
  1643. 'letter-spacing', 'word-spacing' (when appropriate), 'line-height',
  1644. 'float', 'vertical-align' (only if 'float' is 'none'), margin
  1645. properties, padding properties, border properties, color property,
  1646. background properties. UAs may apply other properties as well. To
  1647. allow UAs to render a typographically correct drop cap or initial cap,
  1648. the UA may choose a line-height, width and height based on the shape
  1649. of the letter, unlike for normal elements.</p>
  1650. <div class="example">
  1651. <p>Example:</p>
  1652. <p>This example shows a possible rendering of an initial cap. Note
  1653. that the 'line-height' that is inherited by the <code>::first-letter</code>
  1654. pseudo-element is 1.1, but the UA in this example has computed the
  1655. height of the first letter differently, so that it doesn't cause any
  1656. unnecessary space between the first two lines. Also note that the
  1657. fictional start tag of the first letter is inside the <span>span</span>, and thus
  1658. the font weight of the first letter is normal, not bold as the <span>span</span>:
  1659. <pre>
  1660. p { line-height: 1.1 }
  1661. p::first-letter { font-size: 3em; font-weight: normal }
  1662. span { font-weight: bold }
  1663. ...
  1664. &lt;p>&lt;span>Het hemelsche&lt;/span> gerecht heeft zich ten lange lesten&lt;br>
  1665. Erbarremt over my en mijn benaeuwde vesten&lt;br>
  1666. En arme burgery, en op mijn volcx gebed&lt;br>
  1667. En dagelix geschrey de bange stad ontzet.
  1668. </pre>
  1669. <div class="figure">
  1670. <p><img src="initial-cap.png" alt="Image illustrating the ::first-letter pseudo-element">
  1671. </div>
  1672. </div>
  1673. <div class="example">
  1674. <p>The following CSS will make a drop cap initial letter span about two lines:</p>
  1675. <pre>
  1676. &lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"&gt;
  1677. &lt;HTML&gt;
  1678. &lt;HEAD&gt;
  1679. &lt;TITLE&gt;Drop cap initial letter&lt;/TITLE&gt;
  1680. &lt;STYLE type="text/css"&gt;
  1681. P { font-size: 12pt; line-height: 1.2 }
  1682. P::first-letter { font-size: 200%; font-weight: bold; float: left }
  1683. SPAN { text-transform: uppercase }
  1684. &lt;/STYLE&gt;
  1685. &lt;/HEAD&gt;
  1686. &lt;BODY&gt;
  1687. &lt;P&gt;&lt;SPAN&gt;The first&lt;/SPAN&gt; few words of an article
  1688. in The Economist.&lt;/P&gt;
  1689. &lt;/BODY&gt;
  1690. &lt;/HTML&gt;
  1691. </pre>
  1692. <p>This example might be formatted as follows:</p>
  1693. <div class="figure">
  1694. <P><img src="first-letter.gif" alt="Image illustrating the combined effect of the ::first-letter and ::first-line pseudo-elements"></p>
  1695. </div>
  1696. <p>The <span class="index-inst" title="fictional tag
  1697. sequence">fictional tag sequence</span> is:</p>
  1698. <pre>
  1699. &lt;P&gt;
  1700. &lt;SPAN&gt;
  1701. &lt;P::first-letter&gt;
  1702. T
  1703. &lt;/P::first-letter&gt;he first
  1704. &lt;/SPAN&gt;
  1705. few words of an article in the Economist.
  1706. &lt;/P&gt;
  1707. </pre>
  1708. <p>Note that the <code>::first-letter</code> pseudo-element tags abut
  1709. the content (i.e., the initial character), while the ::first-line
  1710. pseudo-element start tag is inserted right after the start tag of the
  1711. block element.</p> </div>
  1712. <p>In order to achieve traditional drop caps formatting, user agents
  1713. may approximate font sizes, for example to align baselines. Also, the
  1714. glyph outline may be taken into account when formatting.</p>
  1715. <p>Punctuation (i.e, characters defined in Unicode in the "open" (Ps),
  1716. "close" (Pe), "initial" (Pi). "final" (Pf) and "other" (Po)
  1717. punctuation classes), that precedes or follows the first letter should
  1718. be included. <a href="#refsUNICODE">[UNICODE]</a></p>
  1719. <div class="figure">
  1720. <P><img src="first-letter2.gif" alt="Quotes that precede the
  1721. first letter should be included."></p>
  1722. </div>
  1723. <p>The <code>::first-letter</code> also applies if the first letter is
  1724. in fact a digit, e.g., the "6" in "67 million dollars is a lot of
  1725. money."</p>
  1726. <p>In CSS, the <code>::first-letter</code> pseudo-element applies to
  1727. block, list-item, table-cell, table-caption, and inline-block
  1728. elements. <span class="note">A future version of this specification
  1729. may allow this pesudo-element to apply to more element
  1730. types.</span></p>
  1731. <p>The <code>::first-letter</code> pseudo-element can be used with all
  1732. such elements that contain text, or that have a descendant in the same
  1733. flow that contains text. A UA should act as if the fictional start tag
  1734. of the ::first-letter pseudo-element is just before the first text of
  1735. the element, even if that first text is in a descendant.</p>
  1736. <div class="example">
  1737. <p>Example:</p>
  1738. <p>The fictional tag sequence for this HTMLfragment:
  1739. <pre>&lt;div>
  1740. &lt;p>The first text.</pre>
  1741. <p>is:
  1742. <pre>&lt;div>
  1743. &lt;p>&lt;div::first-letter>&lt;p::first-letter>T&lt;/...>&lt;/...>he first text.</pre>
  1744. </div>
  1745. <p>The first letter of a table-cell or inline-block cannot be the
  1746. first letter of an ancestor element. Thus, in <code>&lt;DIV&gt;&lt;P
  1747. STYLE="display: inline-block">Hello&lt;BR&gt;Goodbye&lt;/P&gt;
  1748. etcetera&lt;/DIV&gt;</code> the first letter of the <code>div</code> is not the
  1749. letter "H". In fact, the <code>div</code> doesn't have a first letter.
  1750. <p>The first letter must occur on the <a
  1751. href="#first-formatted-line">first formatted line.</a> For example, in
  1752. this fragment: <code>&lt;p&gt&lt;br&gt;First...</code> the first line
  1753. doesn't contain any letters and <code>::first-letter</code> doesn't
  1754. match anything (assuming the default style for <code>br</code> in HTML
  1755. 4). In particular, it does not match the "F" of "First."
  1756. <p>In CSS, if an element is a list item ('display: list-item'), the
  1757. <code>::first-letter</code> applies to the first letter in the
  1758. principal box after the marker. UAs may ignore
  1759. <code>::first-letter</code> on list items with 'list-style-position:
  1760. inside'. If an element has <code>::before</code> or
  1761. <code>::after</code> content, the <code>::first-letter</code> applies
  1762. to the first letter of the element <em>including</em> that content.
  1763. <div class="example">
  1764. <p>Example:</p>
  1765. <p>After the rule 'p::before {content: "Note: "}', the selector
  1766. 'p::first-letter' matches the "N" of "Note".</p>
  1767. </div>
  1768. <p>Some languages may have specific rules about how to treat certain
  1769. letter combinations. In Dutch, for example, if the letter combination
  1770. "ij" appears at the beginning of a word, both letters should be
  1771. considered within the <code>::first-letter</code> pseudo-element.
  1772. <p>If the letters that would form the ::first-letter are not in the
  1773. same element, such as "'T" in <code>&lt;p>'&lt;em>T...</code>, the UA
  1774. may create a ::first-letter pseudo-element from one of the elements,
  1775. both elements, or simply not create a pseudo-element.</p>
  1776. <p>Similarly, if the first letter(s) of the block are not at the start
  1777. of the line (for example due to bidirectional reordering), then the UA
  1778. need not create the pseudo-element(s).
  1779. <div class="example">
  1780. <p>Example:</p>
  1781. <p><a name="overlapping-example">The following example</a> illustrates
  1782. how overlapping pseudo-elements may interact. The first letter of
  1783. each P element will be green with a font size of '24pt'. The rest of
  1784. the first formatted line will be 'blue' while the rest of the
  1785. paragraph will be 'red'.</p>
  1786. <pre>p { color: red; font-size: 12pt }
  1787. p::first-letter { color: green; font-size: 200% }
  1788. p::first-line { color: blue }
  1789. &lt;P&gt;Some text that ends up on two lines&lt;/P&gt;</pre>
  1790. <p>Assuming that a line break will occur before the word "ends", the
  1791. <span class="index-inst" title="fictional tag sequence">fictional tag
  1792. sequence</span> for this fragment might be:</p>
  1793. <pre>&lt;P&gt;
  1794. &lt;P::first-line&gt;
  1795. &lt;P::first-letter&gt;
  1796. S
  1797. &lt;/P::first-letter&gt;ome text that
  1798. &lt;/P::first-line&gt;
  1799. ends up on two lines
  1800. &lt;/P&gt;</pre>
  1801. <p>Note that the <code>::first-letter</code> element is inside the <code>::first-line</code>
  1802. element. Properties set on <code>::first-line</code> are inherited by
  1803. <code>::first-letter</code>, but are overridden if the same property is set on
  1804. <code>::first-letter</code>.</p>
  1805. </div>
  1806. <h4><a name=UIfragments>7.3.</a> <a name=selection>The ::selection pseudo-element</a></h4>
  1807. <p>The <code>::selection</code> pseudo-element applies to the portion
  1808. of a document that has been highlighted by the user. This also
  1809. applies, for example, to selected text within an editable text
  1810. field. This pseudo-element should not be confused with the <code><a
  1811. href="#checked">:checked</a></code> pseudo-class (which used to be
  1812. named <code>:selected</code>)
  1813. <p>Although the <code>::selection</code> pseudo-element is dynamic in
  1814. nature, and is altered by user action, it is reasonable to expect that
  1815. when a UA re-renders to a static medium (such as a printed page, see
  1816. <a href="#refsCSS21">[CSS21]</a>) which was originally rendered to a
  1817. dynamic medium (like screen), the UA may wish to transfer the current
  1818. <code>::selection</code> state to that other medium, and have all the
  1819. appropriate formatting and rendering take effect as well. This is not
  1820. required &mdash; UAs may omit the <code>::selection</code>
  1821. pseudo-element for static media.
  1822. <p>These are the CSS properties that apply to <code>::selection</code>
  1823. pseudo-elements: color, background, cursor (optional), outline
  1824. (optional). The computed value of the 'background-image' property on
  1825. <code>::selection</code> may be ignored.
  1826. <h4><a name=gen-content>7.4. The ::before and ::after pseudo-elements</a></h4>
  1827. <p>The <code>::before</code> and <code>::after</code> pseudo-elements
  1828. can be used to describe generated content before or after an element's
  1829. content. They are explained in CSS 2.1 <a
  1830. href="#refsCSS21">[CSS21]</a>.</p>
  1831. <p>When the <code>::first-letter</code> and <code>::first-line</code>
  1832. pseudo-elements are combined with <code>::before</code> and
  1833. <code>::after</code>, they apply to the first letter or line of the
  1834. element including the inserted text.</p>
  1835. <h2><a name=combinators>8. Combinators</a></h2>
  1836. <h3><a name=descendant-combinators>8.1. Descendant combinator</a></h3>
  1837. <p>At times, authors may want selectors to describe an element that is
  1838. the descendant of another element in the document tree (e.g., "an
  1839. <code>EM</code> element that is contained within an <code>H1</code>
  1840. element"). Descendant combinators express such a relationship. A
  1841. descendant combinator is <a href="#whitespace">white space</a> that
  1842. separates two sequences of simple selectors. A selector of the form
  1843. "<code>A B</code>" represents an element <code>B</code> that is an
  1844. arbitrary descendant of some ancestor element <code>A</code>.
  1845. <div class="example">
  1846. <p>Examples:</p>
  1847. <p>For example, consider the following selector:</p>
  1848. <pre>h1 em</pre>
  1849. <p>It represents an <code>em</code> element being the descendant of
  1850. an <code>h1</code> element. It is a correct and valid, but partial,
  1851. description of the following fragment:</p>
  1852. <pre>&lt;h1&gt;This &lt;span class="myclass"&gt;headline
  1853. is &lt;em&gt;very&lt;/em&gt; important&lt;/span&gt;&lt;/h1&gt;</pre>
  1854. <p>The following selector:</p>
  1855. <pre>div * p</pre>
  1856. <p>represents a <code>p</code> element that is a grandchild or later
  1857. descendant of a <code>div</code> element. Note the whitespace on
  1858. either side of the "*" is not part of the universal selector; the
  1859. whitespace is a combinator indicating that the DIV must be the
  1860. ancestor of some element, and that that element must be an ancestor
  1861. of the P.</p>
  1862. <p>The following selector, which combines descendant combinators and
  1863. <a href="#attribute-selectors">attribute selectors</a>, represents an
  1864. element that (1) has the <code>href</code> attribute set and (2) is
  1865. inside a <code>p</code> that is itself inside a <code>div</code>:</p>
  1866. <pre>div p *[href]</pre>
  1867. </div>
  1868. <h3><a name=child-combinators>8.2. Child combinators</a></h3>
  1869. <p>A <dfn>child combinator</dfn> describes a childhood relationship
  1870. between two elements. A child combinator is made of the
  1871. &quot;greater-than sign&quot; (<code>&gt;</code>) character and
  1872. separates two sequences of simple selectors.
  1873. <div class="example">
  1874. <p>Examples:</p>
  1875. <p>The following selector represents a <code>p</code> element that is
  1876. child of <code>body</code>:</p>
  1877. <pre>body &gt; p</pre>
  1878. <p>The following example combines descendant combinators and child
  1879. combinators.</p>
  1880. <pre>div ol&gt;li p</pre><!-- LEAVE THOSE SPACES OUT! see below -->
  1881. <p>It represents a <code>p</code> element that is a descendant of an
  1882. <code>li</code> element; the <code>li</code> element must be the
  1883. child of an <code>ol</code> element; the <code>ol</code> element must
  1884. be a descendant of a <code>div</code>. Notice that the optional white
  1885. space around the "&gt;" combinator has been left out.</p>
  1886. </div>
  1887. <p>For information on selecting the first child of an element, please
  1888. see the section on the <code><a
  1889. href="#structural-pseudos">:first-child</a></code> pseudo-class
  1890. above.</p>
  1891. <h3><a name=sibling-combinators>8.3. Sibling combinators</a></h3>
  1892. <p>There are two different sibling combinators: the adjacent sibling
  1893. combinator and the general sibling combinator. In both cases,
  1894. non-element nodes (e.g. text between elements) are ignored when
  1895. considering adjacency of elements.</p>
  1896. <h4><a name=adjacent-sibling-combinators>8.3.1. Adjacent sibling combinator</a></h4>
  1897. <p>The adjacent sibling combinator is made of the &quot;plus
  1898. sign&quot; (U+002B, <code>+</code>) character that separates two
  1899. sequences of simple selectors. The elements represented by the two
  1900. sequences share the same parent in the document tree and the element
  1901. represented by the first sequence immediately precedes the element
  1902. represented by the second one.</p>
  1903. <div class="example">
  1904. <p>Examples:</p>
  1905. <p>The following selector represents a <code>p</code> element
  1906. immediately following a <code>math</code> element:</p>
  1907. <pre>math + p</pre>
  1908. <p>The following selector is conceptually similar to the one in the
  1909. previous example, except that it adds an attribute selector &mdash; it
  1910. adds a constraint to the <code>h1</code> element, that it must have
  1911. <code>class="opener"</code>:</p>
  1912. <pre>h1.opener + h2</pre>
  1913. </div>
  1914. <h4><a name=general-sibling-combinators>8.3.2. General sibling combinator</a></h4>
  1915. <p>The general sibling combinator is made of the &quot;tilde&quot;
  1916. (U+007E, <code>~</code>) character that separates two sequences of
  1917. simple selectors. The elements represented by the two sequences share
  1918. the same parent in the document tree and the element represented by
  1919. the first sequence precedes (not necessarily immediately) the element
  1920. represented by the second one.</p>
  1921. <div class="example">
  1922. <p>Example:</p>
  1923. <pre>h1 ~ pre</pre>
  1924. <p>represents a <code>pre</code> element following an <code>h1</code>. It
  1925. is a correct and valid, but partial, description of:</p>
  1926. <pre>&lt;h1&gt;Definition of the function a&lt;/h1&gt;
  1927. &lt;p&gt;Function a(x) has to be applied to all figures in the table.&lt;/p&gt;
  1928. &lt;pre&gt;function a(x) = 12x/13.5&lt;/pre&gt;</pre>
  1929. </div>
  1930. <h2><a name=specificity>9. Calculating a selector's specificity</a></h2>
  1931. <p>A selector's specificity is calculated as follows:</p>
  1932. <ul>
  1933. <li>count the number of ID selectors in the selector (= a)</li>
  1934. <li>count the number of class selectors, attributes selectors, and pseudo-classes in the selector (= b)</li>
  1935. <li>count the number of element names in the selector (= c)</li>
  1936. <li>ignore pseudo-elements</li>
  1937. </ul>
  1938. <p>Selectors inside <a href="#negation">the negation pseudo-class</a>
  1939. are counted like any other, but the negation itself does not count as
  1940. a pseudo-class.</p>
  1941. <p>Concatenating the three numbers a-b-c (in a number system with a
  1942. large base) gives the specificity.</p>
  1943. <div class="example">
  1944. <p>Examples:</p>
  1945. <pre>* /* a=0 b=0 c=0 -&gt; specificity = 0 */
  1946. LI /* a=0 b=0 c=1 -&gt; specificity = 1 */
  1947. UL LI /* a=0 b=0 c=2 -&gt; specificity = 2 */
  1948. UL OL+LI /* a=0 b=0 c=3 -&gt; specificity = 3 */
  1949. H1 + *[REL=up] /* a=0 b=1 c=1 -&gt; specificity = 11 */
  1950. UL OL LI.red /* a=0 b=1 c=3 -&gt; specificity = 13 */
  1951. LI.red.level /* a=0 b=2 c=1 -&gt; specificity = 21 */
  1952. #x34y /* a=1 b=0 c=0 -&gt; specificity = 100 */
  1953. #s12:not(FOO) /* a=1 b=0 c=1 -&gt; specificity = 101 */
  1954. </pre>
  1955. </div>
  1956. <p class="note"><strong>Note:</strong> the specificity of the styles
  1957. specified in an HTML <code>style</code> attribute is described in CSS
  1958. 2.1. <a href="#refsCSS21">[CSS21]</a>.</p>
  1959. <h2><a name=w3cselgrammar>10. The grammar of Selectors</a></h2>
  1960. <h3><a name=grammar>10.1. Grammar</a></h3>
  1961. <p>The grammar below defines the syntax of Selectors. It is globally
  1962. LL(1) and can be locally LL(2) (but note that most UA's should not use
  1963. it directly, since it doesn't express the parsing conventions). The
  1964. format of the productions is optimized for human consumption and some
  1965. shorthand notations beyond Yacc (see <a href="#refsYACC">[YACC]</a>)
  1966. are used:</p>
  1967. <ul>
  1968. <li><b>*</b>: 0 or more
  1969. <li><b>+</b>: 1 or more
  1970. <li><b>?</b>: 0 or 1
  1971. <li><b>|</b>: separates alternatives
  1972. <li><b>[ ]</b>: grouping </li>
  1973. </ul>
  1974. <p>The productions are:</p>
  1975. <pre>selectors_group
  1976. : selector [ COMMA S* selector ]*
  1977. ;
  1978. selector
  1979. : simple_selector_sequence [ combinator simple_selector_sequence ]*
  1980. ;
  1981. combinator
  1982. /* combinators can be surrounded by white space */
  1983. : PLUS S* | GREATER S* | TILDE S* | S+
  1984. ;
  1985. simple_selector_sequence
  1986. : [ type_selector | universal ]
  1987. [ HASH | class | attrib | pseudo | negation ]*
  1988. | [ HASH | class | attrib | pseudo | negation ]+
  1989. ;
  1990. type_selector
  1991. : [ namespace_prefix ]? element_name
  1992. ;
  1993. namespace_prefix
  1994. : [ IDENT | '*' ]? '|'
  1995. ;
  1996. element_name
  1997. : IDENT
  1998. ;
  1999. universal
  2000. : [ namespace_prefix ]? '*'
  2001. ;
  2002. class
  2003. : '.' IDENT
  2004. ;
  2005. attrib
  2006. : '[' S* [ namespace_prefix ]? IDENT S*
  2007. [ [ PREFIXMATCH |
  2008. SUFFIXMATCH |
  2009. SUBSTRINGMATCH |
  2010. '=' |
  2011. INCLUDES |
  2012. DASHMATCH ] S* [ IDENT | STRING ] S*
  2013. ]? ']'
  2014. ;
  2015. pseudo
  2016. /* '::' starts a pseudo-element, ':' a pseudo-class */
  2017. /* Exceptions: :first-line, :first-letter, :before and :after. */
  2018. /* Note that pseudo-elements are restricted to one per selector and */
  2019. /* occur only in the last simple_selector_sequence. */
  2020. : ':' ':'? [ IDENT | functional_pseudo ]
  2021. ;
  2022. functional_pseudo
  2023. : FUNCTION S* expression ')'
  2024. ;
  2025. expression
  2026. /* In CSS3, the expressions are identifiers, strings, */
  2027. /* or of the form "an+b" */
  2028. : [ [ PLUS | '-' | DIMENSION | NUMBER | STRING | IDENT ] S* ]+
  2029. ;
  2030. negation
  2031. : NOT S* negation_arg S* ')'
  2032. ;
  2033. negation_arg
  2034. : type_selector | universal | HASH | class | attrib | pseudo
  2035. ;</pre>
  2036. <h3><a name=lex>10.2. Lexical scanner</a></h3>
  2037. <p>The following is the <a name=x3>tokenizer</a>, written in Flex (see
  2038. <a href="#refsFLEX">[FLEX]</a>) notation. The tokenizer is
  2039. case-insensitive.</p>
  2040. <p>The two occurrences of "\377" represent the highest character
  2041. number that current versions of Flex can deal with (decimal 255). They
  2042. should be read as "\4177777" (decimal 1114111), which is the highest
  2043. possible code point in Unicode/ISO-10646. <a
  2044. href="#refsUNICODE">[UNICODE]</a></p>
  2045. <pre>%option case-insensitive
  2046. ident [-]?{nmstart}{nmchar}*
  2047. name {nmchar}+
  2048. nmstart [_a-z]|{nonascii}|{escape}
  2049. nonascii [^\0-\177]
  2050. unicode \\[0-9a-f]{1,6}(\r\n|[ \n\r\t\f])?
  2051. escape {unicode}|\\[^\n\r\f0-9a-f]
  2052. nmchar [_a-z0-9-]|{nonascii}|{escape}
  2053. num [0-9]+|[0-9]*\.[0-9]+
  2054. string {string1}|{string2}
  2055. string1 \"([^\n\r\f\\"]|\\{nl}|{nonascii}|{escape})*\"
  2056. string2 \'([^\n\r\f\\']|\\{nl}|{nonascii}|{escape})*\'
  2057. invalid {invalid1}|{invalid2}
  2058. invalid1 \"([^\n\r\f\\"]|\\{nl}|{nonascii}|{escape})*
  2059. invalid2 \'([^\n\r\f\\']|\\{nl}|{nonascii}|{escape})*
  2060. nl \n|\r\n|\r|\f
  2061. w [ \t\r\n\f]*
  2062. %%
  2063. [ \t\r\n\f]+ return S;
  2064. "~=" return INCLUDES;
  2065. "|=" return DASHMATCH;
  2066. "^=" return PREFIXMATCH;
  2067. "$=" return SUFFIXMATCH;
  2068. "*=" return SUBSTRINGMATCH;
  2069. {ident} return IDENT;
  2070. {string} return STRING;
  2071. {ident}"(" return FUNCTION;
  2072. {num} return NUMBER;
  2073. "#"{name} return HASH;
  2074. {w}"+" return PLUS;
  2075. {w}"&gt;" return GREATER;
  2076. {w}"," return COMMA;
  2077. {w}"~" return TILDE;
  2078. ":not(" return NOT;
  2079. @{ident} return ATKEYWORD;
  2080. {invalid} return INVALID;
  2081. {num}% return PERCENTAGE;
  2082. {num}{ident} return DIMENSION;
  2083. "&lt;!--" return CDO;
  2084. "--&gt;" return CDC;
  2085. "url("{w}{string}{w}")" return URI;
  2086. "url("{w}([!#$%&*-~]|{nonascii}|{escape})*{w}")" return URI;
  2087. U\+[0-9a-f?]{1,6}(-[0-9a-f]{1,6})? return UNICODE_RANGE;
  2088. \/\*[^*]*\*+([^/*][^*]*\*+)*\/ /* ignore comments */
  2089. . return *yytext;</pre>
  2090. <h2><a name=downlevel>11. Namespaces and down-level clients</a></h2>
  2091. <p>An important issue is the interaction of CSS selectors with XML
  2092. documents in web clients that were produced prior to this
  2093. document. Unfortunately, due to the fact that namespaces must be
  2094. matched based on the URI which identifies the namespace, not the
  2095. namespace prefix, some mechanism is required to identify namespaces in
  2096. CSS by their URI as well. Without such a mechanism, it is impossible
  2097. to construct a CSS style sheet which will properly match selectors in
  2098. all cases against a random set of XML documents. However, given
  2099. complete knowledge of the XML document to which a style sheet is to be
  2100. applied, and a limited use of namespaces within the XML document, it
  2101. is possible to construct a style sheet in which selectors would match
  2102. elements and attributes correctly.</p>
  2103. <p>It should be noted that a down-level CSS client will (if it
  2104. properly conforms to CSS forward compatible parsing rules) ignore all
  2105. <code>@namespace</code> at-rules, as well as all style rules that make
  2106. use of namespace qualified element type or attribute selectors. The
  2107. syntax of delimiting namespace prefixes in CSS was deliberately chosen
  2108. so that down-level CSS clients would ignore the style rules rather
  2109. than possibly match them incorrectly.</p>
  2110. <p>The use of default namespaces in CSS makes it possible to write
  2111. element type selectors that will function in both namespace aware CSS
  2112. clients as well as down-level clients. It should be noted that
  2113. down-level clients may incorrectly match selectors against XML
  2114. elements in other namespaces.</p>
  2115. <p>The following are scenarios and examples in which it is possible to
  2116. construct style sheets which would function properly in web clients
  2117. that do not implement this proposal.</p>
  2118. <ol>
  2119. <li>
  2120. <p>The XML document does not use namespaces.</p>
  2121. <ul>
  2122. <li>In this case, it is obviously not necessary to declare or use
  2123. namespaces in the style sheet. Standard CSS element type and
  2124. attribute selectors will function adequately in a down-level
  2125. client.</li>
  2126. <li>In a CSS namespace aware client, the default behavior of
  2127. element selectors matching without regard to namespace will
  2128. function properly against all elements, since no namespaces are
  2129. present. However, the use of specific element type selectors that
  2130. match only elements that have no namespace ("<code>|name</code>")
  2131. will guarantee that selectors will match only XML elements that do
  2132. not have a declared namespace. </li>
  2133. </ul>
  2134. </li>
  2135. <li>
  2136. <p>The XML document defines a single, default namespace used
  2137. throughout the document. No namespace prefixes are used in element
  2138. names.</p>
  2139. <ul>
  2140. <li>In this case, a down-level client will function as if
  2141. namespaces were not used in the XML document at all. Standard CSS
  2142. element type and attribute selectors will match against all
  2143. elements. </li>
  2144. </ul>
  2145. </li>
  2146. <li>
  2147. <p>The XML document does <b>not</b> use a default namespace, all
  2148. namespace prefixes used are known to the style sheet author, and
  2149. there is a direct mapping between namespace prefixes and namespace
  2150. URIs. (A given prefix may only be mapped to one namespace URI
  2151. throughout the XML document; there may be multiple prefixes mapped
  2152. to the same URI).</p>
  2153. <ul>
  2154. <li>In this case, the down-level client will view and match
  2155. element type and attribute selectors based on their fully
  2156. qualified name, not the local part as outlined in the <a
  2157. href="#typenmsp">Type selectors and Namespaces</a> section. CSS
  2158. selectors may be declared using an escaped colon "<code>\:</code>"
  2159. to describe the fully qualified names, e.g.
  2160. "<code>html\:h1</code>" will match
  2161. <code>&lt;html:h1&gt;</code>. Selectors using the qualified name
  2162. will only match XML elements that use the same prefix. Other
  2163. namespace prefixes used in the XML that are mapped to the same URI
  2164. will not match as expected unless additional CSS style rules are
  2165. declared for them.</li>
  2166. <li>Note that selectors declared in this fashion will
  2167. <em>only</em> match in down-level clients. A CSS namespace aware
  2168. client will match element type and attribute selectors based on
  2169. the name's local part. Selectors declared with the fully
  2170. qualified name will not match (unless there is no namespace prefix
  2171. in the fully qualified name).</li>
  2172. </ul>
  2173. </li>
  2174. </ol>
  2175. <p>In other scenarios: when the namespace prefixes used in the XML are
  2176. not known in advance by the style sheet author; or a combination of
  2177. elements with no namespace are used in conjunction with elements using
  2178. a default namespace; or the same namespace prefix is mapped to
  2179. <em>different</em> namespace URIs within the same document, or in
  2180. different documents; it is impossible to construct a CSS style sheet
  2181. that will function properly against all elements in those documents,
  2182. unless, the style sheet is written using a namespace URI syntax (as
  2183. outlined in this document or similar) and the document is processed by
  2184. a CSS and XML namespace aware client.</p>
  2185. <h2><a name=profiling>12. Profiles</a></h2>
  2186. <p>Each specification using Selectors must define the subset of W3C
  2187. Selectors it allows and excludes, and describe the local meaning of
  2188. all the components of that subset.</p>
  2189. <p>Non normative examples:
  2190. <div class="profile">
  2191. <table class="tprofile">
  2192. <tbody>
  2193. <tr>
  2194. <th class="title" colspan=2>Selectors profile</th></tr>
  2195. <tr>
  2196. <th>Specification</th>
  2197. <td>CSS level 1</td></tr>
  2198. <tr>
  2199. <th>Accepts</th>
  2200. <td>type selectors<br>class selectors<br>ID selectors<br>:link,
  2201. :visited and :active pseudo-classes<br>descendant combinator
  2202. <br>::first-line and ::first-letter pseudo-elements</td></tr>
  2203. <tr>
  2204. <th>Excludes</th>
  2205. <td>
  2206. <p>universal selector<br>attribute selectors<br>:hover and :focus
  2207. pseudo-classes<br>:target pseudo-class<br>:lang() pseudo-class<br>all UI
  2208. element states pseudo-classes<br>all structural
  2209. pseudo-classes<br>negation pseudo-class<br>all
  2210. UI element fragments pseudo-elements<br>::before and ::after
  2211. pseudo-elements<br>child combinators<br>sibling combinators
  2212. <p>namespaces</td></tr>
  2213. <tr>
  2214. <th>Extra constraints</th>
  2215. <td>only one class selector allowed per sequence of simple
  2216. selectors</td></tr></tbody></table><br><br>
  2217. <table class="tprofile">
  2218. <tbody>
  2219. <tr>
  2220. <th class="title" colspan=2>Selectors profile</th></tr>
  2221. <tr>
  2222. <th>Specification</th>
  2223. <td>CSS level 2</td></tr>
  2224. <tr>
  2225. <th>Accepts</th>
  2226. <td>type selectors<br>universal selector<br>attribute presence and
  2227. values selectors<br>class selectors<br>ID selectors<br>:link, :visited,
  2228. :active, :hover, :focus, :lang() and :first-child pseudo-classes
  2229. <br>descendant combinator<br>child combinator<br>adjacent sibling
  2230. combinator<br>::first-line and ::first-letter pseudo-elements<br>::before
  2231. and ::after pseudo-elements</td></tr>
  2232. <tr>
  2233. <th>Excludes</th>
  2234. <td>
  2235. <p>content selectors<br>substring matching attribute
  2236. selectors<br>:target pseudo-classes<br>all UI element
  2237. states pseudo-classes<br>all structural pseudo-classes other
  2238. than :first-child<br>negation pseudo-class<br>all UI element
  2239. fragments pseudo-elements<br>general sibling combinators
  2240. <p>namespaces</td></tr>
  2241. <tr>
  2242. <th>Extra constraints</th>
  2243. <td>more than one class selector per sequence of simple selectors (CSS1
  2244. constraint) allowed</td></tr></tbody></table>
  2245. <p>In CSS, selectors express pattern matching rules that determine which style
  2246. rules apply to elements in the document tree.
  2247. <p>The following selector (CSS level 2) will <b>match</b> all anchors <code>a</code>
  2248. with attribute <code>name</code> set inside a section 1 header <code>h1</code>:
  2249. <pre>h1 a[name]</pre>
  2250. <p>All CSS declarations attached to such a selector are applied to elements
  2251. matching it. </div>
  2252. <div class="profile">
  2253. <table class="tprofile">
  2254. <tbody>
  2255. <tr>
  2256. <th class="title" colspan=2>Selectors profile</th></tr>
  2257. <tr>
  2258. <th>Specification</th>
  2259. <td>STTS 3</td>
  2260. </tr>
  2261. <tr>
  2262. <th>Accepts</th>
  2263. <td>
  2264. <p>type selectors<br>universal selectors<br>attribute selectors<br>class
  2265. selectors<br>ID selectors<br>all structural pseudo-classes<br>
  2266. all combinators
  2267. <p>namespaces</td></tr>
  2268. <tr>
  2269. <th>Excludes</th>
  2270. <td>non-accepted pseudo-classes<br>pseudo-elements<br></td></tr>
  2271. <tr>
  2272. <th>Extra constraints</th>
  2273. <td>some selectors and combinators are not allowed in fragment
  2274. descriptions on the right side of STTS declarations.</td></tr></tbody></table>
  2275. <p>Selectors can be used in STTS 3 in two different
  2276. manners:
  2277. <ol>
  2278. <li>a selection mechanism equivalent to CSS selection mechanism: declarations
  2279. attached to a given selector are applied to elements matching that selector,
  2280. <li>fragment descriptions that appear on the right side of declarations.
  2281. </li></ol></div>
  2282. <h2><a name=Conformance></a>13. Conformance and requirements</h2>
  2283. <p>This section defines conformance with the present specification only.
  2284. <p>The inability of a user agent to implement part of this specification due to
  2285. the limitations of a particular device (e.g., non interactive user agents will
  2286. probably not implement dynamic pseudo-classes because they make no sense without
  2287. interactivity) does not imply non-conformance.
  2288. <p>All specifications reusing Selectors must contain a <a
  2289. href="#profiling">Profile</a> listing the
  2290. subset of Selectors it accepts or excludes, and describing the constraints
  2291. it adds to the current specification.
  2292. <p>Invalidity is caused by a parsing error, e.g. an unrecognized token or a token
  2293. which is not allowed at the current parsing point.
  2294. <p>User agents must observe the rules for handling parsing errors:
  2295. <ul>
  2296. <li>a simple selector containing an undeclared namespace prefix is invalid</li>
  2297. <li>a selector containing an invalid simple selector, an invalid combinator
  2298. or an invalid token is invalid. </li>
  2299. <li>a group of selectors containing an invalid selector is invalid.</li>
  2300. </ul>
  2301. <p>Specifications reusing Selectors must define how to handle parsing
  2302. errors. (In the case of CSS, the entire rule in which the selector is
  2303. used is dropped.)</p>
  2304. <!-- Apparently all these references are out of date:
  2305. <p>Implementations of this specification must behave as
  2306. "recipients of text data" as defined by <a href="#refsCWWW">[CWWW]</a>
  2307. when parsing selectors and attempting matches. (In particular,
  2308. implementations must assume the data is normalized and must not
  2309. normalize it.) Normative rules for matching strings are defined in
  2310. <a href="#refsCWWW">[CWWW]</a> and <a
  2311. href="#refsUNICODE">[UNICODE]</a> and apply to implementations of this
  2312. specification.</p>-->
  2313. <h2><a name=Tests></a>14. Tests</h2>
  2314. <p>This specification has <a
  2315. href="http://www.w3.org/Style/CSS/Test/CSS3/Selectors/current/">a test
  2316. suite</a> allowing user agents to verify their basic conformance to
  2317. the specification. This test suite does not pretend to be exhaustive
  2318. and does not cover all possible combined cases of Selectors.</p>
  2319. <h2><a name=ACKS></a>15. Acknowledgements</h2>
  2320. <p>The CSS working group would like to thank everyone who has sent
  2321. comments on this specification over the years.</p>
  2322. <p>The working group would like to extend special thanks to Donna
  2323. McManus, Justin Baker, Joel Sklar, and Molly Ives Brower who perfermed
  2324. the final editorial review.</p>
  2325. <h2><a name=references>16. References</a></h2>
  2326. <dl class="refs">
  2327. <dt>[CSS1]
  2328. <dd><a name=refsCSS1></a> Bert Bos, H&aring;kon Wium Lie; "<cite>Cascading Style Sheets, level 1</cite>", W3C Recommendation, 17 Dec 1996, revised 11 Jan 1999
  2329. <dd>(<code><a href="http://www.w3.org/TR/REC-CSS1">http://www.w3.org/TR/REC-CSS1</a></code>)
  2330. <dt>[CSS21]
  2331. <dd><a name=refsCSS21></a> Bert Bos, Tantek &Ccedil;elik, Ian Hickson, H&aring;kon Wium Lie, editors; "<cite>Cascading Style Sheets, level 2 revision 1</cite>", W3C Working Draft, 13 June 2005
  2332. <dd>(<code><a href="http://www.w3.org/TR/CSS21">http://www.w3.org/TR/CSS21</a></code>)
  2333. <dt>[CWWW]
  2334. <dd><a name=refsCWWW></a> Martin J. D&uuml;rst, Fran&ccedil;ois Yergeau, Misha Wolf, Asmus Freytag, Tex Texin, editors; "<cite>Character Model for the World Wide Web</cite>", W3C Recommendation, 15 February 2005
  2335. <dd>(<code><a href="http://www.w3.org/TR/charmod/">http://www.w3.org/TR/charmod/</a></code>)
  2336. <dt>[FLEX]
  2337. <dd><a name="refsFLEX"></a> "<cite>Flex: The Lexical Scanner Generator</cite>", Version 2.3.7, ISBN 1882114213
  2338. <dt>[HTML4]
  2339. <dd><a name="refsHTML4"></a> Dave Ragget, Arnaud Le Hors, Ian Jacobs, editors; "<cite>HTML 4.01 Specification</cite>", W3C Recommendation, 24 December 1999
  2340. <dd>(<a href="http://www.w3.org/TR/html4/"><code>http://www.w3.org/TR/html4/</code></a>)
  2341. <dt>[MATH]
  2342. <dd><a name="refsMATH"></a> Patrick Ion, Robert Miner, editors; "<cite>Mathematical Markup Language (MathML) 1.01</cite>", W3C Recommendation, revision of 7 July 1999
  2343. <dd>(<code><a href="http://www.w3.org/TR/REC-MathML/">http://www.w3.org/TR/REC-MathML/</a></code>)
  2344. <dt>[RFC3066]
  2345. <dd><a name="refsRFC3066"></a> H. Alvestrand; "<cite>Tags for the Identification of Languages</cite>", Request for Comments 3066, January 2001
  2346. <dd>(<a href="http://www.ietf.org/rfc/rfc3066.txt"><code>http://www.ietf.org/rfc/rfc3066.txt</code></a>)
  2347. <dt>[STTS]
  2348. <dd><a name=refsSTTS></a> Daniel Glazman; "<cite>Simple Tree Transformation Sheets 3</cite>", Electricit&eacute; de France, submission to the W3C, 11 November 1998
  2349. <dd>(<code><a href="http://www.w3.org/TR/NOTE-STTS3">http://www.w3.org/TR/NOTE-STTS3</a></code>)
  2350. <dt>[SVG]
  2351. <dd><a name="refsSVG"></a> Jon Ferraiolo, &#34276;&#27810; &#28147;, Dean Jackson, editors; "<cite>Scalable Vector Graphics (SVG) 1.1 Specification</cite>", W3C Recommendation, 14 January 2003
  2352. <dd>(<code><a href="http://www.w3.org/TR/SVG/">http://www.w3.org/TR/SVG/</a></code>)
  2353. <dt>[UNICODE]</dt>
  2354. <dd><a name="refsUNICODE"></a> <cite><a
  2355. href="http://www.unicode.org/versions/Unicode4.1.0/">The Unicode Standard, Version 4.1</a></cite>, The Unicode Consortium. Boston, MA, Addison-Wesley, March 2005. ISBN 0-321-18578-1, as amended by <a href="http://www.unicode.org/versions/Unicode4.0.1/">Unicode 4.0.1</a> and <a href="http://www.unicode.org/versions/Unicode4.1.0/">Unicode 4.1.0</a>.
  2356. <dd>(<code><a href="http://www.unicode.org/versions/">http://www.unicode.org/versions/</a></code>)</dd>
  2357. <dt>[XML10]
  2358. <dd><a name="refsXML10"></a> Tim Bray, Jean Paoli, C. M. Sperberg-McQueen, Eve Maler, Fran&ccedil;ois Yergeau, editors; "<cite>Extensible Markup Language (XML) 1.0 (Third Edition)</cite>", W3C Recommendation, 4 February 2004
  2359. <dd>(<a href="http://www.w3.org/TR/REC-xml/"><code>http://www.w3.org/TR/REC-xml/</code></a>)
  2360. <dt>[XMLNAMES]
  2361. <dd><a name="refsXMLNAMES"></a> Tim Bray, Dave Hollander, Andrew Layman, editors; "<cite>Namespaces in XML</cite>", W3C Recommendation, 14 January 1999
  2362. <dd>(<a href="http://www.w3.org/TR/REC-xml-names/"><code>http://www.w3.org/TR/REC-xml-names/</code></a>)
  2363. <dt>[YACC]
  2364. <dd><a name="refsYACC"></a> S. C. Johnson; "<cite>YACC &mdash; Yet another compiler compiler</cite>", Technical Report, Murray Hill, 1975
  2365. </dl>
  2366. </body>
  2367. </html>