Selectors, Pseudo Classes, and Pseudo Elements
The following selector types, pseudo classes, and pseudo elements are available for use in CSS 2.1.Selector:
Adjacent sibling selectorDescription:
Selects an adjacent sibling (one that is directly adjacent to the first defined selector and that shares the same parents)Example:
Tips:
h1 + p {
text-indent: 0;
}
This selector is unavailable for use in Internet Explorer and, therefore, is used primarily for enhancing styles in browsers that do support it, or for hacks.Selector:
Attribute selectorDescription:
Selects an element based on its attributesExample:
Tips:
acronym [title] {
color: red;
}
This selector is unavailable for use in Internet Explorer and, therefore, is used primarily for enhancing styles in browsers that do support it, or for hacks.Selector:
Child selectorDescription:
Selects the children of a given elementExample:
Tips:
#content > p {
padding: 10px;
}
This selector is unavailable for use in Internet Explorer and, therefore, is used primarily for enhancing styles in browsers that do support it, or for hacks.Selector:
Class selectorDescription:
Allows the creation of a custom class. Called in the HTML or XHTML using the class attribute within the element to be selected.Example:
Tips:
.notation {
font-size: xx-small;
}
This selector is widely supported and, therefore, overused. Avoid overuse by streamlining CSS with descendant selectors instead.Selector:
Descendant selectorDescription:
Selects all the descendant elements of a parent elementExample:
Tips:
#content p {
font-family: Arial, Helvetica, sans-serif;
}
This selector is widely supported and should be used as often as possible, to reduce reliance on class selectors.Selector:
ID selectorDescription:
Allows the creation of a uniquely identified selector. Called in the HTML or XHTML using the id attribute within the element to be selected. Can be used only once per document.Example:
Tips:
#content {
margin-left: 25px;
margin-right: 25px;
}
This selector is widely supported and typically used to describe sections of a document used for layout. It is also used to bind elements to scripts in DHTML.Selector:
Universal selectorDescription:
Selects all elementsExample:
Tips:
* {
border: 1px dashed blue;
}
This selector is very helpful when working with diagnostics, but it should be avoided for general use. Some problems with universal selector behavior exist in Internet Explorer versions.Selector:
Element (type) selectorDescription:
A selector matching the element typeExample:
Tips:
h1 {
font-size: 22px;
}
Very widely supported, this is the most commonly used selector.Selector:
:active dynamic pseudo classDescription:
Selects an element while that element is being activated by the userExample:
Tips:
a:active {
color: red;
}
This selector is widely supported. When styling links, you must follow the LoVe/HAte order of link, visited, hover, active, or results might be inconsistent.Selector:
:after pseudo elementDescription:
Used to insert generated text after the selected elementExample:
Tips:
a:after {
content: link;
}
This selector is unavailable for use in Internet Explorer and, therefore, is used primarily for enhancing styles in browsers that do support it.Selector:
:before pseudo elementDescription:
Used to insert generated text before the selected elementExample:
Tips:
a:before {
content: link;
}
This selector is unavailable for use in Internet Explorer and, therefore, is used primarily for enhancing styles in browsers that do support it.Selector:
:firstchild pseudo classDescription:
Used to select the first child of an element onlyExample:
Tips:
p:firstchild em {
font-weight: bold;
}
This selector is unavailable for use in Internet Explorer and, therefore, is used primarily for enhancing styles in browsers that do support it.Selector:
:firstletter pseudo elementDescription:
Used to select the first letter of an element onlyExample:
Tips:
#content p:firstletter {
font-size: larger;
}
Good support exists in all contemporary browsers. This selector is helpful in creating nonimage-based drop caps.Selector:
:firstline pseudo elementDescription:
Used to select the first line of an element onlyExample:
Tips:
#content p:firstline {
color: red;
}
Good support exists in all contemporary browsers. This selector applies the style to the first line. If the line length changes because of browser resize, the style is still applied to whatever the first line is.Selector:
:focus dynamic pseudo classDescription:
Applies the style when an element has focus (is accepting keyboard input, such as in a form)Example:
Tips:
input:focus {
border: 1px solid red;
}
This selector is unavailable for use in Internet Explorer and, therefore, is used primarily for enhancing styles in browsers that do support it.Selector:
:hover dynamic pseudo classDescription:
Applies the style when an element is hovered over with the mouse or pointing deviceExample:
Tips:
#toggle:hover {
border: 1px solid green;
}
This selector is unavailable for use in Internet Explorer except as applied to the a element. Therefore, it is used primarily for enhancing styles in browsers that do support it.Selector:
:lang pseudo classDescription:
Selects an element based on its languageExample:
Tips:
p:lang(de) {
quotes: '»' '«' '\2039' '\203A'
}
Inconsistent support exists. This selector is used for multilingual documents and internationalization.Selector:
:link link pseudo classDescription:
Selects a link in the normal stateExample:
Tips:
a:link {
color: #ccc;
}
This selector is very widely supported. Remember the LoVe/HAte order rule, to avoid inconsistent behavior.Selector:
:visited link pseudo classDescription:
Selects a link in the visited stateExample:
Tips:
a:visited {
color: #333;
}
This selector is very widely supported. Remember the LoVe/HAte order rule, to avoid inconsistent behavior.