English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
CSS selectors are patterns used to select the styles of the elements you want.
"CSS" column shows the attribute definitions in CSS versions (CSS1, CSS2, or for CSS3)
Selector | Example | Description | CSS Version |
---|---|---|---|
.class | .intro | Select all elements with class="intro" | 1 |
#id | #firstname | Select all elements with id="firstname" | 1 |
* | * | Select all elements | 2 |
element | p | Select all <p> elements | 1 |
element,element | div,p | Select all <div> elements and <p> elements | 1 |
element element | div p | Select all <p> elements within a <div> element | 1 |
element>element | div>p | Select all <p> elements that are children of a <div> element | 2 |
element+element | div+p | Select all <p> elements immediately following a <div> element | 2 |
[attribute] | [target] | Select all elements with a target attribute | 2 |
[attribute=value] | [target=-blank] | Select all elements using target="-elements with "blank" | 2 |
[attribute~=value] | [title~=flower] | Select all elements with a title attribute containing the word "flower" | 2 |
[attribute|=language] | [lang|=en] | Select all elements with a lang attribute starting with "en" | 2 |
[attribute^=value] | a[src^="https"] | Select every element with a src attribute value starting with "https" | 3 |
[attribute$=value] | a[src$=".pdf"] | Select every element with a src attribute value ending with ".pdf" | 3 |
[attribute*=value] | a[src*="w3codebox] | Select every element with a src attribute value containing the substring "w"3elements with "codebox" | 3 |
:link | a:link | Select all unvisited links | 1 |
:visited | a:visited | Select all visited links | 1 |
:active | a:active | Select the active link | 1 |
:hover | a:hover | Select when the mouse is over the link | 1 |
:focus | input:focus | Select the input element with focus | 2 |
:first-letter | p:first-letter | Select every first letter of each <p> element | 1 |
:first-line | p:first-line | Select every first line of each <p> element | 1 |
:first-child | p:first-child | Specify the style for only when the <p> element is the first child of its parent. | 2 |
:before | p:before | Insert content before each <p> element | 2 |
:after | p:after | Insert content after each <p> element | 2 |
:lang(language) | p:lang(it) | Selects all <p> elements with a starting value of 'it' for the lang attribute | 2 |
element1~element2 | p~ul | Selects each ul element following a p element | 3 |
:first-of-type | p:first-of-type | Selects each p element that is the first p element of its parent | 3 |
:last-of-type | p:last-of-type | Selects each p element that is the last p element of its parent | 3 |
:only-of-type | p:only-of-type | Selects each p element that is the only p element of its parent | 3 |
:only-child | p:only-child | Selects each p element that is the only child of its parent | 3 |
:nth-child(n) | p:nth-child(2) | Selects each p element that is the second child of its parent | 3 |
:nth-last-child(n) | p:nth-last-child(2) | Selects each p element that is the second child of its parent, counting from the last child | 3 |
:nth-of-type(n) | p:nth-of-type(2) | Selects each p element that is the second child of its parent | 3 |
:nth-last-of-type(n) | p:nth-last-of-type(2) | Selects each p element that is the second p element of its parent, counting from the last child | 3 |
:last-child | p:last-child | Selects each p element that is the last child of its parent | 3 |
:root | :root | Selects the root element of the document | 3 |
:empty | p:empty | Selects each p element that has no child elements (including text nodes) | 3 |
:target | #news:target | Selects the currently active #news element (including the URL of the anchor clicked with that name) | 3 |
:enabled | input:enabled | Selects each enabled input element | 3 |
:disabled | input:disabled | Selects each disabled input element | 3 |
:checked | input:checked | Selects each selected input element | 3 |
:not(selector) | :not(p) | Selects each element that is not a p element | 3 |
::selection | ::selection | Matches the selected or highlighted part of the element by the user | 3 |
:out-of-range | :out-of-range | Matches input elements with values outside the specified range | 3 |
:in-range | :in-range | Matches input elements with values within the specified range | 3 |
:read-write | :read-write | Used to match elements that are both readable and writable | 3 |
:read-only | :read-only | Used to match elements with 'readonly' (read-only) attribute set | 3 |
:optional | :optional | Used to match optional input elements | 3 |
:required | :required | Used to match elements with 'required' attribute set | 3 |
:valid | :valid | Used to match elements with legal input values | 3 |
:invalid | :invalid | Used to match elements with illegal input values | 3 |