├── package.json ├── LICENSE ├── smoothscroll.min.js ├── README.md ├── smoothscroll.js └── example.html /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "smoothscroll", 3 | "version": "0.4.0", 4 | "description": "A teeny tiny smooth scroll script with ease-in-out effect and no jQuery.", 5 | "main": "smoothscroll.js", 6 | "author": "Alice Lieutier (http://alice.lieutier.me)", 7 | "license": "MIT", 8 | "homepage": "https://github.com/alicelieutier/smoothScroll#readme", 9 | "repository": "https://github.com/alicelieutier/smoothScroll.git", 10 | "keywords": [ 11 | "smooth scroll", 12 | "scroll", 13 | "no jQuery", 14 | "anchor" 15 | ], 16 | "scripts": { 17 | "test": "echo \"Error: no test specified\" && exit 1" 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2013 Alice Lieutier 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /smoothscroll.min.js: -------------------------------------------------------------------------------- 1 | (function(root,smoothScroll){"use strict";if(typeof define==="function"&&define.amd){define(smoothScroll)}else if(typeof exports==="object"&&typeof module==="object"){module.exports=smoothScroll()}else{root.smoothScroll=smoothScroll()}})(this,function(){"use strict";if(typeof window!=="object")return;if(document.querySelectorAll===void 0||window.pageYOffset===void 0||history.pushState===void 0){return}var getTop=function(element,start){if(element.nodeName==="HTML")return-start;return element.getBoundingClientRect().top+start};var easeInOutCubic=function(t){return t<.5?4*t*t*t:(t-1)*(2*t-2)*(2*t-2)+1};var position=function(start,end,elapsed,duration){if(elapsed>duration)return end;return start+(end-start)*easeInOutCubic(elapsed/duration)};var smoothScroll=function(el,duration,callback,context){duration=duration||500;context=context||window;var start=context.scrollTop||window.pageYOffset;if(typeof el==="number"){var end=parseInt(el)}else{var end=getTop(el,start)}var clock=Date.now();var requestAnimationFrame=window.requestAnimationFrame||window.mozRequestAnimationFrame||window.webkitRequestAnimationFrame||function(fn){window.setTimeout(fn,15)};var step=function(){var elapsed=Date.now()-clock;if(context!==window){context.scrollTop=position(start,end,elapsed,duration)}else{window.scroll(0,position(start,end,elapsed,duration))}if(elapsed>duration){if(typeof callback==="function"){callback(el)}}else{requestAnimationFrame(step)}};step()};var linkHandler=function(ev){if(!ev.defaultPrevented){ev.preventDefault();if(location.hash!==this.hash)window.history.pushState(null,null,this.hash);var node=document.getElementById(this.hash.substring(1));if(!node)return;smoothScroll(node,500,function(el){location.replace("#"+el.id)})}};document.addEventListener("DOMContentLoaded",function(){var internal=document.querySelectorAll('a[href^="#"]:not([href="#"])'),a;for(var i=internal.length;a=internal[--i];){a.addEventListener("click",linkHandler,false)}});return smoothScroll}); 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | smoothScroll 2 | ============ 3 | 4 | A teeny tiny, standard compliant, smooth scroll script with ease-in-out effect and no dependancy. 5 | 6 | smoothScroll will tie all your internal links to a handler that will produce a smooth scroll to their target instead of an instant jump. It also returns an API that you can use to call a smooth scroll yourself. 7 | 8 | This works in Firefox, Chrome, IE10, Opera and Safari. 9 | Unsupported browsers would just use the normal internal link behaviour. 10 | 11 | 12 | How to use 13 | - 14 | Just include smoothscroll inside your page, like this: 15 | 16 | 17 | 18 | All your internal links will be tied to a smooth scroll. 19 | If you want to call a smooth scroll from your code, you can now use the API by calling: 20 | 21 | `window.smoothScroll(target, duration, callback, context)` 22 | 23 | where: 24 | * `target` is a `HTMLElement Object` from your document that you want to scroll to, or a numeric position on the page 25 | * `duration` is the total duration of the scroll (optional, defaults to 500ms) 26 | * `callback` is a function to be executed when the scrolling is over (optional) 27 | * `context` is the scrolling context (optional, defaults to window, can be any `HTMLElement Object`) 28 | 29 | Alternatively, you can install smoothscroll as a dependency using npm: 30 | 31 | ``` 32 | npm install --save smoothscroll 33 | ``` 34 | 35 | Example usage as a module, binding to a custom element: 36 | 37 | ```javascript 38 | var smoothScroll = require('smoothscroll'); 39 | 40 | var exampleBtn = document.querySelector('.example-button'); 41 | var exampleDestination = document.querySelector('.example-destination'); 42 | 43 | // This function can easily be an onClick handler in React components 44 | var handleClick = function(event) { 45 | event.preventDefault(); 46 | 47 | smoothScroll(exampleDestination); 48 | }; 49 | 50 | exampleBtn.addEventListener('click', handleClick); 51 | ``` 52 | 53 | smoothscroll.js 54 | - 55 | Here are some indications if you want to tweak the code to fit your needs: 56 | 57 | There is an ease-in-out type timing function. You can change it quite easily in the code. Here is where I found the one I use: 58 | - http://blog.greweb.fr/2012/02/bezier-curve-based-easing-functions-from-concept-to-implementation/ 59 | 60 | You can also change the default duration of a scroll, which is 500ms by default. 61 | 62 | My code is **heavily** commented so you shoudn't lose yourself too much. 63 | 64 | Example 65 | - 66 | The example.html file is basically the script applied to a w3c page. I just changed the style so the table of content is fixed to the left. 67 | 68 | Check out the result. Wouldn't it be great if all w3c specs where that easy to navigate in? 69 | 70 | Similar scripts 71 | - 72 | While I was looking for a name for this script, I found these sites. If this script is not what you need, you might have more luck there: 73 | - http://www.itnewb.com/tutorial/Creating-the-Smooth-Scroll-Effect-with-JavaScript 74 | - http://www.kryogenix.org/code/browser/smoothscroll (best cross browser compatibility) 75 | 76 | License 77 | - 78 | This library is released under the MIT license. 79 | -------------------------------------------------------------------------------- /smoothscroll.js: -------------------------------------------------------------------------------- 1 | (function (root, smoothScroll) { 2 | 'use strict'; 3 | 4 | // Support RequireJS and CommonJS/NodeJS module formats. 5 | // Attach smoothScroll to the `window` when executed as a 28 | 29 | 30 | 31 |
32 |

W3C 34 | 35 |

Selectors Level 3

36 | 37 |

W3C Recommendation 38 | 29 September 2011

39 | 40 |
41 |
This version: 42 | 43 |
44 | http://www.w3.org/TR/2011/REC-css3-selectors-20110929/ 45 | 47 | 48 |
Latest version: 49 | 50 |
51 | http://www.w3.org/TR/css3-selectors/ 52 | 53 |
Latest Selectors specification: 54 | 55 |
56 | http://www.w3.org/TR/selectors/ 57 | 58 |
Previous version: 59 | 60 |
61 | http://www.w3.org/TR/2009/PR-css3-selectors-20091215/ 62 | 63 |
Editors: 64 | 65 |
Tantek Çelik (Invited Expert) 67 | 68 |
Elika J. Etemad 70 | (Invited Expert) 71 | 72 |
Daniel Glazman (Disruptive 73 | Innovations SARL) 74 | 75 |
Ian 76 | Hickson (Google) 78 | 79 |
Peter Linss (former editor, Netscape/AOL) 82 | 83 |
John Williams (former editor, Quark, Inc.) 85 |
86 | 87 |

Please refer to the errata for this document, which may include some normative corrections.

88 | 89 |

See also translations.

90 | 91 | 92 | 93 | 94 |
95 |
96 | 97 |

Abstract

98 | 99 |

Selectors are patterns that match against elements in a tree, 100 | and as such form one of several technologies that can be used to select 101 | nodes in an XML document. Selectors have been optimized for use with HTML 102 | and XML, and are designed to be usable in performance-critical code. 103 | 104 |

CSS (Cascading Style 105 | Sheets) is a language for describing the rendering of HTML and XML documents on screen, on 108 | paper, in speech, etc. CSS uses Selectors for binding style properties to 109 | elements in the document. 110 | 111 |

This document describes the selectors that already exist in CSS1 [CSS1] and CSS2 [CSS21], and further introduces new 116 | selectors for CSS3 and other languages 117 | that may need them. 118 | 119 |

Selectors define the following function: 120 | 121 |

expression ∗ element → boolean
122 | 123 |

That is, given an element and a selector, this specification defines 124 | whether that element matches the selector. 125 | 126 |

These expressions can also be used, for instance, to select a set of 127 | elements, or a single element from a set of elements, by evaluating the 128 | expression across all the elements in a subtree. STTS (Simple Tree Transformation 130 | Sheets), a language for transforming XML trees, uses this mechanism. [STTS3] 132 | 133 |

Status of this document

134 | 135 | 136 |

This section describes the status of this document at the time of 137 | its publication. Other documents may supersede this document. A list of 138 | current W3C publications and the latest revision of this technical report 139 | can be found in the W3C technical reports 140 | index at http://www.w3.org/TR/. 141 | 142 |

This document was produced by the CSS Working Group as a Proposed 145 | Recommendation. 146 | 147 |

A W3C Recommendation is a mature document that has been widely 148 | reviewed and has been shown to be implementable. W3C encourages everybody 149 | to implement this specification. Comments may be sent to the (archived) public 151 | mailing list www-style@w3.org 153 | (see instructions). When 154 | sending e-mail, please put the text “css3-selectors” in the 155 | subject, preferably like this: “[css3-selectors] 156 | …summary of comment…” 157 | 158 |

This document has been reviewed by W3C Members, by software developers, and by other W3C groups and interested parties, and is endorsed by the Director as a W3C Recommendation. It is a stable document and may be used as reference material or cited from another document. W3C's role in making the Recommendation is to draw attention to the specification and to promote its widespread deployment. This enhances the functionality and interoperability of the Web.

159 | 160 |

This document was produced by a group operating under the 5 February 162 | 2004 W3C Patent Policy. W3C maintains a public list of any patent disclosures made in 165 | connection with the deliverables of the group; that page also includes 166 | instructions for disclosing a patent. An individual who has actual 167 | knowledge of a patent which the individual believes contains Essential 169 | Claim(s) must disclose the information in accordance with 171 | section 6 of the W3C Patent Policy.

172 | 173 | 174 |

A separate 176 | implementation report contains a test suite and shows several 177 | implementations of the specification. 178 | 179 |

This document is the same as the previous, Proposed Recommendation 180 | version, except for editorial changes to the front matter, and updating of references. 181 | 182 | 183 |

Table of Contents

184 | 185 | 186 | 401 | 402 | 403 |

1. Introduction

404 | 405 |

Selectors Level 1 and Selectors Level 2 are defined as the subsets of 406 | selector functionality defined in the CSS1 and CSS2.1 specifications, 409 | respectively. 410 | 411 |

1.1. Dependencies

412 | 413 |

Some features of this specification are specific to CSS, or have 414 | particular limitations or rules specific to CSS. In this specification, 415 | these have been described in terms of CSS2.1. [CSS21] 417 | 418 |

1.2. Terminology

419 | 420 |

All of the text of this specification is normative except examples, 421 | notes, and sections explicitly marked as non-normative. 422 | 423 |

Additional terminology is defined in the Definitions 425 | section of [CSS21]. 426 | Examples of document source code and fragments are given in XML [[XML10] 427 | or HTML [[HTML40]] syntax. 428 | 429 |

1.3. Changes from CSS2

430 | 431 |

This section is non-normative. 432 | 433 |

The main differences between the selectors in CSS2 and those in 434 | Selectors are: 435 | 436 |

466 | 467 |

2. Selectors

468 | 469 |

This section is non-normative, as it merely summarizes the following 470 | sections. 471 | 472 |

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

Selectors may range from simple element names to rich contextual 478 | representations. 479 | 480 |

The following table summarizes the Selector syntax: 481 | 482 | 483 | 484 | 485 | 494 | 495 | 504 | 513 | 523 | 534 | 545 | 556 | 567 | 578 | 589 | 599 | 609 | 620 | 630 | 641 | 651 | 661 | 671 | 681 | 691 | 701 | 712 | 724 | 736 | 745 | 755 | 767 | 780 | 790 | 800 | 810 | 820 | 830 | 839 | 848 | 858 | 867 | 877 |
Pattern 486 | 487 | Meaning 488 | 489 | Described in section 490 | 491 | First defined in CSS level 492 | 493 |
* 496 | 497 | any element 498 | 499 | Universal selector 500 | 501 | 2 502 | 503 |
E 505 | 506 | an element of type E 507 | 508 | Type selector 509 | 510 | 1 511 | 512 |
E[foo] 514 | 515 | an E element with a "foo" attribute 516 | 517 | Attribute 518 | selectors 519 | 520 | 2 521 | 522 |
E[foo="bar"] 524 | 525 | an E element whose "foo" attribute value is exactly 526 | equal to "bar" 527 | 528 | Attribute 529 | selectors 530 | 531 | 2 532 | 533 |
E[foo~="bar"] 535 | 536 | an E element whose "foo" attribute value is a list of 537 | whitespace-separated values, one of which is exactly equal to "bar" 538 | 539 | Attribute 540 | selectors 541 | 542 | 2 543 | 544 |
E[foo^="bar"] 546 | 547 | an E element whose "foo" attribute value begins 548 | exactly with the string "bar" 549 | 550 | Attribute 551 | selectors 552 | 553 | 3 554 | 555 |
E[foo$="bar"] 557 | 558 | an E element whose "foo" attribute value ends exactly 559 | with the string "bar" 560 | 561 | Attribute 562 | selectors 563 | 564 | 3 565 | 566 |
E[foo*="bar"] 568 | 569 | an E element whose "foo" attribute value contains the 570 | substring "bar" 571 | 572 | Attribute 573 | selectors 574 | 575 | 3 576 | 577 |
E[foo|="en"] 579 | 580 | an E element whose "foo" attribute has a 581 | hyphen-separated list of values beginning (from the left) with "en" 582 | 583 | Attribute 584 | selectors 585 | 586 | 2 587 | 588 |
E:root 590 | 591 | an E element, root of the document 592 | 593 | Structural 594 | pseudo-classes 595 | 596 | 3 597 | 598 |
E:nth-child(n) 600 | 601 | an E element, the n-th child of its parent 602 | 603 | Structural 604 | pseudo-classes 605 | 606 | 3 607 | 608 |
E:nth-last-child(n) 610 | 611 | an E element, the n-th child of its parent, counting 612 | from the last one 613 | 614 | Structural 615 | pseudo-classes 616 | 617 | 3 618 | 619 |
E:nth-of-type(n) 621 | 622 | an E element, the n-th sibling of its type 623 | 624 | Structural 625 | pseudo-classes 626 | 627 | 3 628 | 629 |
E:nth-last-of-type(n) 631 | 632 | an E element, the n-th sibling of its type, counting 633 | from the last one 634 | 635 | Structural 636 | pseudo-classes 637 | 638 | 3 639 | 640 |
E:first-child 642 | 643 | an E element, first child of its parent 644 | 645 | Structural 646 | pseudo-classes 647 | 648 | 2 649 | 650 |
E:last-child 652 | 653 | an E element, last child of its parent 654 | 655 | Structural 656 | pseudo-classes 657 | 658 | 3 659 | 660 |
E:first-of-type 662 | 663 | an E element, first sibling of its type 664 | 665 | Structural 666 | pseudo-classes 667 | 668 | 3 669 | 670 |
E:last-of-type 672 | 673 | an E element, last sibling of its type 674 | 675 | Structural 676 | pseudo-classes 677 | 678 | 3 679 | 680 |
E:only-child 682 | 683 | an E element, only child of its parent 684 | 685 | Structural 686 | pseudo-classes 687 | 688 | 3 689 | 690 |
E:only-of-type 692 | 693 | an E element, only sibling of its type 694 | 695 | Structural 696 | pseudo-classes 697 | 698 | 3 699 | 700 |
E:empty 702 | 703 | an E element that has no children (including text 704 | nodes) 705 | 706 | Structural 707 | pseudo-classes 708 | 709 | 3 710 | 711 |
E:link
713 | E:visited 714 | 715 |
an E element being the source anchor of a hyperlink of 716 | which the target is not yet visited (:link) or already visited 717 | (:visited) 718 | 719 | The link pseudo-classes 720 | 721 | 1 722 | 723 |
E:active
725 | E:hover
726 | E:focus 727 | 728 |
an E element during certain user actions 729 | 730 | The user action 731 | pseudo-classes 732 | 733 | 1 and 2 734 | 735 |
E:target 737 | 738 | an E element being the target of the referring URI 739 | 740 | The target pseudo-class 741 | 742 | 3 743 | 744 |
E:lang(fr) 746 | 747 | an element of type E in language "fr" (the document 748 | language specifies how language is determined) 749 | 750 | The :lang() pseudo-class 751 | 752 | 2 753 | 754 |
E:enabled
756 | E:disabled 757 | 758 |
a user interface element E which is enabled or 759 | disabled 760 | 761 | The UI element states 762 | pseudo-classes 763 | 764 | 3 765 | 766 |
E:checked 768 | 769 | a user interface element E which is 770 | checked (for instance a 772 | radio-button or checkbox) 773 | 774 | The UI element states 775 | pseudo-classes 776 | 777 | 3 778 | 779 |
E::first-line 781 | 782 | the first formatted line of an E element 783 | 784 | The ::first-line 785 | pseudo-element 786 | 787 | 1 788 | 789 |
E::first-letter 791 | 792 | the first formatted letter of an E element 793 | 794 | The ::first-letter 795 | pseudo-element 796 | 797 | 1 798 | 799 |
E::before 801 | 802 | generated content before an E element 803 | 804 | The ::before 805 | pseudo-element 806 | 807 | 2 808 | 809 |
E::after 811 | 812 | generated content after an E element 813 | 814 | The ::after 815 | pseudo-element 816 | 817 | 2 818 | 819 |
E.warning 821 | 822 | an E element whose class is "warning" (the document 823 | language specifies how class is determined). 824 | 825 | Class selectors 826 | 827 | 1 828 | 829 |
E#myid 831 | 832 | an E element with ID equal to "myid". 833 | 834 | ID selectors 835 | 836 | 1 837 | 838 |
E:not(s) 840 | 841 | an E element that does not match simple selector s 842 | 843 | Negation pseudo-class 844 | 845 | 3 846 | 847 |
E F 849 | 850 | an F element descendant of an E element 851 | 852 | Descendant 853 | combinator 854 | 855 | 1 856 | 857 |
E > F 859 | 860 | an F element child of an E element 861 | 862 | Child combinator 863 | 864 | 2 865 | 866 |
E + F 868 | 869 | an F element immediately preceded by an E element 870 | 871 | Adjacent 872 | sibling combinator 873 | 874 | 2 875 | 876 |
E ~ F 878 | 879 | an F element preceded by an E element 880 | 881 | General 882 | sibling combinator 883 | 884 | 3 885 |
886 | 887 |

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

3. Case sensitivity

891 | 892 |

All Selectors syntax is case-insensitive within the ASCII range (i.e. 893 | [a-z] and [A-Z] are equivalent), except for parts that are not under the 894 | control of Selectors. The case sensitivity of document language element 895 | names, attribute names, and attribute values in selectors depends on the 896 | document language. For example, in HTML, element names are 897 | case-insensitive, but in XML, they are case-sensitive. Case sensitivity of 898 | namespace prefixes is defined in [CSS3NAMESPACE]. 900 | 901 |

4. Selector syntax

902 | 903 |

A selector is a chain of one or more sequences of simple selectors separated by combinators. One pseudo-element may be appended to the last 907 | sequence of simple selectors in a selector. 908 | 909 |

A sequence of 910 | simple selectors is a chain of simple selectors that are not separated 912 | by a combinator. It always begins with a type selector or a universal selector. No other type selector 915 | or universal selector is allowed in the sequence. 916 | 917 |

A simple selector is either a type selector, universal selector, attribute selector, class selector, ID 923 | selector, or pseudo-class. 924 | 925 |

Combinators are: whitespace, 926 | "greater-than sign" (U+003E, >), "plus 927 | sign" (U+002B, +) and "tilde" (U+007E, 928 | ~). White space may appear between a combinator and the 929 | simple selectors around it. Only the characters 930 | "space" (U+0020), "tab" (U+0009), "line feed" (U+000A), "carriage return" 931 | (U+000D), and "form feed" (U+000C) can occur in whitespace. Other 932 | space-like characters, such as "em-space" (U+2003) and "ideographic space" 933 | (U+3000), are never part of whitespace. 934 | 935 |

The elements of a document tree that are represented by a selector are 936 | the subjects of the 937 | selector. A selector consisting of a single sequence of simple 938 | selectors represents any element satisfying its requirements. Prepending 939 | another sequence of simple selectors and a combinator to a sequence 940 | imposes additional matching constraints, so the subjects of a selector are 941 | always a subset of the elements represented by the last sequence of simple 942 | selectors. 943 | 944 |

An empty selector, containing no sequence of simple selectors and no 945 | pseudo-element, is an invalid selector. 946 | 947 |

Characters in Selectors can be escaped with a backslash according to the 948 | same escaping 949 | rules as CSS. [CSS21]. 951 | 952 |

Certain selectors support namespace prefixes. The mechanism by 953 | which namespace prefixes are declared should be 954 | specified by the language that uses Selectors. If the language does not 955 | specify a namespace prefix declaration mechanism, then no prefixes are 956 | declared. In CSS, namespace prefixes are declared with the @namespace 958 | rule. [CSS3NAMESPACE] 960 | 961 |

5. Groups of selectors

962 | 963 |

A comma-separated list of selectors represents the union of all elements 964 | selected by each of the individual selectors in the list. (A comma is 965 | U+002C.) For example, in CSS when several selectors share the same 966 | declarations, they may be grouped into a comma-separated list. White space 967 | may appear before and/or after the comma. 968 | 969 |

970 |

CSS example:

971 | 972 |

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

974 | 975 |
h1 { font-family: sans-serif }
 976 | h2 { font-family: sans-serif }
 977 | h3 { font-family: sans-serif }
978 | 979 |

is equivalent to:

980 | 981 |
h1, h2, h3 { font-family: sans-serif }
982 |
983 | 984 |

Warning: the equivalence is true in this example 985 | because all the selectors are valid selectors. If just one of these 986 | selectors were invalid, the entire group of selectors would be invalid. 987 | This would invalidate the rule for all three heading elements, whereas in 988 | the former case only one of the three individual heading rules would be 989 | invalidated. 990 | 991 |

992 |

Invalid CSS example:

993 | 994 |
h1 { font-family: sans-serif }
 995 | h2..foo { font-family: sans-serif }
 996 | h3 { font-family: sans-serif }
997 | 998 |

is not equivalent to:

999 | 1000 |
h1, h2..foo, h3 { font-family: sans-serif }
1001 | 1002 |

because the above selector (h1, h2..foo, h3) is entirely 1003 | invalid and the entire style rule is dropped. (When the selectors are not 1004 | grouped, only the rule for h2..foo is dropped.)

1005 |
1006 | 1007 |

6. Simple selectors

1008 | 1009 |

6.1. Type selector

1010 | 1011 |

A type selector is the name of a document 1012 | language element type written using the syntax of CSS qualified 1014 | names [CSS3NAMESPACE]. A type 1016 | selector represents an instance of the element type in the document tree. 1017 | 1018 |

1019 |

Example:

1020 | 1021 |

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

1023 | 1024 |
h1
1025 |
1026 | 1027 |

6.1.1. Type selectors and 1028 | namespaces

1029 | 1030 |

Type selectors allow an optional namespace component: a namespace prefix 1031 | that has been previously declared may be prepended 1032 | to the element name separated by the namespace separator "vertical 1033 | bar" (U+007C, |). (See, e.g., [XML-NAMES] for the use of 1035 | namespaces in XML.) 1036 | 1037 |

The namespace component may be left empty (no prefix before the 1038 | namespace separator) to indicate that the selector is only to represent 1039 | elements with no namespace. 1040 | 1041 |

An asterisk may be used for the namespace prefix, indicating that the 1042 | selector represents elements in any namespace (including elements with no 1043 | namespace). 1044 | 1045 |

Element type selectors that have no namespace component (no namespace 1046 | separator) represent elements without regard to the element's namespace 1047 | (equivalent to "*|") unless a default namespace has been declared for namespaced selectors (e.g. in CSS, in the 1049 | style sheet). If a default namespace has been declared, such selectors 1050 | will represent only elements in the default namespace. 1051 | 1052 |

A type selector containing a namespace prefix that has not been 1053 | previously declared for namespaced selectors is an 1054 | invalid selector. 1055 | 1056 |

In a namespace-aware client, the name part of element type selectors 1057 | (the part after the namespace separator, if it is present) will only match 1058 | against the local part of 1060 | the element's qualified 1062 | name. 1063 | 1064 |

In summary: 1065 | 1066 |

1067 |
ns|E 1068 | 1069 |
elements with name E in namespace ns 1070 | 1071 |
*|E 1072 | 1073 |
elements with name E in any namespace, including those without a 1074 | namespace 1075 | 1076 |
|E 1077 | 1078 |
elements with name E without a namespace 1079 | 1080 |
E 1081 | 1082 |
if no default namespace has been declared for 1083 | selectors, this is equivalent to *|E. Otherwise it is equivalent to ns|E 1084 | where ns is the default namespace. 1085 |
1086 | 1087 |
1088 |

CSS examples:

1089 | 1090 |
@namespace foo url(http://www.example.com);
1091 |  foo|h1 { color: blue }  /* first rule */
1092 |  foo|* { color: yellow } /* second rule */
1093 |  |h1 { color: red }      /* ...*/
1094 |  *|h1 { color: green }
1095 |  h1 { color: green }
1096 | 1097 |

The first rule (not counting the @namespace at-rule) will 1098 | match only h1 elements in the "http://www.example.com" 1099 | namespace.

1100 | 1101 |

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

1103 | 1104 |

The third rule will match only h1 elements with no 1105 | namespace.

1106 | 1107 |

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

1109 | 1110 |

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

1112 |
1113 | 1114 |

6.2. Universal selector

1115 | 1116 |

The universal selector, written as a 1117 | CSS qualified 1118 | name [CSS3NAMESPACE] with an 1120 | asterisk (* U+002A) as the local name, represents the 1121 | qualified name of any element type. It represents any single element in 1122 | the document tree in any namespace (including those without a namespace) 1123 | if no default namespace has been specified for selectors. If a default 1124 | namespace has been specified, see Universal selector 1125 | and Namespaces below. 1126 | 1127 |

If a universal selector represented by * (i.e. without a 1128 | namespace prefix) is not the only component of a sequence of simple selectors selectors or is 1130 | immediately followed by a pseudo-element, 1131 | then the * may be omitted and the universal selector's 1132 | presence implied. 1133 | 1134 |

1135 |

Examples:

1136 | 1137 | 1145 |
1146 | 1147 |

Note: it is recommended that the 1148 | * not be omitted, because it decreases the potential 1149 | confusion between, for example, div 1150 | :first-child and div:first-child. Here, div *:first-child is more readable. 1153 | 1154 |

6.2.1. Universal selector and 1155 | namespaces

1156 | 1157 |

The universal selector allows an optional namespace component. It is 1158 | used as follows: 1159 | 1160 |

1161 |
ns|* 1162 | 1163 |
all elements in namespace ns 1164 | 1165 |
*|* 1166 | 1167 |
all elements 1168 | 1169 |
|* 1170 | 1171 |
all elements without a namespace 1172 | 1173 |
* 1174 | 1175 |
if no default namespace has been specified, this is equivalent to *|*. 1176 | Otherwise it is equivalent to ns|* where ns is the default namespace. 1177 |
1178 | 1179 |

A universal selector containing a namespace prefix that has not been 1180 | previously declared is an invalid selector. 1182 | 1183 |

6.3. Attribute 1184 | selectors

1185 | 1186 |

Selectors allow the representation of an element's attributes. When a 1187 | selector is used as an expression to match against an element, attribute 1188 | selectors must be considered to match an element if that element has an 1189 | attribute that matches the attribute represented by the attribute 1190 | selector. 1191 | 1192 |

6.3.1. Attribute 1193 | presence and value selectors

1194 | 1195 |

CSS2 introduced four attribute selectors: 1196 | 1197 |

1198 |
[att] 1199 | 1200 |
Represents an element with the att attribute, whatever 1201 | the value of the attribute. 1202 | 1203 |
[att=val] 1204 | 1205 |
Represents an element with the att attribute whose value 1206 | is exactly "val". 1207 | 1208 |
[att~=val] 1209 | 1210 |
Represents an element with the att attribute whose value 1211 | is a whitespace-separated list of words, one of 1212 | which is exactly "val". If "val" contains whitespace, it will never 1213 | represent anything (since the words are separated by spaces). 1214 | Also if "val" is the empty string, it will never represent anything. 1215 | 1216 |
[att|=val] 1217 | 1218 |
Represents an element with the att attribute, its value 1219 | either being exactly "val" or beginning with "val" immediately followed 1220 | by "-" (U+002D). This is primarily intended to allow language subcode 1221 | matches (e.g., the hreflang attribute on the a 1222 | element in HTML) as described in BCP 47 ([BCP47]) or its successor. For 1224 | lang (or xml:lang) language subcode matching, 1225 | please see the :lang 1226 | pseudo-class. 1227 |
1228 | 1229 |

Attribute values must be CSS identifiers 1231 | or strings. 1232 | [CSS21] The 1233 | case-sensitivity of attribute names and values in selectors depends on the 1234 | document language. 1235 | 1236 |

1237 |

Examples:

1238 | 1239 |

The following attribute selector represents an h1 element 1240 | that carries the title attribute, whatever its value:

1241 | 1242 |
h1[title]
1243 | 1244 |

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

1247 | 1248 |
span[class="example"]
1249 | 1250 |

Multiple attribute selectors can be used to represent several 1251 | attributes of an element, or several conditions on the same attribute. 1252 | Here, the selector represents a span element whose 1253 | hello attribute has exactly the value "Cleveland" and whose 1254 | goodbye attribute has exactly the value "Columbus":

1255 | 1256 |
span[hello="Cleveland"][goodbye="Columbus"]
1257 | 1258 |

The following CSS rules illustrate the differences between "=" and 1259 | "~=". The first selector would match, for example, an a 1260 | element with the value "copyright copyleft copyeditor" on a 1261 | rel attribute. The second selector would only match an 1262 | a element with an href attribute having the 1263 | exact value "http://www.w3.org/".

1264 | 1265 |
a[rel~="copyright"] { ... }
1266 | a[href="http://www.w3.org/"] { ... }
1267 | 1268 |

The following selector represents an a element whose 1269 | hreflang attribute is exactly "fr".

1270 | 1271 |
a[hreflang=fr]
1272 | 1273 |

The following selector represents an a element for which 1274 | the value of the hreflang attribute begins with "en", 1275 | including "en", "en-US", and "en-scouse":

1276 | 1277 |
a[hreflang|="en"]
1278 | 1279 |

The following selectors represent a DIALOGUE element 1280 | whenever it has one of two different values for an attribute 1281 | character:

1282 | 1283 |
DIALOGUE[character=romeo]
1284 | DIALOGUE[character=juliet]
1285 |
1286 | 1287 |

6.3.2. Substring 1288 | matching attribute selectors

1289 | 1290 |

Three additional attribute selectors are provided for matching 1291 | substrings in the value of an attribute: 1292 | 1293 |

1294 |
[att^=val] 1295 | 1296 |
Represents an element with the att attribute whose value 1297 | begins with the prefix "val". If "val" is the empty string then the 1298 | selector does not represent anything. 1299 | 1300 |
[att$=val] 1301 | 1302 |
Represents an element with the att attribute whose value 1303 | ends with the suffix "val". If "val" is the empty string then the 1304 | selector does not represent anything. 1305 | 1306 |
[att*=val] 1307 | 1308 |
Represents an element with the att attribute whose value 1309 | contains at least one instance of the substring "val". If "val" is the 1310 | empty string then the selector does not represent anything. 1311 |
1312 | 1313 |

Attribute values must be CSS identifiers 1315 | or strings. 1316 | [CSS21] The 1317 | case-sensitivity of attribute names in selectors depends on the document 1318 | language. 1319 | 1320 |

1321 |

Examples:

1322 | 1323 |

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

1325 | 1326 |
object[type^="image/"]
1327 | 1328 |

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

1330 | 1331 |
a[href$=".html"]
1332 | 1333 |

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

1335 | 1336 |
p[title*="hello"]
1337 |
1338 | 1339 |

6.3.3. Attribute selectors and 1340 | namespaces

1341 | 1342 |

The attribute name in an attribute selector is given as a CSS qualified 1344 | name: a namespace prefix that has been previously declared may be prepended to the attribute name 1346 | separated by the namespace separator "vertical bar" 1347 | (|). In keeping with the Namespaces in the XML 1348 | recommendation, default namespaces do not apply to attributes, therefore 1349 | attribute selectors without a namespace component apply only to attributes 1350 | that have no namespace (equivalent to "|attr"; these 1351 | attributes are said to be in the "per-element-type namespace partition"). 1352 | An asterisk may be used for the namespace prefix indicating that the 1353 | selector is to match all attribute names without regard to the attribute's 1354 | namespace. 1355 | 1356 |

An attribute selector with an attribute name containing a namespace 1357 | prefix that has not been previously declared is an 1358 | invalid selector. 1359 | 1360 |

1361 |

CSS examples:

1362 | 1363 |
@namespace foo "http://www.example.com";
1364 | [foo|att=val] { color: blue }
1365 | [*|att] { color: yellow }
1366 | [|att] { color: green }
1367 | [att] { color: green }
1368 | 1369 |

The first rule will match only elements with the attribute 1370 | att in the "http://www.example.com" namespace with the value 1371 | "val".

1372 | 1373 |

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

1376 | 1377 |

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

1379 |
1380 | 1381 |

6.3.4. Default attribute values 1382 | in DTDs

1383 | 1384 |

Attribute selectors represent attribute values in the document tree. How 1385 | that document tree is constructed is outside the scope of Selectors. In 1386 | some document formats default attribute values can be defined in a DTD or 1387 | elsewhere, but these can only be selected by attribute selectors if they 1388 | appear in the document tree. Selectors should be designed so that they 1389 | work whether or not the default values are included in the document tree. 1390 | 1391 |

For example, a XML UA may, but is not required to read an 1392 | "external subset" of the DTD but is required to look for default 1393 | attribute values in the document's "internal subset." (See, e.g., [XML10] for definitions 1395 | of these subsets.) Depending on the UA, a default attribute value defined 1396 | in the external subset of the DTD might or might not appear in the 1397 | document tree. 1398 | 1399 |

A UA that recognizes an XML namespace may, but is not required to use 1400 | its knowledge of that namespace to treat default attribute values as if 1401 | they were present in the document. (For example, an XHTML UA is not 1402 | required to use its built-in knowledge of the XHTML DTD. See, e.g., [XML-NAMES] for 1404 | details on namespaces in XML 1.0.) 1405 | 1406 |

Note: Typically, implementations choose to 1407 | ignore external subsets. This corresponds to the behaviour of 1408 | non-validating processors as defined by the XML specification. 1409 | 1410 |

1411 |

Example:

1412 | 1413 |

Consider an element EXAMPLE with an attribute 1414 | radix that has a default value of "decimal". 1415 | The DTD fragment might be

1416 | 1417 |
<!ATTLIST EXAMPLE radix (decimal,octal) "decimal">
1419 | 1420 |

If the style sheet contains the rules

1421 | 1422 |
EXAMPLE[radix=decimal] { /*... default property settings ...*/ }
1423 | EXAMPLE[radix=octal]   { /*... other settings...*/ }
1424 | 1425 |

the first rule might not match elements whose radix 1426 | attribute is set by default, i.e. not set explicitly. To catch all cases, 1427 | the attribute selector for the default value must be dropped:

1428 | 1429 |
EXAMPLE                { /*... default property settings ...*/ }
1430 | EXAMPLE[radix=octal]   { /*... other settings...*/ }
1431 | 1432 |

Here, because the selector EXAMPLE[radix=octal] is more 1433 | specific than the type selector alone, the style declarations in the 1434 | second rule will override those in the first for elements that have a 1435 | radix attribute value of "octal". Care has to 1436 | be taken that all property declarations that are to apply only to the 1437 | default case are overridden in the non-default cases' style rules.

1438 |
1439 | 1440 |

6.4. Class selectors

1441 | 1442 |

Working with HTML, authors may use the "period" notation (also known as 1443 | "full stop", U+002E, .) as an alternative to the 1444 | ~= notation when representing the class 1445 | attribute. Thus, for HTML, div.value and 1446 | div[class~=value] have the same meaning. The attribute value 1447 | must immediately follow the full stop (.). 1448 | 1449 |

UAs may apply selectors using the period (.) notation in XML documents 1450 | if the UA has namespace-specific knowledge that allows it to determine 1451 | which attribute is the "class" attribute for the respective namespace. One 1452 | such example of namespace-specific knowledge is the prose in the 1453 | specification for a particular namespace (e.g. SVG 1.0 [SVG11] describes the SVG 1456 | class attribute and how a UA should interpret it, and 1457 | similarly MathML 1.01 [MATHML] describes the MathML 1460 | class attribute.) 1461 | 1462 |

1463 |

CSS examples:

1464 | 1465 |

We can assign style information to all elements with 1466 | class~="pastoral" as follows:

1467 | 1468 |
*.pastoral { color: green }  /* all elements with class~=pastoral */
1469 | 1470 |

or just

1471 | 1472 |
.pastoral { color: green }  /* all elements with class~=pastoral */
1473 | 1474 |

The following assigns style only to H1 elements with 1475 | class~="pastoral":

1476 | 1477 |
H1.pastoral { color: green }  /* H1 elements with class~=pastoral */
1478 | 1479 |

Given these rules, the first H1 instance below would not 1480 | have green text, while the second would:

1481 | 1482 |
<H1>Not green</H1>
1483 | <H1 class="pastoral">Very green</H1>
1484 | 1485 |

The following rule matches any P element whose 1486 | class attribute has been assigned a list of whitespace-separated values that includes both 1488 | pastoral and marine:

1489 | 1490 |
p.pastoral.marine { color: green }
1491 | 1492 |

This rule matches when class="pastoral blue aqua marine" 1493 | but does not match for class="pastoral blue".

1494 |
1495 | 1496 |

Note: Because CSS gives considerable power 1497 | to the "class" attribute, authors could conceivably design their own 1498 | "document language" based on elements with almost no associated 1499 | presentation (such as DIV and SPAN in HTML) and 1500 | assigning style information through the "class" attribute. Authors should 1501 | avoid this practice since the structural elements of a document language 1502 | often have recognized and accepted meanings and author-defined classes may 1503 | not. 1504 | 1505 |

Note: If an element has multiple class 1506 | attributes, their values must be concatenated with spaces between the 1507 | values before searching for the class. As of this time the working group 1508 | is not aware of any manner in which this situation can be reached, 1509 | however, so this behavior is explicitly non-normative in this 1510 | specification. 1511 | 1512 |

6.5. ID selectors

1513 | 1514 |

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

An ID-typed attribute of a document language allows authors to assign an 1523 | identifier to one element instance in the document tree. An ID selector 1524 | contains a "number sign" (U+0023, #) immediately 1525 | followed by the ID value, which must be an CSS identifiers. 1527 | An ID selector represents an element instance that has an identifier that 1528 | matches the identifier in the ID selector. 1529 | 1530 |

Selectors does not specify how a UA knows the ID-typed attribute of an 1531 | element. The UA may, e.g., read a document's DTD, have the information 1532 | hard-coded or ask the user. 1533 | 1534 |

1535 |

Examples:

1536 | 1537 |

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

1539 | 1540 |
h1#chapter1
1541 | 1542 |

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

1544 | 1545 |
#chapter1
1546 | 1547 |

The following selector represents any element whose ID-typed attribute 1548 | has the value "z98y".

1549 | 1550 |
*#z98y
1551 |
1552 | 1553 |

Note: In XML 1.0 [XML10], the information about which 1555 | attribute contains an element's IDs is contained in a DTD or a schema. 1556 | When parsing XML, UAs do not always read the DTD, and thus may not know 1557 | what the ID of an element is (though a UA may have namespace-specific 1558 | knowledge that allows it to determine which attribute is the ID attribute 1559 | for that namespace). If a style sheet author knows or suspects that a UA 1560 | may not know what the ID of an element is, he should use normal attribute 1561 | selectors instead: [name=p371] instead of #p371. 1562 | 1563 |

If an element has multiple ID attributes, all of them must be treated as 1564 | IDs for that element for the purposes of the ID selector. Such a situation 1565 | could be reached using mixtures of xml:id, DOM3 Core, XML DTDs, and 1566 | namespace-specific knowledge. 1567 | 1568 |

6.6. Pseudo-classes

1569 | 1570 |

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

A pseudo-class always consists of a "colon" (:) 1575 | followed by the name of the pseudo-class and optionally by a value between 1576 | parentheses. 1577 | 1578 |

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

6.6.1. Dynamic 1588 | pseudo-classes

1589 | 1590 |

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

Dynamic pseudo-classes do not appear in the document source or document 1595 | tree. 1596 | 1597 |

1599 | 1600 |

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

1611 | 1612 |

After some amount of time, user agents may choose to return a visited 1613 | link to the (unvisited) ‘:link’ state. 1614 | 1615 |

The two states are mutually exclusive. 1616 | 1617 |

1618 |

Example:

1619 | 1620 |

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

1622 | 1623 |
a.external:visited
1624 |
1625 | 1626 |

Note: It is possible for style sheet authors 1627 | to abuse the :link and :visited pseudo-classes to determine which sites a 1628 | user has visited without the user's consent. 1629 | 1630 |

UAs may therefore treat all links as unvisited links, or implement other 1631 | measures to preserve the user's privacy while rendering visited and 1632 | unvisited links differently. 1633 | 1634 |

6.6.1.2. 1635 | The user action pseudo-classes :hover, 1636 | :active, and :focus
1637 | 1638 |

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

1665 | 1666 |

There may be document language or implementation specific limits on 1667 | which elements can become :active or acquire 1668 | :focus. 1669 | 1670 |

These pseudo-classes are not mutually exclusive. An element may match 1671 | several pseudo-classes at the same time. 1672 | 1673 |

Selectors doesn't define if the parent of an element that is 1674 | ‘:active’ or ‘:hover’ is also in that state. 1676 | 1677 |

Note: If the ‘:hover’ state applies to an element because its 1679 | child is designated by a pointing device, then it's possible for 1680 | ‘:hover’ to apply to an element that is 1681 | not underneath the pointing device. 1682 | 1683 |

1684 |

Examples:

1685 | 1686 |
a:link    /* unvisited links */
1687 | a:visited /* visited links */
1688 | a:hover   /* user hovers */
1689 | a:active  /* active links */
1690 | 1691 |

An example of combining dynamic pseudo-classes:

1692 | 1693 |
a:focus
1694 | a:focus:hover
1695 | 1696 |

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

1698 |
1699 | 1700 |

Note: An element can be both ‘:visited’ and ‘:active’ (or ‘:link’ and ‘:active’). 1705 | 1706 |

6.6.2. The target 1707 | pseudo-class :target

1708 | 1709 |

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

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

http://example.com/html/top.html#section_2
1718 | 1719 |

A target element can be represented by the :target 1720 | pseudo-class. If the document's URI has no fragment identifier, then the 1721 | document has no target element. 1722 | 1723 |

1724 |

Example:

1725 | 1726 |
p.note:target
1727 | 1728 |

This selector represents a p element of class 1729 | note that is the target element of the referring URI.

1730 |
1731 | 1732 |
1733 |

CSS example:

1734 | 1735 |

Here, the :target pseudo-class is used to make the target 1736 | element red and place an image before it, if there is one:

1737 | 1738 |
*:target { color : red }
1739 | *:target::before { content : url(target.png) }
1740 |
1741 | 1742 |

6.6.3. The language 1743 | pseudo-class :lang

1744 | 1745 |

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

The pseudo-class :lang(C) represents an element that is in 1756 | language C. Whether an element is represented by a :lang() 1757 | selector is based solely on the element's language value (normalized to 1758 | BCP 47 syntax if necessary) being equal to the identifier C, or beginning 1759 | with the identifier C immediately followed by "-" (U+002D). The matching 1760 | of C against the element's language value is performed case-insensitively. 1761 | The identifier C does not have to be a valid language name. 1762 | 1763 |

C must be a valid CSS identifier 1765 | [CSS21] and must not 1766 | be empty. (Otherwise, the selector is invalid.) 1767 | 1768 |

Note: It is recommended that documents and 1769 | protocols indicate language using codes from BCP 47 [BCP47] or its successor, and by means 1771 | of "xml:lang" attributes in the case of XML-based documents [XML10]. See "FAQ: 1774 | Two-letter or three-letter language codes." 1775 | 1776 |

1777 |

Examples:

1778 | 1779 |

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

1782 | 1783 |
html:lang(fr-be)
1784 | html:lang(de)
1785 | :lang(fr-be) > q
1786 | :lang(de) > q
1787 |
1788 | 1789 |

The difference between :lang(C) and the ‘|=’ operator is that the ‘|=’ operator only performs a comparison against a 1792 | given attribute on the element, while the :lang(C) 1793 | pseudo-class uses the UAs knowledge of the document's semantics to perform 1794 | the comparison. 1795 | 1796 |

1797 |

In this HTML example, only the BODY matches [lang|=fr] 1798 | (because it has a LANG attribute) but both the BODY and the P match 1799 | :lang(fr) (because both are in French). The P does not match 1800 | the [lang|=fr] because it does not have a LANG attribute.

1801 | 1802 |
<body lang=fr>
1803 |   <p>Je suis français.</p>
1804 | </body>
1805 |
1806 | 1807 |

6.6.4. The UI element states 1808 | pseudo-classes

1809 | 1810 |
6.6.4.1. The :enabled and 1811 | :disabled pseudo-classes
1812 | 1813 |

The :enabled pseudo-class represents user interface 1814 | elements that are in an enabled state; such elements have a corresponding 1815 | disabled state. 1816 | 1817 |

Conversely, the :disabled pseudo-class represents user 1818 | interface elements that are in a disabled state; such elements have a 1819 | corresponding enabled state. 1820 | 1821 |

What constitutes an enabled state, a disabled state, and a user 1822 | interface element is language-dependent. In a typical document most 1823 | elements will be neither :enabled nor :disabled. 1824 | 1825 |

Note: CSS properties that might affect a 1826 | user’s ability to interact with a given user interface element do not 1827 | affect whether it matches :enabled or :disabled; 1828 | e.g., the display and visibility properties have 1829 | no effect on the enabled/disabled state of an element. 1830 | 1831 |

6.6.4.2. The :checked pseudo-class
1832 | 1833 |

Radio and checkbox elements can be toggled by the user. Some menu items 1834 | are "checked" when the user selects them. When such elements are toggled 1835 | "on" the :checked pseudo-class applies. While the 1836 | :checked pseudo-class is dynamic in nature, and can altered 1837 | by user action, since it can also be based on the presence of semantic 1838 | attributes in the document, it applies to all media. For example, the 1839 | :checked pseudo-class initially applies to such elements that 1840 | have the HTML4 selected and checked attributes 1841 | as described in Section 1843 | 17.2.1 of HTML4, but of course the user can toggle "off" such elements 1844 | in which case the :checked pseudo-class would no longer 1845 | apply. 1846 | 1847 |

6.6.4.3. The :indeterminate 1848 | pseudo-class
1849 | 1850 |
1851 |

Note: Radio and checkbox elements can be toggled by 1852 | the user, but are sometimes in an indeterminate state, neither checked 1853 | nor unchecked. This can be due to an element attribute, or DOM 1854 | manipulation.

1855 | 1856 |

A future version of this specification may introduce an 1857 | :indeterminate pseudo-class that applies to such elements. 1858 |

1864 |
1865 | 1866 |

6.6.5. Structural 1867 | pseudo-classes

1868 | 1869 |

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

Standalone text and other non-element nodes are not counted when 1875 | calculating the position of an element in the list of children of its 1876 | parent. When calculating the position of an element in the list of 1877 | children of its parent, the index numbering starts at 1. 1878 | 1879 |

6.6.5.1. :root pseudo-class
1880 | 1881 |

The :root pseudo-class represents an element that is the 1882 | root of the document. In HTML 4, this is always the HTML 1883 | element. 1884 | 1885 |

6.6.5.2. :nth-child() 1886 | pseudo-class
1887 | 1888 |

The :nth-child(an+b) 1889 | pseudo-class notation represents an element that has 1890 | an+b-1 siblings before 1891 | it in the document tree, for any positive integer or zero value of 1892 | n, and has a parent element. For values of a and 1893 | b greater than zero, this effectively divides the element's 1894 | children into groups of a elements (the last group taking the 1895 | remainder), and selecting the bth element of each group. For 1896 | example, this allows the selectors to address every other row in a table, 1897 | and could be used to alternate the color of paragraph text in a cycle of 1898 | four. The a and b values must be integers (positive, 1899 | negative, or zero). The index of the first child of an element is 1. 1900 | 1901 |

In addition to this, :nth-child() can take ‘odd’ and ‘even’ as arguments instead. 1904 | ‘odd’ has the same 1905 | signification as 2n+1, and ‘even’ has the same signification as 1907 | 2n. 1908 | 1909 |

The argument to :nth-child() must match the grammar below, 1910 | where INTEGER matches the token [0-9]+ and the 1911 | rest of the tokenization is given by the Lexical 1912 | scanner in section 10.2: 1913 | 1914 |

nth
1915 |   : S* [ ['-'|'+']? INTEGER? {N} [ S* ['-'|'+'] S* INTEGER ]? |
1916 |          ['-'|'+']? INTEGER | {O}{D}{D} | {E}{V}{E}{N} ] S*
1917 |   ;
1918 | 1919 |
1920 |

Examples:

1921 | 1922 |
tr:nth-child(2n+1) /* represents every odd row of an HTML table */
1923 | tr:nth-child(odd)  /* same */
1924 | tr:nth-child(2n+0) /* represents every even row of an HTML table */
1925 | tr:nth-child(even) /* same */
1926 | 
1927 | /* Alternate paragraph colours in CSS */
1928 | p:nth-child(4n+1) { color: navy; }
1929 | p:nth-child(4n+2) { color: green; }
1930 | p:nth-child(4n+3) { color: maroon; }
1931 | p:nth-child(4n+4) { color: purple; }
1932 |
1933 | 1934 |

When the value b is preceded by a negative sign, the "+" 1935 | character in the expression must be removed (it is effectively replaced by 1936 | the "-" character indicating the negative value of b). 1937 | 1938 |

1939 |

Examples:

1940 | 1941 |
:nth-child(10n-1)  /* represents the 9th, 19th, 29th, etc, element */
1942 | :nth-child(10n+9)  /* Same */
1943 | :nth-child(10n+-1) /* Syntactically invalid, and would be ignored */
1944 |
1945 | 1946 |

When a=0, the an part need not be 1947 | included (unless the b part is already omitted). When 1948 | an is not included and b is 1949 | non-negative, the + sign before b (when allowed) 1950 | may also be omitted. In this case the syntax simplifies to 1951 | :nth-child(b). 1952 | 1953 |

1954 |

Examples:

1955 | 1956 |
foo:nth-child(0n+5)   /* represents an element foo that is the 5th child
1957 |                          of its parent element */
1958 | foo:nth-child(5)      /* same */
1959 |
1960 | 1961 |

When a=1, or a=-1, the number may be omitted from 1962 | the rule. 1963 | 1964 |

1965 |

Examples:

1966 | 1967 |

The following selectors are therefore equivalent:

1968 | 1969 |
bar:nth-child(1n+0)   /* represents all bar elements, specificity (0,1,1) */
1970 | bar:nth-child(n+0)    /* same */
1971 | bar:nth-child(n)      /* same */
1972 | bar                   /* same but lower specificity (0,0,1) */
1973 |
1974 | 1975 |

If b=0, then every ath element is picked. In such 1976 | a case, the +b (or -b) part may be omitted unless 1977 | the a part is already omitted. 1978 | 1979 |

1980 |

Examples:

1981 | 1982 |
tr:nth-child(2n+0) /* represents every even row of an HTML table */
1983 | tr:nth-child(2n) /* same */
1984 |
1985 | 1986 |

Whitespace is permitted after the "(", before the ")", and on either 1987 | side of the "+" or "-" that separates the an and 1988 | b parts when both are present. 1989 | 1990 |

1991 |

Valid Examples with white space:

1992 | 1993 |
1994 | :nth-child( 3n + 1 )
1995 | :nth-child( +3n - 2 )
1996 | :nth-child( -n+ 6)
1997 | :nth-child( +6 )
1998 | 
1999 | 2000 |

Invalid Examples with white space:

2001 | 2002 |
2003 | :nth-child(3 n)
2004 | :nth-child(+ 2n)
2005 | :nth-child(+ 2)
2006 | 
2007 |
2008 | 2009 |

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

The value a can be negative, but only the positive values of 2013 | an+b, for n≥0, may 2014 | represent an element in the document tree. 2015 | 2016 |

2017 |

Example:

2018 | 2019 |
html|tr:nth-child(-n+6)  /* represents the 6 first rows of XHTML tables */
2020 |
2021 | 2022 |
6.6.5.3. 2023 | :nth-last-child() pseudo-class
2024 | 2025 |

The :nth-last-child(an+b) 2026 | pseudo-class notation represents an element that has 2027 | an+b-1 siblings after 2028 | it in the document tree, for any positive integer or zero value of 2029 | n, and has a parent element. See :nth-child() pseudo-class for 2031 | the syntax of its argument. It also accepts the ‘even’ and ‘odd’ values as arguments. 2034 | 2035 |

2036 |

Examples:

2037 | 2038 |
tr:nth-last-child(-n+2)    /* represents the two last rows of an HTML table */
2039 | 
2040 | foo:nth-last-child(odd)    /* represents all odd foo elements in their parent element,
2041 |                               counting from the last one */
2042 |
2043 | 2044 |
6.6.5.4. :nth-of-type() 2045 | pseudo-class
2046 | 2047 |

The :nth-of-type(an+b) pseudo-class 2048 | notation represents an element that has 2049 | an+b-1 siblings with the same expanded 2050 | element name before it in the document tree, for any zero 2051 | or positive integer value of n, and has a parent element. See 2052 | :nth-child() pseudo-class for 2053 | the syntax of its argument. It also accepts the ‘even’ and ‘odd’ values. 2056 | 2057 |

2058 |

CSS example:

2059 | 2060 |

This allows an author to alternate the position of floated images:

2061 | 2062 |
img:nth-of-type(2n+1) { float: right; }
2063 | img:nth-of-type(2n) { float: left; }
2064 |
2065 | 2066 |
6.6.5.5. 2067 | :nth-last-of-type() pseudo-class
2068 | 2069 |

The :nth-last-of-type(an+b) 2070 | pseudo-class notation represents an element that has 2071 | an+b-1 siblings with the same expanded 2072 | element name after it in the document tree, for any zero 2073 | or positive integer value of n, and has a parent element. See 2074 | :nth-child() pseudo-class for 2075 | the syntax of its argument. It also accepts the ‘even’ and ‘odd’ values. 2078 | 2079 |

2080 |

Example:

2081 | 2082 |

To represent all h2 children of an XHTML body 2083 | except the first and last, one could use the following selector:

2084 | 2085 |
body > h2:nth-of-type(n+2):nth-last-of-type(n+2)
2086 | 2087 |

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

2089 | 2090 |
body > h2:not(:first-of-type):not(:last-of-type)
2091 |
2092 | 2093 |
6.6.5.6. :first-child 2094 | pseudo-class
2095 | 2096 |

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

2101 |

Examples:

2102 | 2103 |

The following selector represents a p element that is the 2104 | first child of a div element:

2105 | 2106 |
div > p:first-child
2107 | 2108 |

This selector can represent the p inside the 2109 | div of the following fragment:

2110 | 2111 |
<p> The last P before the note.</p>
2112 | <div class="note">
2113 |    <p> The first P inside the note.</p>
2114 | </div>
2115 | but cannot represent the second p in the following fragment: 2116 |
<p> The last P before the note.</p>
2117 | <div class="note">
2118 |    <h2> Note </h2>
2119 |    <p> The first P inside the note.</p>
2120 | </div>
2121 | 2122 |

The following two selectors are usually equivalent:

2123 | 2124 |
* > a:first-child /* a is first child of any element */
2125 | a:first-child /* Same (assuming a is not the root element) */
2126 |
2127 | 2128 |
6.6.5.7. :last-child 2129 | pseudo-class
2130 | 2131 |

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

2136 |

Example:

2137 | 2138 |

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

ol > li:last-child
2142 |
2143 | 2144 |
6.6.5.8. 2145 | :first-of-type pseudo-class
2146 | 2147 |

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

2152 |

Example:

2153 | 2154 |

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

2157 | 2158 |
dl dt:first-of-type
2159 | 2160 |

It is a valid description for the first two dt elements in 2161 | the following example but not for the third one:

2162 | 2163 |
<dl>
2164 |  <dt>gigogne</dt>
2165 |  <dd>
2166 |   <dl>
2167 |    <dt>fusée</dt>
2168 |    <dd>multistage rocket</dd>
2169 |    <dt>table</dt>
2170 |    <dd>nest of tables</dd>
2171 |   </dl>
2172 |  </dd>
2173 | </dl>
2174 |
2175 | 2176 |
6.6.5.9. :last-of-type 2177 | pseudo-class
2178 | 2179 |

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

2184 |

Example:

2185 | 2186 |

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

2188 | 2189 |
tr > td:last-of-type
2190 |
2191 | 2192 |
6.6.5.10. :only-child 2193 | pseudo-class
2194 | 2195 |

Represents an element that has a parent element and whose parent element 2196 | has no other element children. Same as 2197 | :first-child:last-child or 2198 | :nth-child(1):nth-last-child(1), but with a lower 2199 | specificity. 2200 | 2201 |

6.6.5.11. :only-of-type 2202 | pseudo-class
2203 | 2204 |

Represents an element that has a parent element and whose parent element 2205 | has no other element children with the same expanded element name. Same as 2206 | :first-of-type:last-of-type or 2207 | :nth-of-type(1):nth-last-of-type(1), but with a lower 2208 | specificity. 2209 | 2210 |

6.6.5.12. :empty pseudo-class
2211 | 2212 |

The :empty pseudo-class represents an element that has no 2213 | children at all. In terms of the document tree, only element nodes and 2214 | content nodes (such as DOM [DOM-LEVEL-3-CORE] text 2216 | nodes, CDATA nodes, and entity references) whose data has a non-zero 2217 | length must be considered as affecting emptiness; comments, processing 2218 | instructions, and other nodes must not affect whether an element is 2219 | considered empty or not. 2220 | 2221 |

2222 |

Examples:

2223 | 2224 |

p:empty is a valid representation of the following 2225 | fragment:

2226 | 2227 |
<p></p>
2228 | 2229 |

foo:empty is not a valid representation for the following 2230 | fragments:

2231 | 2232 |
<foo>bar</foo>
2233 | 2234 |
<foo><bar>bla</bar></foo>
2235 | 2236 |
<foo>this is not <bar>:empty</bar></foo>
2237 |
2238 | 2239 |

6.6.6. Blank

2240 | 2241 | 2242 |

This section intentionally left blank. (This section previously defined 2243 | a :contains() pseudo-class.)

2244 | 2245 | 2246 |

6.6.7. The negation pseudo-class

2247 | 2248 |

The negation pseudo-class, :not(X), is a 2249 | functional notation taking a simple 2250 | selector (excluding the negation pseudo-class itself) as an argument. 2251 | It represents an element that is not represented by its argument. 2252 | 2253 |

Negations may not be nested; :not(:not(...)) is invalid. 2254 | Note also that since pseudo-elements are not simple selectors, they are 2255 | not a valid argument to :not(). 2256 | 2257 |

2258 |

Examples:

2259 | 2260 |

The following selector matches all button elements in an 2261 | HTML document that are not disabled.

2262 | 2263 |
button:not([DISABLED])
2264 | 2265 |

The following selector represents all but FOO elements.

2266 | 2267 |
*:not(FOO)
2268 | 2269 |

The following group of selectors represents all HTML elements except 2270 | links.

2271 | 2272 |
html|*:not(:link):not(:visited)
2273 |
2274 | 2275 |

Default namespace declarations do not affect the argument of the 2276 | negation pseudo-class unless the argument is a universal selector or a 2277 | type selector. 2278 | 2279 |

2280 |

Examples:

2281 | 2282 |

Assuming that the default namespace is bound to "http://example.com/", 2283 | the following selector represents all elements that are not in that 2284 | namespace:

2285 | 2286 |
*|*:not(*)
2287 | 2288 |

The following selector matches any element that is not being hovered, 2289 | regardless of its namespace. In particular, it is not limited to only 2290 | matching elements in the default namespace that are not being hovered, 2291 | and elements not in the default namespace don't match the rule when they 2292 | are being hovered.

2293 | 2294 |
*|*:not(:hover)
2295 |
2296 | 2297 |

Note: the :not() pseudo allows useless 2298 | selectors to be written. For instance :not(*|*), which 2299 | represents no element at all, or foo:not(bar), which is 2300 | equivalent to foo but with a higher specificity. 2301 | 2302 |

7. Pseudo-elements

2303 | 2304 |

Pseudo-elements create abstractions about the document tree beyond those 2305 | specified by the document language. For instance, document languages do 2306 | not offer mechanisms to access the first letter or first line of an 2307 | element's content. Pseudo-elements allow authors to refer to this 2308 | otherwise inaccessible information. Pseudo-elements may also provide 2309 | authors a way to refer to content that does not exist in the source 2310 | document (e.g., the ::before and ::after 2311 | pseudo-elements give access to generated content). 2312 | 2313 |

A pseudo-element is made of two colons (::) followed by the 2314 | name of the pseudo-element. 2315 | 2316 |

This :: notation is introduced by the current document in 2317 | order to establish a discrimination between pseudo-classes and 2318 | pseudo-elements. For compatibility with existing style sheets, user agents 2319 | must also accept the previous one-colon notation for pseudo-elements 2320 | introduced in CSS levels 1 and 2 (namely, :first-line, 2321 | :first-letter, :before and :after). 2322 | This compatibility is not allowed for the new pseudo-elements introduced 2323 | in this specification. 2324 | 2325 |

Only one pseudo-element may appear per selector, and if present it must 2326 | appear after the sequence of simple selectors that represents the subjects of the selector. Note: A future version of this specification 2329 | may allow multiple pseudo-elements per selector. 2330 | 2331 |

7.1. The ::first-line 2332 | pseudo-element

2333 | 2334 |

The ::first-line pseudo-element describes the contents of 2335 | the first formatted line of an element. 2336 | 2337 |

2338 |

CSS example:

2339 | 2340 |
p::first-line { text-transform: uppercase }
2341 | 2342 |

The above rule means "change the letters of the first line of every 2343 | p element to uppercase".

2344 | 2345 |

The selector p::first-line does not match any real 2346 | document element. It does match a pseudo-element that conforming user 2347 | agents will insert at the beginning of every p element.

2348 |
2349 | 2350 |

Note that the length of the first line depends on a number of factors, 2351 | including the width of the page, the font size, etc. Thus, an ordinary 2352 | HTML paragraph such as: 2353 | 2354 |

2355 | <P>This is a somewhat long HTML 
2356 | paragraph that will be broken into several 
2357 | lines. The first line will be identified
2358 | by a fictional tag sequence. The other lines 
2359 | will be treated as ordinary lines in the 
2360 | paragraph.</P>
2361 | 
2362 | 2363 |

the lines of which happen to be broken as follows: 2364 | 2365 |

2366 | THIS IS A SOMEWHAT LONG HTML PARAGRAPH THAT
2367 | will be broken into several lines. The first
2368 | line will be identified by a fictional tag 
2369 | sequence. The other lines will be treated as 
2370 | ordinary lines in the paragraph.
2371 | 
2372 | 2373 |

This paragraph might be "rewritten" by user agents to include the 2374 | fictional tag sequence for ::first-line. This 2375 | fictional tag sequence helps to show how properties are inherited. 2376 | 2377 |

2378 | <P><P::first-line> This is a somewhat long HTML 
2379 | paragraph that </P::first-line> will be broken into several
2380 | lines. The first line will be identified 
2381 | by a fictional tag sequence. The other lines 
2382 | will be treated as ordinary lines in the 
2383 | paragraph.</P>
2384 | 
2385 | 2386 |

If a pseudo-element breaks up a real element, the desired effect can 2387 | often be described by a fictional tag sequence that closes and then 2388 | re-opens the element. Thus, if we mark up the previous paragraph with a 2389 | span element: 2390 | 2391 |

2392 | <P><SPAN class="test"> This is a somewhat long HTML
2393 | paragraph that will be broken into several
2394 | lines.</SPAN> The first line will be identified
2395 | by a fictional tag sequence. The other lines 
2396 | will be treated as ordinary lines in the 
2397 | paragraph.</P>
2398 | 
2399 | 2400 |

the user agent could simulate start and end tags for span 2401 | when inserting the fictional tag sequence for ::first-line. 2402 | 2403 |

2404 | <P><P::first-line><SPAN class="test"> This is a
2405 | somewhat long HTML
2406 | paragraph that will </SPAN></P::first-line><SPAN class="test"> be
2407 | broken into several
2408 | lines.</SPAN> The first line will be identified
2409 | by a fictional tag sequence. The other lines
2410 | will be treated as ordinary lines in the 
2411 | paragraph.</P>
2412 | 
2413 | 2414 |

7.1.1. First formatted line definition in CSS

2416 | 2417 |

In CSS, the ::first-line pseudo-element can only have an 2418 | effect when attached to a block-like container such as a block box, 2419 | inline-block, table-caption, or table-cell. 2420 | 2421 |

The first formatted line of an element may occur inside a block-level 2422 | descendant in the same flow (i.e., a block-level descendant that is not 2423 | out-of-flow due to floating or positioning). For example, the first line 2424 | of the DIV in <DIV><P>This 2425 | line...</P></DIV> is the first line of the P 2426 | (assuming that both P and DIV are block-level). 2427 | 2428 |

The first line of a table-cell or inline-block cannot be the first 2429 | formatted line of an ancestor element. Thus, in <DIV><P 2430 | STYLE="display: inline-block">Hello<BR>Goodbye</P> 2431 | etcetera</DIV> the first formatted line of the DIV 2432 | is not the line "Hello". 2433 | 2434 |

Note: Note that the first line of the 2435 | p in this fragment: <p><br>First... 2436 | doesn't contain any letters (assuming the default style for 2437 | br in HTML 4). The word "First" is not on the first formatted 2438 | line. 2439 | 2440 |

A UA should act as if the fictional start tags of the 2441 | ::first-line pseudo-elements were nested just inside the 2442 | innermost enclosing block-level element. (Since CSS1 and CSS2 were silent 2443 | on this case, authors should not rely on this behavior.) For example, the 2444 | fictional tag sequence for 2445 | 2446 |

2447 | <DIV>
2448 |   <P>First paragraph</P>
2449 |   <P>Second paragraph</P>
2450 | </DIV>
2451 | 
2452 | 2453 |

is 2454 | 2455 |

2456 | <DIV>
2457 |   <P><DIV::first-line><P::first-line>First paragraph</P::first-line></DIV::first-line></P>
2458 |   <P><P::first-line>Second paragraph</P::first-line></P>
2459 | </DIV>
2460 | 
2461 | 2462 |

The ::first-line pseudo-element is similar to an 2463 | inline-level element, but with certain restrictions. The following CSS 2464 | properties apply to a ::first-line pseudo-element: font 2465 | properties, color property, background properties, ‘word-spacing’, ‘letter-spacing’, ‘text-decoration’, ‘vertical-align’, ‘text-transform’, ‘line-height’. UAs may apply other properties 2472 | as well. 2473 | 2474 |

During CSS inheritance, the portion of a child element that occurs on 2475 | the first line only inherits properties applicable to the 2476 | ::first-line pseudo-element from the 2477 | ::first-line pseudo-element. For all other properties 2478 | inheritence is from the non-pseudo-element parent of the first line pseudo 2479 | element. (The portion of a child element that does not occur on the first 2480 | line always inherits from the parent of that child.) 2481 | 2482 |

7.2. The ::first-letter 2483 | pseudo-element

2484 | 2485 |

The ::first-letter pseudo-element represents the first 2486 | letter of an element, if it is not preceded by any other content (such as 2487 | images or inline tables) on its line. The ::first-letter pseudo-element 2488 | may be used for "initial caps" and "drop caps", which are common 2489 | typographical effects. 2490 | 2491 |

Punctuation (i.e, characters defined in Unicode in the "open" (Ps), 2492 | "close" (Pe), "initial" (Pi). "final" (Pf) and "other" (Po) punctuation 2493 | classes), that precedes or follows the first letter should be included. [UNICODE] 2495 | 2496 |

2497 |

Quotes that precede the first letter should be included.

2499 |
2500 | 2501 |

The ::first-letter also applies if the first letter is in 2502 | fact a digit, e.g., the "6" in "67 million dollars is a lot of money." 2503 | 2504 |

Note: In some cases the 2505 | ::first-letter pseudo-element should include more than just 2506 | the first non-punctuation character on a line. For example, combining 2507 | characters must be kept with their base character. Additionally, some 2508 | languages may have specific rules about how to treat certain letter 2509 | combinations. The UA definition of ::first-letter should 2510 | include at least the default grapheme cluster as defined by UAX29 and may 2511 | include more than that as appropriate. In Dutch, for example, if the 2512 | letter combination "ij" appears at the beginning of an element, both 2513 | letters should be considered within the ::first-letter 2514 | pseudo-element. [UAX29] 2516 | 2517 |

If the letters that would form the ::first-letter are not 2518 | in the same element, such as "‘T" in 2519 | <p>'<em>T..., the UA may create a 2520 | ::first-letter pseudo-element from one of the elements, both 2521 | elements, or simply not create a pseudo-element. 2522 | 2523 |

Similarly, if the first letter(s) of the block are not at the start of 2524 | the line (for example due to bidirectional reordering), then the UA need 2525 | not create the pseudo-element(s). 2526 | 2527 |

2528 |

Example:

2529 | 2530 |

The following CSS and HTML example 2531 | illustrates how overlapping pseudo-elements may interact. The first 2532 | letter of each P element will be green with a font size of ’24pt'. 2533 | The rest of the first formatted line will be ‘blue’ while the rest of the paragraph will be 2535 | ‘red’.

2536 | 2537 |
p { color: red; font-size: 12pt }
2538 | p::first-letter { color: green; font-size: 200% }
2539 | p::first-line { color: blue }
2540 | 
2541 | <P>Some text that ends up on two lines</P>
2542 | 2543 |

Assuming that a line break will occur before the word "ends", the fictional tag sequence for this fragment might be:

2546 | 2547 |
<P>
2548 | <P::first-line>
2549 | <P::first-letter> 
2550 | S 
2551 | </P::first-letter>ome text that 
2552 | </P::first-line> 
2553 | ends up on two lines 
2554 | </P>
2555 | 2556 |

Note that the ::first-letter element is inside the 2557 | ::first-line element. Properties set on 2558 | ::first-line are inherited by ::first-letter, 2559 | but are overridden if the same property is set on 2560 | ::first-letter.

2561 |
2562 | 2563 |

The first letter must occur on the first 2564 | formatted line. For example, in this HTML fragment: 2565 | <p><br>First... the first line doesn't contain any 2566 | letters and ::first-letter doesn't match anything (assuming 2567 | the default style for br in HTML 4). In particular, it does 2568 | not match the "F" of "First." 2569 | 2570 |

7.2.1. Application in 2571 | CSS

2572 | 2573 |

In CSS, the ::first-letter pseudo-element applies to 2574 | block-like containers such as block, list-item, table-cell, table-caption, 2575 | and inline-block elements. Note: A 2576 | future version of this specification may allow this pseudo-element to 2577 | apply to more display types. 2578 | 2579 |

The ::first-letter pseudo-element can be used with all such 2580 | elements that contain text, or that have a descendant in the same flow 2581 | that contains text. A UA should act as if the fictional start tag of the 2582 | ::first-letter pseudo-element is just before the first text of the 2583 | element, even if that first text is in a descendant. 2584 | 2585 |

2586 |

Example:

2587 | 2588 |

The fictional tag sequence for this HTML fragment: 2589 | 2590 |

<div>
2591 | <p>The first text.
2592 | 2593 |

is: 2594 | 2595 |

<div>
2596 | <p><div::first-letter><p::first-letter>T</...></...>he first text.
2597 |
2598 | 2599 |

In CSS the first letter of a table-cell or inline-block cannot be the 2600 | first letter of an ancestor element. Thus, in <DIV><P 2601 | STYLE="display: inline-block">Hello<BR>Goodbye</P> 2602 | etcetera</DIV> the first letter of the DIV is 2603 | not the letter "H". In fact, the DIV doesn't have a first 2604 | letter. 2605 | 2606 |

If an element is a list item (‘display: 2607 | list-item’), the ::first-letter applies to the 2608 | first letter in the principal box after the marker. UAs may ignore 2609 | ::first-letter on list items with ‘list-style-position: inside’. If an element has 2611 | ::before or ::after content, the 2612 | ::first-letter applies to the first letter of the element 2613 | including that content. 2614 | 2615 |

2616 |

Example:

2617 | 2618 |

After the rule p::before {content: "Note: "}, the selector 2619 | p::first-letter matches the "N" of "Note".

2620 |
2621 | 2622 |

In CSS a ::first-line pseudo-element is similar to an inline-level 2623 | element if its ‘float’ property is 2624 | ‘none’; otherwise, it is similar 2625 | to a floated element. The following properties that apply to 2626 | ::first-letter pseudo-elements: font properties, ‘text-decoration’, ‘text-transform’, ‘letter-spacing’, ‘word-spacing’ (when appropriate), ‘line-height’, ‘float’, ‘vertical-align’ (only if ‘float’ is ‘none’), margin properties, padding properties, 2636 | border properties, color property, background properties. UAs may apply 2637 | other properties as well. To allow UAs to render a typographically correct 2638 | drop cap or initial cap, the UA may choose a line-height, width and height 2639 | based on the shape of the letter, unlike for normal elements. 2640 | 2641 |

2642 |

Example:

2643 | 2644 |

This CSS and HTML example shows a possible rendering of an initial cap. 2645 | Note that the ‘line-height’ that 2646 | is inherited by the ::first-letter pseudo-element is 1.1, 2647 | but the UA in this example has computed the height of the first letter 2648 | differently, so that it doesn't cause any unnecessary space between the 2649 | first two lines. Also note that the fictional start tag of the first 2650 | letter is inside the span, and thus the font weight of the 2651 | first letter is normal, not bold as the span: 2652 | 2653 |

2654 | p { line-height: 1.1 }
2655 | p::first-letter { font-size: 3em; font-weight: normal }
2656 | span { font-weight: bold }
2657 | ...
2658 | <p><span>Het hemelsche</span> gerecht heeft zich ten lange lesten<br>
2659 | Erbarremt over my en mijn benaeuwde vesten<br>
2660 | En arme burgery, en op mijn volcx gebed<br>
2661 | En dagelix geschrey de bange stad ontzet.
2662 | 
2663 | 2664 |
2665 |

Image illustrating the ::first-letter pseudo-element 2667 |

2668 |
2669 | 2670 |
2671 |

The following CSS will make a drop cap initial letter span about two 2672 | lines:

2673 | 2674 |
2675 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
2676 | <HTML>
2677 |  <HEAD>
2678 |   <TITLE>Drop cap initial letter</TITLE>
2679 |   <STYLE type="text/css">
2680 |    P               { font-size: 12pt; line-height: 1.2 }
2681 |    P::first-letter { font-size: 200%; font-weight: bold; float: left }
2682 |    SPAN            { text-transform: uppercase }
2683 |   </STYLE>
2684 |  </HEAD>
2685 |  <BODY>
2686 |   <P><SPAN>The first</SPAN> few words of an article
2687 |     in The Economist.</P>
2688 |  </BODY>
2689 | </HTML>
2690 | 
2691 | 2692 |

This example might be formatted as follows:

2693 | 2694 |
2695 |

Image illustrating the combined effect of the ::first-letter
2696 |      and ::first-line pseudo-elements

2697 |
2698 | 2699 |

The fictional tag sequence is:

2701 | 2702 |
2703 | <P>
2704 | <SPAN>
2705 | <P::first-letter>
2706 | T
2707 | </P::first-letter>he first
2708 | </SPAN> 
2709 | few words of an article in the Economist.
2710 | </P>
2711 | 
2712 | 2713 |

Note that the ::first-letter pseudo-element tags abut the 2714 | content (i.e., the initial character), while the ::first-line 2715 | pseudo-element start tag is inserted right after the start tag of the 2716 | block element.

2717 |
2718 | 2719 |

In order to achieve traditional drop caps formatting, user agents may 2720 | approximate font sizes, for example to align baselines. Also, the glyph 2721 | outline may be taken into account when formatting. 2722 | 2723 |

7.3. Blank

2724 | 2725 |

This section intentionally left blank. (This section previously defined 2726 | a ::selection pseudo-element.) 2727 | 2728 |

7.4. The ::before and ::after 2729 | pseudo-elements

2730 | 2731 |

The ::before and ::after pseudo-elements can 2732 | be used to describe generated content before or after an element's 2733 | content. They are explained in CSS 2.1 [CSS21]. 2735 | 2736 |

When the ::first-letter and ::first-line 2737 | pseudo-elements are applied to an element having content generated using 2738 | ::before or ::after, they apply to the first 2739 | letter or line of the element including the generated content. 2740 | 2741 |

8. Combinators

2742 | 2743 |

8.1. Descendant 2744 | combinator

2745 | 2746 |

At times, authors may want selectors to describe an element that is the 2747 | descendant of another element in the document tree (e.g., "an 2748 | EM element that is contained within an H1 2749 | element"). Descendant combinators express such a relationship. A 2750 | descendant combinator is whitespace that 2751 | separates two sequences of simple selectors. A selector of the form 2752 | "A B" represents an element B that is an 2753 | arbitrary descendant of some ancestor element A. 2754 | 2755 |

2756 |

Examples:

2757 | 2758 |

For example, consider the following selector:

2759 | 2760 |
h1 em
2761 | 2762 |

It represents an em element being the descendant of an 2763 | h1 element. It is a correct and valid, but partial, 2764 | description of the following fragment:

2765 | 2766 |
<h1>This <span class="myclass">headline
2767 | is <em>very</em> important</span></h1>
2768 | 2769 |

The following selector:

2770 | 2771 |
div * p
2772 | 2773 |

represents a p element that is a grandchild or later 2774 | descendant of a div element. Note the whitespace on either 2775 | side of the "*" is not part of the universal selector; the whitespace is 2776 | a combinator indicating that the div must be the ancestor of 2777 | some element, and that that element must be an ancestor of the 2778 | p.

2779 | 2780 |

The following selector, which combines descendant combinators and attribute selectors, represents an 2782 | element that (1) has the href attribute set and (2) is 2783 | inside a p that is itself inside a div:

2784 | 2785 |
div p *[href]
2786 |
2787 | 2788 |

8.2. Child combinators

2789 | 2790 |

A child combinator describes a childhood 2791 | relationship between two elements. A child combinator is made of the 2792 | "greater-than sign" (U+003E, >) character and 2793 | separates two sequences of simple selectors. 2794 | 2795 |

2796 |

Examples:

2797 | 2798 |

The following selector represents a p element that is 2799 | child of body:

2800 | 2801 |
body > p
2802 | 2803 |

The following example combines descendant combinators and child 2804 | combinators.

2805 | 2806 |
div ol>li p
2807 | 2808 |

It represents a p element that is a descendant of an 2809 | li element; the li element must be the child of 2810 | an ol element; the ol element must be a 2811 | descendant of a div. Notice that the optional white space 2812 | around the ">" combinator has been left out.

2813 |
2814 | 2815 |

For information on selecting the first child of an element, please see 2816 | the section on the :first-child pseudo-class above. 2818 | 2819 |

8.3. Sibling 2820 | combinators

2821 | 2822 |

There are two different sibling combinators: the adjacent sibling 2823 | combinator and the general sibling combinator. In both cases, non-element 2824 | nodes (e.g. text between elements) are ignored when considering adjacency 2825 | of elements. 2826 | 2827 |

8.3.1. 2828 | Adjacent sibling combinator

2829 | 2830 |

The adjacent sibling combinator is made of the "plus sign" 2831 | (U+002B, +) character that separates two sequences of simple 2832 | selectors. The elements represented by the two sequences share the same 2833 | parent in the document tree and the element represented by the first 2834 | sequence immediately precedes the element represented by the second one. 2835 | 2836 |

2837 |

Examples:

2838 | 2839 |

The following selector represents a p element immediately 2840 | following a math element:

2841 | 2842 |
math + p
2843 | 2844 |

The following selector is conceptually similar to the one in the 2845 | previous example, except that it adds an attribute selector — it 2846 | adds a constraint to the h1 element, that it must have 2847 | class="opener":

2848 | 2849 |
h1.opener + h2
2850 |
2851 | 2852 |

8.3.2. General 2853 | sibling combinator

2854 | 2855 |

The general sibling combinator is made of the "tilde" (U+007E, 2856 | ~) character that separates two sequences of simple 2857 | selectors. The elements represented by the two sequences share the same 2858 | parent in the document tree and the element represented by the first 2859 | sequence precedes (not necessarily immediately) the element represented by 2860 | the second one. 2861 | 2862 |

2863 |

Example:

2864 | 2865 |
h1 ~ pre
2866 | 2867 |

represents a pre element following an h1. It 2868 | is a correct and valid, but partial, description of:

2869 | 2870 |
<h1>Definition of the function a</h1>
2871 | <p>Function a(x) has to be applied to all figures in the table.</p>
2872 | <pre>function a(x) = 12x/13.5</pre>
2873 |
2874 | 2875 |

9. Calculating a selector's 2876 | specificity

2877 | 2878 |

A selector's specificity is calculated as follows: 2879 | 2880 |

2891 | 2892 |

Selectors inside the negation pseudo-class are 2893 | counted like any other, but the negation itself does not count as a 2894 | pseudo-class. 2895 | 2896 |

Concatenating the three numbers a-b-c (in a number system with a large 2897 | base) gives the specificity. 2898 | 2899 |

2900 |

Examples:

2901 | 2902 |
*               /* a=0 b=0 c=0 -> specificity =   0 */
2903 | LI              /* a=0 b=0 c=1 -> specificity =   1 */
2904 | UL LI           /* a=0 b=0 c=2 -> specificity =   2 */
2905 | UL OL+LI        /* a=0 b=0 c=3 -> specificity =   3 */
2906 | H1 + *[REL=up]  /* a=0 b=1 c=1 -> specificity =  11 */
2907 | UL OL LI.red    /* a=0 b=1 c=3 -> specificity =  13 */
2908 | LI.red.level    /* a=0 b=2 c=1 -> specificity =  21 */
2909 | #x34y           /* a=1 b=0 c=0 -> specificity = 100 */
2910 | #s12:not(FOO)   /* a=1 b=0 c=1 -> specificity = 101 */
2911 | 
2912 |
2913 | 2914 |

Note: Repeated occurrances of the same 2915 | simple selector are allowed and do increase specificity. 2916 | 2917 |

Note: the specificity of the styles 2918 | specified in an HTML style attribute is described in CSS 2.1. 2919 | [CSS21]. 2920 | 2921 |

10. The grammar of Selectors

2922 | 2923 |

10.1. Grammar

2924 | 2925 |

The grammar below defines the syntax of Selectors. It is globally LL(1) 2926 | and can be locally LL(2) (but note that most UAs should not use it 2927 | directly, since it doesn't express the parsing conventions). The format of 2928 | the productions is optimized for human consumption and some shorthand 2929 | notations beyond Yacc (see [YACC]) are used: 2931 | 2932 |

2943 | 2944 |

The productions are: 2945 | 2946 |

selectors_group
2947 |   : selector [ COMMA S* selector ]*
2948 |   ;
2949 | 
2950 | selector
2951 |   : simple_selector_sequence [ combinator simple_selector_sequence ]*
2952 |   ;
2953 | 
2954 | combinator
2955 |   /* combinators can be surrounded by whitespace */
2956 |   : PLUS S* | GREATER S* | TILDE S* | S+
2957 |   ;
2958 | 
2959 | simple_selector_sequence
2960 |   : [ type_selector | universal ]
2961 |     [ HASH | class | attrib | pseudo | negation ]*
2962 |   | [ HASH | class | attrib | pseudo | negation ]+
2963 |   ;
2964 | 
2965 | type_selector
2966 |   : [ namespace_prefix ]? element_name
2967 |   ;
2968 | 
2969 | namespace_prefix
2970 |   : [ IDENT | '*' ]? '|'
2971 |   ;
2972 | 
2973 | element_name
2974 |   : IDENT
2975 |   ;
2976 | 
2977 | universal
2978 |   : [ namespace_prefix ]? '*'
2979 |   ;
2980 | 
2981 | class
2982 |   : '.' IDENT
2983 |   ;
2984 | 
2985 | attrib
2986 |   : '[' S* [ namespace_prefix ]? IDENT S*
2987 |         [ [ PREFIXMATCH |
2988 |             SUFFIXMATCH |
2989 |             SUBSTRINGMATCH |
2990 |             '=' |
2991 |             INCLUDES |
2992 |             DASHMATCH ] S* [ IDENT | STRING ] S*
2993 |         ]? ']'
2994 |   ;
2995 | 
2996 | pseudo
2997 |   /* '::' starts a pseudo-element, ':' a pseudo-class */
2998 |   /* Exceptions: :first-line, :first-letter, :before and :after. */
2999 |   /* Note that pseudo-elements are restricted to one per selector and */
3000 |   /* occur only in the last simple_selector_sequence. */
3001 |   : ':' ':'? [ IDENT | functional_pseudo ]
3002 |   ;
3003 | 
3004 | functional_pseudo
3005 |   : FUNCTION S* expression ')'
3006 |   ;
3007 | 
3008 | expression
3009 |   /* In CSS3, the expressions are identifiers, strings, */
3010 |   /* or of the form "an+b" */
3011 |   : [ [ PLUS | '-' | DIMENSION | NUMBER | STRING | IDENT ] S* ]+
3012 |   ;
3013 | 
3014 | negation
3015 |   : NOT S* negation_arg S* ')'
3016 |   ;
3017 | 
3018 | negation_arg
3019 |   : type_selector | universal | HASH | class | attrib | pseudo
3020 |   ;
3021 | 3022 |

10.2. Lexical scanner

3023 | 3024 |

The following is the tokenizer, written in Flex (see [FLEX]) notation. The 3026 | tokenizer is case-insensitive. 3027 | 3028 |

The two occurrences of "\377" represent the highest character number 3029 | that current versions of Flex can deal with (decimal 255). They should be 3030 | read as "\4177777" (decimal 1114111), which is the highest possible code 3031 | point in Unicode/ISO-10646. [UNICODE] 3033 | 3034 |

%option case-insensitive
3035 | 
3036 | ident     [-]?{nmstart}{nmchar}*
3037 | name      {nmchar}+
3038 | nmstart   [_a-z]|{nonascii}|{escape}
3039 | nonascii  [^\0-\177]
3040 | unicode   \\[0-9a-f]{1,6}(\r\n|[ \n\r\t\f])?
3041 | escape    {unicode}|\\[^\n\r\f0-9a-f]
3042 | nmchar    [_a-z0-9-]|{nonascii}|{escape}
3043 | num       [0-9]+|[0-9]*\.[0-9]+
3044 | string    {string1}|{string2}
3045 | string1   \"([^\n\r\f\\"]|\\{nl}|{nonascii}|{escape})*\"
3046 | string2   \'([^\n\r\f\\']|\\{nl}|{nonascii}|{escape})*\'
3047 | invalid   {invalid1}|{invalid2}
3048 | invalid1  \"([^\n\r\f\\"]|\\{nl}|{nonascii}|{escape})*
3049 | invalid2  \'([^\n\r\f\\']|\\{nl}|{nonascii}|{escape})*
3050 | nl        \n|\r\n|\r|\f
3051 | w         [ \t\r\n\f]*
3052 | 
3053 | D         d|\\0{0,4}(44|64)(\r\n|[ \t\r\n\f])?
3054 | E         e|\\0{0,4}(45|65)(\r\n|[ \t\r\n\f])?
3055 | N         n|\\0{0,4}(4e|6e)(\r\n|[ \t\r\n\f])?|\\n
3056 | O         o|\\0{0,4}(4f|6f)(\r\n|[ \t\r\n\f])?|\\o
3057 | T         t|\\0{0,4}(54|74)(\r\n|[ \t\r\n\f])?|\\t
3058 | V         v|\\0{0,4}(58|78)(\r\n|[ \t\r\n\f])?|\\v
3059 | 
3060 | %%
3061 | 
3062 | [ \t\r\n\f]+     return S;
3063 | 
3064 | "~="             return INCLUDES;
3065 | "|="             return DASHMATCH;
3066 | "^="             return PREFIXMATCH;
3067 | "$="             return SUFFIXMATCH;
3068 | "*="             return SUBSTRINGMATCH;
3069 | {ident}          return IDENT;
3070 | {string}         return STRING;
3071 | {ident}"("       return FUNCTION;
3072 | {num}            return NUMBER;
3073 | "#"{name}        return HASH;
3074 | {w}"+"           return PLUS;
3075 | {w}">"           return GREATER;
3076 | {w}","           return COMMA;
3077 | {w}"~"           return TILDE;
3078 | ":"{N}{O}{T}"("  return NOT;
3079 | @{ident}         return ATKEYWORD;
3080 | {invalid}        return INVALID;
3081 | {num}%           return PERCENTAGE;
3082 | {num}{ident}     return DIMENSION;
3083 | "<!--"           return CDO;
3084 | "-->"            return CDC;
3085 | 
3086 | \/\*[^*]*\*+([^/*][^*]*\*+)*\/                    /* ignore comments */
3087 | 
3088 | .                return *yytext;
3089 | 3090 |

11. Profiles

3091 | 3092 |

Each specification using Selectors must define the subset of Selectors 3093 | it allows and excludes, and describe the local meaning of all the 3094 | components of that subset. 3095 | 3096 |

Non normative examples: 3097 | 3098 |

3099 | 3100 | 3101 | 3102 | 3105 | 3110 | 3120 | 3138 |
Selectors profile 3103 | 3104 |
Specification 3106 | 3107 | CSS level 1 3108 | 3109 |
Accepts 3111 | 3112 | type selectors
3113 | class selectors
3114 | ID selectors
3115 | :link, :visited and :active pseudo-classes
3116 | descendant combinator
3117 | ::first-line and ::first-letter pseudo-elements 3118 | 3119 |
Excludes 3121 | 3122 | 3123 |

universal selector
3124 | attribute selectors
3125 | :hover and :focus pseudo-classes
3126 | :target pseudo-class
3127 | :lang() pseudo-class
3128 | all UI element states pseudo-classes
3129 | all structural pseudo-classes
3130 | negation pseudo-class
3131 | ::before and ::after pseudo-elements
3132 | child combinators
3133 | sibling combinators 3134 | 3135 |

namespaces 3136 | 3137 |

Extra constraints 3139 | 3140 | only one class selector allowed per sequence of simple selectors 3141 |
3142 |
3143 |
3144 | 3145 | 3146 | 3147 | 3148 | 3151 | 3156 | 3172 | 3185 |
Selectors profile 3149 | 3150 |
Specification 3152 | 3153 | CSS level 2 3154 | 3155 |
Accepts 3157 | 3158 | type selectors
3159 | universal selector
3160 | attribute presence and values selectors
3161 | class selectors
3162 | ID selectors
3163 | :link, :visited, :active, :hover, :focus, :lang() and :first-child 3164 | pseudo-classes
3165 | descendant combinator
3166 | child combinator
3167 | adjacent sibling combinator
3168 | ::first-line and ::first-letter pseudo-elements
3169 | ::before and ::after pseudo-elements 3170 | 3171 |
Excludes 3173 | 3174 | 3175 |

substring matching attribute selectors
3176 | :target pseudo-classes
3177 | all UI element states pseudo-classes
3178 | all structural pseudo-classes other than :first-child
3179 | negation pseudo-class
3180 | general sibling combinators 3181 | 3182 |

namespaces 3183 | 3184 |

Extra constraints 3186 | 3187 | more than one class selector per sequence of simple selectors (CSS1 3188 | constraint) allowed 3189 |
3190 | 3191 |

In CSS, selectors express pattern matching rules that determine which 3192 | style rules apply to elements in the document tree. 3193 | 3194 |

The following selector (CSS level 2) will match all anchors 3195 | a with attribute name set inside a section 1 3196 | header h1: 3197 | 3198 |

h1 a[name]
3199 | 3200 |

All CSS declarations attached to such a selector are applied to 3201 | elements matching it. 3202 |

3203 | 3204 |
3205 | 3206 | 3207 | 3208 | 3211 | 3216 | 3230 | 3236 |
Selectors profile 3209 | 3210 |
Specification 3212 | 3213 | STTS 3 3214 | 3215 |
Accepts 3217 | 3218 | 3219 |

type selectors
3220 | universal selectors
3221 | attribute selectors
3222 | class selectors
3223 | ID selectors
3224 | all structural pseudo-classes
3225 | all combinators 3226 | 3227 |

namespaces 3228 | 3229 |

Excludes 3231 | 3232 | non-accepted pseudo-classes
3233 | pseudo-elements
3234 | 3235 |
Extra constraints 3237 | 3238 | some selectors and combinators are not allowed in fragment 3239 | descriptions on the right side of STTS declarations. 3240 |
3241 | 3242 |

Selectors can be used in STTS 3 in two different manners: 3243 | 3244 |

    3245 |
  1. a selection mechanism equivalent to CSS selection mechanism: 3246 | declarations attached to a given selector are applied to elements 3247 | matching that selector, 3248 | 3249 |
  2. fragment descriptions that appear on the right side of declarations. 3250 |
3251 |
3252 | 3253 |

12. Conformance and 3254 | requirements

3255 | 3256 |

This section defines conformance with the present specification only. 3257 | 3258 |

The inability of a user agent to implement part of this specification 3259 | due to the limitations of a particular device (e.g., non interactive user 3260 | agents will probably not implement dynamic pseudo-classes because they 3261 | make no sense without interactivity) does not imply non-conformance. 3262 | 3263 |

All specifications reusing Selectors must contain a Profile listing the subset of Selectors it accepts 3265 | or excludes, and describing the constraints it adds to the current 3266 | specification. 3267 | 3268 |

Invalidity is caused by a parsing error, e.g. an unrecognized token or a 3269 | token which is not allowed at the current parsing point. 3270 | 3271 |

User agents must observe the rules for handling parsing errors: 3272 | 3273 |

3282 | 3283 |

Specifications reusing Selectors must define how to handle parsing 3284 | errors. (In the case of CSS, the entire rule in which the selector is used 3285 | is dropped.) 3286 | 3287 |

13. Tests

3288 | 3289 |

This specification has a test 3291 | suite allowing user agents to verify their basic conformance to the 3292 | specification. This test suite does not pretend to be exhaustive and does 3293 | not cover all possible combined cases of Selectors. 3294 | 3295 |

14. Acknowledgements

3296 | 3297 |

The CSS working group would like to thank everyone who has sent comments 3298 | on this specification over the years. 3299 | 3300 |

In particular, the working group would like to extend special thanks to 3301 | Donna McManus, Justin Baker, Joel Sklar, and Molly Ives Brower who 3302 | performed the final editorial review of the last call draft. The working 3303 | group would also like to thank Adam Kuehn, Boris Zbarsky, David Perrell, 3304 | Elliotte Harold, Matthew Raymond, Ruud Steltenpool, Patrick Garies, Anton 3305 | Prowse, and the W3C Internationalization Working Group for their last call 3306 | comments and kind words. 3307 | 3308 |

15. References

3309 | 3310 |

15.1. Normative 3311 | References

3312 | 3313 | 3314 | 3315 |
3316 |
3317 | 3318 | 3319 |
[CSS21] 3320 | 3321 |
Bert Bos; et al. Cascading Style 3323 | Sheets Level 2 Revision 1 (CSS 2.1) Specification. 07 June 2011. W3C Recommendation. URL: http://www.w3.org/TR/2011/REC-CSS2-20110607/ 3325 |
3326 | 3327 | 3328 |
[CSS3NAMESPACE] 3329 | 3330 |
Elika J. Etemad; Anne van Kesteren. CSS 3332 | Namespaces Module. 29 September 2011. W3C Recommendation. 3333 | URL: http://www.w3.org/TR/2011/REC-css3-namespace-20110929/ 3335 |
3336 | 3337 | 3338 |
[FLEX] 3339 | 3340 |
Flex: The Lexical Scanner Generator. Version 2.3.7, ISBN 3341 | 1882114213
3342 | 3343 | 3344 |
[UNICODE] 3345 | 3346 |
The Unicode Consortium. The 3347 | Unicode Standard, Version 6.0.0, (Mountain View, CA: The Unicode 3348 | Consortium, 2011. ISBN 978-1-936213-01-6) 3349 | and as updated from time to time by the publication 3350 | of new versions. (See 3351 | http://www.unicode.org/unicode/standard/versions/ for the latest 3352 | version and additional information on versions of the standard and of 3353 | the Unicode Character Database).
Available at 3354 | http://www.unicode.org/versions/Unicode6.0.0/ 3355 |
3356 | 3357 | 3358 |
[YACC] 3359 | 3360 |
S. C. Johnson. YACC - Yet another compiler compiler. 3361 | Murray Hill. 1975. Technical Report.
3362 | 3363 |
3364 | 3365 | 3366 |

15.2. Informative 3367 | References

3368 | 3369 | 3370 | 3371 |
3372 |
3373 | 3374 | 3375 |
[BCP47] 3376 | 3377 |
A. Phillips; M. DavisTags for Identifying Languages and Matching of Language Tags. September 2009. Internet Best 3379 | Current Practice 47. URL: http://www.rfc-editor.org/rfc/bcp/bcp47.txt 3381 |
3382 | 3383 | 3384 |
[CSS1] 3385 | 3386 |
Håkon Wium Lie; Bert Bos. Cascading Style 3388 | Sheets (CSS1) Level 1 Specification. 11 April 2008. W3C 3389 | Recommendation. URL: http://www.w3.org/TR/2008/REC-CSS1-20080411 3391 |
3392 | 3393 | 3394 |
[DOM-LEVEL-3-CORE] 3395 | 3396 |
Gavin Nicol; et al. Document 3398 | Object Model (DOM) Level 3 Core Specification. 7 April 2004. 3399 | W3C Recommendation. URL: http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407 3401 |
3402 | 3403 | 3404 |
[HTML401] 3405 | 3406 |
David Raggett; Ian Jacobs; Arnaud Le Hors. HTML 4.01 3408 | Specification. 24 December 1999. W3C Recommendation. URL: http://www.w3.org/TR/1999/REC-html401-19991224 3410 |
3411 | 3412 | 3413 |
[MATHML] 3414 | 3415 |
Patrick Ion; Robert Miner. Mathematical 3417 | Markup Language (MathML) 1.01 Specification. 7 July 1999. W3C 3418 | Recommendation. URL: http://www.w3.org/1999/07/REC-MathML-19990707 3420 |
3421 | 3422 | 3423 |
[STTS3] 3424 | 3425 |
Daniel Glazman. Simple 3426 | Tree Transformation Sheets 3. Electricité de France. 11 3427 | November 1998. Submission to the W3C. URL: http://www.w3.org/TR/NOTE-STTS3 3429 |
3430 | 3431 | 3432 |
[SVG11] 3433 | 3434 |
Erik Dahlström et. al. Scalable Vector 3436 | Graphics (SVG) 1.1 Specification. 16 August 2011. W3C 3437 | Recommendation. URL: http://www.w3.org/TR/2011/REC-SVG11-20110816/ 3439 |
3440 | 3441 | 3442 |
[UAX29] 3443 | 3444 |
Mark Davis. Text 3446 | Boundaries. 25 March 2005. Unicode Standard Annex #29. URL: http://www.unicode.org/unicode/reports/tr29/tr29-9.html 3448 |
3449 | 3450 | 3451 |
[XML-NAMES] 3452 | 3453 |
Tim Bray; et al. Namespaces 3455 | in XML 1.0 (Third Edition). 6 August 2009. W3C Proposed Edited 3456 | Recommendation. URL: http://www.w3.org/TR/2009/PER-xml-names-20090806 3458 |
3459 | 3460 | 3461 |
[XML10] 3462 | 3463 |
C. M. Sperberg-McQueen; et al. Extensible Markup 3465 | Language (XML) 1.0 (Fifth Edition). 10 February 1998. W3C 3466 | Proposed Edited Recommendation. Revised 5 February 2008 URL: http://www.w3.org/TR/2008/PER-xml-20080205 3468 |
3469 | 3470 |
3471 | 3472 | 3473 | 3474 | 3475 | --------------------------------------------------------------------------------