jslib-event-jquery.html 115 KB

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