CSS selectors define the elements to which a set of CSS rules apply. They are similar to XPath in that they select a set of elements.
- Type selector
- Selects all elements that match the given node name.
inputwill match any
- Class selector
- Selects all elements that have the given
.indexwill match any element that has a class of "index".
- ID selector
- Selects an element based on the value of its
idattribute. There should be only one element with a given ID in a document.
#tocwill match the element that has the ID "toc".
- Universal selector
- Selects all elements. Optionally, it may be restricted to a specific namespace or to all namespaces.
*will match all the elements of the document.
- Attribute selector
- Selects elements based on the value of the given attribute.
[autoplay]will match all elements that have the
autoplayattribute set (to any value).
- Adjacent sibling combinator
+combinator selects adjacent siblings. This means that the second element directly follows the first, and both share the same parent.
A + B
h2 + pwill match all
<p>elements that directly follow an
- General sibling combinator
~combinator selects siblings. This means that the second element follows the first (though not necessarily immediately), and both share the same parent.
A ~ B
p ~ spanwill match all
<span>elements that follow a
- Child combinator
>combinator selects nodes that are direct children of the first element.
A > B
ul > liwill match all
<li>elements that are nested directly inside a
- Descendant combinator
div spanwill match all
<span>elements that are inside a
- Column combinator
||combinator selects nodes which belong to a column.
A || B
col || tdwill match all
<td>elements that belong to the scope of the
- Pseudo-classes allow the selection of elements based on state information that is not contained in the document tree.
a:visitedwill match all
<a>elements that have been visited by the user.
- Pseudo-elements represent entities that are not included in HTML.
p::first-linewill match the first line of all
|Selectors Level 4||Working Draft||Added the
|Selectors Level 3||Recommendation||Added the
Made pseudo-elements use a
|CSS Level 2 (Revision 1)||Recommendation||Added the
Added the universal and attribute selectors.
|CSS Level 1||Recommendation||Initial definition.|