jslib-attr-jquery-2.x.html 115 KB

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