Selectors

Note: the table contains most common CSS selectors.

Selector Example Example description Selector type
* div * Selects all elements inside <div> Universal selector
element div Selects all <div> elements Type selector
.class .intro Selects all elements with class="intro" Class selector
#id #firstname Selects the element with id="firstname" ID selector
element p Selects all <p> elements Type selector
element, element div, p Selects all <div> elements and all <p> elements Type selector
element element div p Selects all <p> elements inside <div> elements (descendants) Descendant combinator
element > element div > p Selects all <p> elements where the parent is a <div> element (direct descendants) Child combinator
element + element div + p Any <p> element that is the next sibling of a <div> element (that is: the next child of the same parent) In other words, selects any <p> that is placed immediately after <div> element. Adjacent sibling combinator
element ~ element p ~ ul Selects every <ul> element that are preceded by a <p> element General sibling combinator
[attribute] [target], p[id] Selects all elements that have a target attribute; selects all <p> elements that have an id attribute Attribute selector
[attribute=value] [target="_blank"] Selects all elements with target="_blank" Attribute selector
[attribute~=value] [title~="flower"] Selects all elements with a title attribute containing the word "flower" Attribute selector
[attribute|=value] class|="top" Selects all elements with a class attribute that has a hyphen-separated list of values beginning (from the left) with "top".
The value has to be a whole word, either alone, like class="top", or followed by a hyphen( - ), like class="top-text"!
Attribute selector
[attribute^=value] a[href^="https"] Selects every <a> element whose href attribute value begins exactly with "https" Attribute selector
[attribute$=value] a[href$=".pdf"] Selects every <a> element whose href attribute value ends with ".pdf" Attribute selector
[attribute*=value] a[href*="w3schools"] Selects every <a> element whose href attribute value contains the substring "w3schools". The value does not have to be a whole word! Attribute selector
:active a:active Selects the active link The user action pseudo-classes
::after p::after Insert something after the content of each <p> element The ::after pseudo-element
::before h1::before { content: "Chapter "; color: gray; } Insert something before the content of each <p> element The ::before pseudo-element
:checked input:checked Selects every checked <input> element The UI element states pseudo-classes
:disabled input:disabled Selects every disabled <input> element The UI element states pseudo-classes
:empty p:empty Selects every <p> element that has no children (including text nodes) Structural pseudo-classes
:enabled input:enabled Selects every enabled <input> element The UI element states pseudo-classes
:first-child p:first-child Selects every <p> element that is the first child of its parent Structural pseudo-classes
::first-letter p::first-letter Selects the first letter of every <p> element The ::first-letter pseudo-element
::first-line p::first-line Selects the first line of every <p> element The ::first-line pseudo-element
:first-of-type p:first-of-type Selects every <p> element that is the first <p> element of its parent Structural pseudo-classes
:focus input:focus Selects the input element which has focus The user action pseudo-classes
:hover a:hover Selects links on mouse over The user action pseudo-classes
:in-range input:in-range Selects input elements with a value within a specified range
:invalid input:invalid Selects all input elements with an invalid value The :invalid pseudo-class
:lang(language) p:lang(it) Selects every <p> element with a lang attribute equal to "it" (Italian) The :lang() pseudo-class
:last-child p:last-child Selects every <p> element that is the last child of its parent Structural pseudo-classes
:last-of-type p:last-of-type Selects every <p> element that is the last <p> element of its parent Structural pseudo-classes
:link a:link Selects all unvisited links The link pseudo-classes
:not(selector) :not(p) Selects every element that is not a <p> element Negation pseudo-class
:nth-child(n) p:nth-child(2) Selects every <p> element that is the second child of its parent Structural pseudo-classes (n can be number | An+B | even | odd)
:nth-last-child(n) p:nth-last-child(2) Selects every <p> element that is the second child of its parent, counting from the last child Structural pseudo-classes (n can be number | An+B | even | odd)
:nth-last-of-type(n) p:nth-last-of-type(2) Selects every <p> element that is the second <p> element of its parent, counting from the last child Structural pseudo-classes (n can be number | An+B | even | odd)
:nth-of-type(n) p:nth-of-type(2) Selects every <p> element that is the second <p> element of its parent Structural pseudo-classes (n can be number | An+B | even | odd)
:only-of-type p:only-of-type Selects every <p> element that is the only <p> element of its parent Structural pseudo-classes
:only-child p:only-child Selects every <p> element that is the only child of its parent Structural pseudo-classes
:optional input:optional Selects input elements with no "required" attribute
:out-of-range input:out-of-range Selects input elements with a value outside a specified range
:read-only input:read-only Selects input elements with the "readonly" attribute specified
:read-write input:read-write Selects input elements with the "readonly" attribute NOT specified
:required input:required Selects input elements with the "required" attribute specified
:root :root Selects the document's root element Structural pseudo-classes
::selection ::selection Selects the portion of an element that is selected by a user
:target #news:target Selects the current active #news element (clicked on a URL containing that anchor name)
:valid input:valid Selects all input elements with a valid value
:visited a:visited Selects all visited links The user action pseudo-classes
FOR FULL TABLE HEIGHT IN IFRAME