CSS Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.Anti-spam check. Do not fill this in! ===Selector=== {{Redirect|CSS class|non-CSS use of element classes in HTML|class attribute (HTML)}} In CSS, ''selectors'' declare which part of the markup a style applies to by matching tags and attributes in the markup itself. ====Selector types==== Selectors may apply to the following: *all [[HTML element|elements]] of a specific type, e.g. the second-level headers [[HTML element#Basic text|h2]] *elements specified by [[HTML attribute|attribute]], in particular: **''id'': an identifier unique within the document, denoted in the selector language by a [[number sign|hash]] prefix e.g. {{Code|code=#id}} **''class'': an identifier that can annotate multiple elements in a document, denoted by a [[period (punctuation)|dot]] prefix e.g. {{Code|code=.classname}} (the phrase "CSS class", although sometimes used, is a misnomer, as element classes—specified with the [[class attribute (HTML)|HTML class attribute]]—is a markup feature that is distinct from browsers' CSS subsystem and the related W3C/WHATWG standards work on document styles; see [[Resource Description Framework|RDF]] and [[microformat]]s for the origins of the "class" system of the Web content model) *elements depending on how they are placed relative to others in the [[Document Object Model|document tree]]. Classes and IDs are case-sensitive, start with letters, and can include alphanumeric characters, hyphens, and underscores. A class may apply to any number of instances of any element. An ID may only be applied to a single element. ====Pseudo-classes==== ''Pseudo-classes'' are used in CSS selectors to permit formatting based on information that is not contained in the document tree. One example of a widely used pseudo-class is {{code|lang=css|:hover}}, which identifies content only when the user "points to" the visible element, usually by holding the mouse cursor over it. It is appended to a selector as in {{code|lang=css|code=a:hover}} or {{code|lang=css|code=#elementid:hover}}. A pseudo-class classifies document elements, such as {{code|lang=css|code=:link}} or {{code|lang=css|code=:visited}}, whereas a ''pseudo-element'' makes a selection that may consist of partial elements, such as {{code|lang=css|code=::first-line}} or {{code|lang=css|code=::first-letter}}.<ref>{{cite web |url=https://www.w3.org/TR/CSS21/selector.html#pseudo-elements |title=W3C CSS2.1 specification for pseudo-elements and pseudo-classes |publisher=World Wide Web Consortium |date=7 June 2011 |access-date=30 April 2012 |archive-url=https://web.archive.org/web/20120430011514/https://www.w3.org/TR/CSS21/selector.html#pseudo-elements |archive-date=30 April 2012 |url-status=live }}</ref> Note the distinction between the double-colon notation used for pseudo-elements and the single-colon notation used for pseudo-classes. ====Combinators==== Multiple simple selectors may be joined using combinators to specify elements by location, element type, id, class, or any combination thereof.<ref>{{cite web |url-status=live |url=https://www.w3.org/TR/CSS21/selector.html |archive-url=https://web.archive.org/web/20060423174213/https://www.w3.org/TR/CSS21/selector.html |archive-date=2006-04-23 |title=Selectors |publisher=W3C |work=Cascading Style Sheets Level 2 Revision 1 (CSS 2.1) Specification}}</ref> The order of the selectors is important. For example, <syntaxhighlight inline lang="css" class=nowrap> div .myClass {color: red;}</syntaxhighlight> applies to all elements of class myClass that are inside div elements, whereas <syntaxhighlight inline lang="css" class=nowrap>.myClass div {color: red;}</syntaxhighlight> applies to all div elements that are inside elements of class myClass. This is not to be confused with concatenated identifiers such as <syntaxhighlight inline="" lang="css" class="nowrap"> div.myClass {color: red;}</syntaxhighlight> which applies to div elements of class myClass. ====Summary of selector syntax==== The following table provides a summary of selector syntax indicating usage and the version of CSS that introduced it.<ref>{{cite web |url=https://www.w3.org/TR/selectors/ |title=Selectors Level 3 |publisher=W3C |access-date=2014-05-30 |archive-url=https://web.archive.org/web/20140603165900/https://www.w3.org/TR/selectors/ |archive-date=2014-06-03 |url-status=live }}</ref> {| class="wikitable" |- ! Pattern !! Matches !! First defined<br/>in CSS level |- | {{code|2=css|1=E}} || an element of type E || 1 |- | {{code|2=css|1=E:link}} || an E element that is the source anchor of a hyperlink whose target is either not yet visited (:link) or already visited (:visited) || 1 |- | {{code|2=css|1=E:active}} || an E element during certain user actions || 1 |- | {{code|2=css|1=E::first-line}} || the first formatted line of an E element || 1 |- | {{code|2=css|1=E::first-letter}} || the first formatted letter of an E element || 1 |- | {{code|2=css|1=.c}} || all elements with class="c" || 1 |- | {{code|2=css|1=#myid}} || the element with id="myid" || 1 |- | {{code|2=css|1=E.warning}} || an E element whose class is "warning" (the document language specifies how class is determined) || 1 |- | {{code|2=css|1=E#myid}} || an E element with ID equal to "myid" || 1 |- | {{code|2=css|1=.c#myid}} || the element with class="c" and ID equal to "myid" || 1 |- | {{code|2=css|1=E F}} || an F element descendant of an E element || 1 |- | {{code|2=css|1=*}} || any element || 2 |- | {{code|2=css|1=E[foo]}} || an E element with a "foo" attribute || 2 |- | {{code|2=css|1=E[foo="bar"]}} || an E element whose "foo" attribute value is exactly equal to "bar" || 2 |- | {{code|2=css|1=E[foo~="bar"]}} || an E element whose "foo" attribute value is a list of whitespace-separated values, one of which is exactly equal to "bar" || 2 |- | {{code|2=css|1=E<nowiki>[foo|="en"]</nowiki>}} || an E element whose "foo" attribute has a hyphen-separated list of values beginning (from the left) with "en" || 2 |- | {{code|2=css|1=E:first-child}} || an E element, first child of its parent || 2 |- | {{code|2=css|1=E:lang(fr)}} || an element of type E in language "fr" (the document language specifies how language is determined) || 2 |- | {{code|2=css|1=E::before}} || generated content before an E element's content || 2 |- | {{code|2=css|1=E::after}} || generated content after an E element's content || 2 |- | {{code|2=css|1=E > F}} || an F element child of an E element || 2 |- | {{code|2=css|1=E + F}} || an F element immediately preceded by an E element || 2 |- | {{code|2=css|1=E[foo^="bar"]}} || an E element whose "foo" attribute value begins exactly with the string "bar" || 3 |- | {{code|2=css|1=E[foo$="bar"]}} || an E element whose "foo" attribute value ends exactly with the string "bar" || 3 |- | {{code|2=css|1=E[foo*="bar"]}} || an E element whose "foo" attribute value contains the substring "bar" || 3 |- | {{code|2=css|1=E:root}} || an E element, root of the document || 3 |- | {{code|2=css|1=E:nth-child(n)}} || an E element, the n-th child of its parent || 3 |- | {{code|2=css|1=E:nth-last-child(n)}} || an E element, the n-th child of its parent, counting from the last one || 3 |- | {{code|2=css|1=E:nth-of-type(n)}} || an E element, the n-th sibling of its type || 3 |- | {{code|2=css|1=E:nth-last-of-type(n)}} || an E element, the n-th sibling of its type, counting from the last one || 3 |- | {{code|2=css|1=E:last-child}} || an E element, last child of its parent || 3 |- | {{code|2=css|1=E:first-of-type}} || an E element, first sibling of its type || 3 |- | {{code|2=css|1=E:last-of-type}} || an E element, last sibling of its type || 3 |- | {{code|2=css|1=E:only-child}} || an E element, only child of its parent || 3 |- | {{code|2=css|1=E:only-of-type}} || an E element, only sibling of its type || 3 |- | {{code|2=css|1=E:empty}} || an E element that has no children (including text nodes) || 3 |- | {{code|2=css|1=E:target}} || an E element being the target of the referring URI || 3 |- | {{code|2=css|1=E:enabled}} || a user interface element E that is enabled || 3 |- | {{code|2=css|1=E:disabled}} || a user interface element E that is disabled || 3 |- | {{code|2=css|1=E:checked}} || a user interface element E that is checked (for instance a radio button or checkbox) || 3 |- | {{code|2=css|1=E:not(s)}} || an E element that does not match simple selector s || 3 |- | {{code|2=css|1=E ~ F}} || an F element preceded by an E element || 3 |} Summary: Please note that all contributions to Christianpedia may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here. You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see Christianpedia:Copyrights for details). Do not submit copyrighted work without permission! Cancel Editing help (opens in new window) Discuss this page