--- title: CSS selectors slug: Web/CSS/CSS_Selectors tags: - CSS - NeedsTranslation - Overview - Reference - Selectors - TopicStub translation_of: Web/CSS/CSS_Selectors ---
CSS selectors define the elements to which a set of CSS rules apply.
Note: There are no selectors or combinators to select parent items, siblings of parents, or children of parent siblings.
*
ns|*
*|*
*
will match all the elements of the document.elementname
input
will match any {{HTMLElement("input")}} element.class
attribute..classname
.index
will match any element that has a class of "index".id
attribute. There should be only one element with a given ID in a document.#idname
#toc
will match the element that has the ID "toc".[attr]
[attr=value]
[attr~=value]
[attr|=value]
[attr^=value]
[attr$=value]
[attr*=value]
[autoplay]
will match all elements that have the autoplay
attribute set (to any value).,
is a grouping method, it selects all the matching nodes.A, B
div, span
will match both {{HTMLElement("span")}} and {{HTMLElement("div")}} elements.
(space) combinator selects nodes that are descendants of the first element.A B
div span
will match all {{HTMLElement("span")}} elements that are inside a {{HTMLElement("div")}} element.>
combinator selects nodes that are direct children of the first element.A > B
ul > li
will match all {{HTMLElement("li")}} elements that are nested directly inside a {{HTMLElement("ul")}} element.~
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 ~ span
will match all {{HTMLElement("span")}} elements that follow a {{HTMLElement("p")}}, immediately or not.+
combinator selects adjacent siblings. This means that the second element directly follows the first, and both share the same parent.A + B
h2 + p
will match all {{HTMLElement("p")}} elements that directly follow an {{HTMLElement("h2")}}.||
combinator selects nodes which belong to a column.A || B
col || td
will match all {{HTMLElement("td")}} elements that belong to the scope of the {{HTMLElement("col")}}.:
pseudo allow the selection of elements based on state information that is not contained in the document tree.a:visited
will match all {{HTMLElement("a")}} elements that have been visited by the user.::
pseudo represent entities that are not included in HTML.p::first-line
will match the first line of all {{HTMLElement("p")}} elements.Specification | Status | Comment |
---|---|---|
{{SpecName("CSS4 Selectors")}} | {{Spec2("CSS4 Selectors")}} | Added the || column combinator, grid structural selectors, logical combinators, location, time-demensional, resource state, linguistic and UI pseudo-classes, modifier for ASCII case-sensitive and case-insensitive attribute value selection. |
{{SpecName("CSS3 Selectors")}} | {{Spec2("CSS3 Selectors")}} | Added the ~ general sibling combinator and tree-structural pseudo-classes.Made pseudo-elements use a :: double-colon prefix. Additional attribute selectors |
{{SpecName("CSS2.1", "selector.html")}} | {{Spec2("CSS2.1")}} | Added the > child and + adjacent sibling combinators.Added the universal and attribute selectors. |
{{SpecName("CSS1")}} | {{Spec2("CSS1")}} | Initial definition. |
See the pseudo-class and pseudo-element specification tables for details on those.