dom-query.html 117 KB

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