CSS Selectors Reference
A complete guide to CSS selectors with interactive examples
CSS Selectors
CSS selectors are used to choose which HTML elements you want to style.
There are five main types:
- Simple selectors – select elements by their name (like
<p>
),id, or class. - Combinator selectors – select elements based on how they are related (like a child inside a parent).
- Pseudo-class selectors – select elements in a special state (like when a link is hovered).
- Pseudo-element selectors – select a part of an element (like the first letter of a paragraph).
- Attribute selectors – select elements based on their attributes (like an input with
type="text"
).
CSS Selectors Quick Reference
Selector | Example | Description |
---|---|---|
element | p | Selects all <p> elements |
.class | .intro | Selects all elements with class="intro" |
#id | #firstname | Selects the element with id="firstname" |
* | * | Selects all elements |
element,element | div, p | Selects all <div> and <p> elements |
element element | div p | Selects all <p> elements inside <div> elements |
element>element | div > p | Selects all <p> where parent is <div> |
element+element | div + p | Selects <p> immediately after <div> |
element1~element2 | p ~ ul | Selects every <ul> preceded by <p> |
[attribute] | [target] | Selects elements with target attribute |
[attribute=value] | [target=_blank] | Selects elements with target="_blank" |
[attribute~=value] | [title~=flower] | Selects elements with title containing "flower" |
[attribute|=value] | [lang|=en] | Selects elements with lang attribute equal to "en" or starting with "en-" |
[attribute^=value] | a[href^="https"] | Selects elements whose href attribute value begins with "https" |
[attribute$=value] | a[href$=".pdf"] | Selects elements whose href attribute value ends with ".pdf" |
[attribute*=value] | a[href*="chillucoder"] | Selects elements whose href attribute value contains "chillucoder" |
:active | a:active | Selects the active link |
:checked | input:checked | Selects checked input elements |
:disabled | input:disabled | Selects disabled input elements |
:empty | p:empty | Selects every <p> with no children |
:enabled | input:enabled | Selects enabled input elements |
:first-child | p:first-child | Selects every <p> that is first child |
:first-of-type | p:first-of-type | Selects every <p> that is first of its type |
:focus | input:focus | Selects input element with focus |
:hover | a:hover | Selects links on mouse over |
:last-child | p:last-child | Selects every <p> that is last child |
:last-of-type | p:last-of-type | Selects every <p> that is last of its type |
:not(selector) | :not(p) | Selects every element that is not <p> |
:nth-child(n) | p:nth-child(2) | Selects every <p> that is second child |
:nth-last-child(n) | p:nth-last-child(2) | Selects every <p> that is second child from last |
:nth-last-of-type(n) | p:nth-last-of-type(2) | Selects every <p> that is second of its type from last |
:nth-of-type(n) | p:nth-of-type(2) | Selects every <p> that is second of its type |
:only-of-type | p:only-of-type | Selects every <p> that is only of its type |
:only-child | p:only-child | Selects every <p> that is only child |
:optional | input:optional | Selects input elements with no "required" attribute |
:required | input:required | Selects input elements with "required" attribute |
:root | :root | Selects the document's root element |
::selection | ::selection | Selects portion of element selected by user |
:target | #news:target | Selects current active #news element |
:valid | input:valid | Selects input elements with valid value |
:invalid | input:invalid | Selects input elements with invalid value |
CSS Selectors Quick Reference
Selector | Example | Description |
---|---|---|
element | p | Selects all <p> elements |
.class | .intro | Selects all elements with class="intro" |
#id | #firstname | Selects the element with id="firstname" |
* | * | Selects all elements |
element,element | div, p | Selects all <div> and <p> elements |
element element | div p | Selects all <p> elements inside <div> elements |
element>element | div > p | Selects all <p> where parent is <div> |
element+element | div + p | Selects <p> immediately after <div> |
element1~element2 | p ~ ul | Selects every <ul> preceded by <p> |
[attribute] | [target] | Selects elements with target attribute |
[attribute=value] | [target=_blank] | Selects elements with target="_blank" |
[attribute~=value] | [title~=flower] | Selects elements with title containing "flower" |
[attribute|=value] | [lang|=en] | Selects elements with lang attribute equal to "en" or starting with "en-" |
[attribute^=value] | a[href^="https"] | Selects elements whose href attribute value begins with "https" |
[attribute$=value] | a[href$=".pdf"] | Selects elements whose href attribute value ends with ".pdf" |
[attribute*=value] | a[href*="chillucoder"] | Selects elements whose href attribute value contains "chillucoder" |
:active | a:active | Selects the active link |
:checked | input:checked | Selects checked input elements |
:disabled | input:disabled | Selects disabled input elements |
:empty | p:empty | Selects every <p> with no children |
:enabled | input:enabled | Selects enabled input elements |
:first-child | p:first-child | Selects every <p> that is first child |
:first-of-type | p:first-of-type | Selects every <p> that is first of its type |
:focus | input:focus | Selects input element with focus |
:hover | a:hover | Selects links on mouse over |
:last-child | p:last-child | Selects every <p> that is last child |
:last-of-type | p:last-of-type | Selects every <p> that is last of its type |
:not(selector) | :not(p) | Selects every element that is not <p> |
:nth-child(n) | p:nth-child(2) | Selects every <p> that is second child |
:nth-last-child(n) | p:nth-last-child(2) | Selects every <p> that is second child from last |
:nth-last-of-type(n) | p:nth-last-of-type(2) | Selects every <p> that is second of its type from last |
:nth-of-type(n) | p:nth-of-type(2) | Selects every <p> that is second of its type |
:only-of-type | p:only-of-type | Selects every <p> that is only of its type |
:only-child | p:only-child | Selects every <p> that is only child |
:optional | input:optional | Selects input elements with no "required" attribute |
:required | input:required | Selects input elements with "required" attribute |
:root | :root | Selects the document's root element |
::selection | ::selection | Selects portion of element selected by user |
:target | #news:target | Selects current active #news element |
:valid | input:valid | Selects input elements with valid value |
:invalid | input:invalid | Selects input elements with invalid value |
CSS Selectors Quick Reference
Selector | Example | Description |
---|---|---|
element | p | Selects all <p> elements |
.class | .intro | Selects all elements with class="intro" |
#id | #firstname | Selects the element with id="firstname" |
* | * | Selects all elements |
element,element | div, p | Selects all <div> and <p> elements |
element element | div p | Selects all <p> elements inside <div> elements |
element>element | div > p | Selects all <p> where parent is <div> |
element+element | div + p | Selects <p> immediately after <div> |
element1~element2 | p ~ ul | Selects every <ul> preceded by <p> |
[attribute] | [target] | Selects elements with target attribute |
[attribute=value] | [target=_blank] | Selects elements with target="_blank" |
[attribute~=value] | [title~=flower] | Selects elements with title containing "flower" |
[attribute|=value] | [lang|=en] | Selects elements with lang attribute equal to "en" or starting with "en-" |
[attribute^=value] | a[href^="https"] | Selects elements whose href attribute value begins with "https" |
[attribute$=value] | a[href$=".pdf"] | Selects elements whose href attribute value ends with ".pdf" |
[attribute*=value] | a[href*="chillucoder"] | Selects elements whose href attribute value contains "chillucoder" |
:active | a:active | Selects the active link |
:checked | input:checked | Selects checked input elements |
:disabled | input:disabled | Selects disabled input elements |
:empty | p:empty | Selects every <p> with no children |
:enabled | input:enabled | Selects enabled input elements |
:first-child | p:first-child | Selects every <p> that is first child |
:first-of-type | p:first-of-type | Selects every <p> that is first of its type |
:focus | input:focus | Selects input element with focus |
:hover | a:hover | Selects links on mouse over |
:last-child | p:last-child | Selects every <p> that is last child |
:last-of-type | p:last-of-type | Selects every <p> that is last of its type |
:not(selector) | :not(p) | Selects every element that is not <p> |
:nth-child(n) | p:nth-child(2) | Selects every <p> that is second child |
:nth-last-child(n) | p:nth-last-child(2) | Selects every <p> that is second child from last |
:nth-last-of-type(n) | p:nth-last-of-type(2) | Selects every <p> that is second of its type from last |
:nth-of-type(n) | p:nth-of-type(2) | Selects every <p> that is second of its type |
:only-of-type | p:only-of-type | Selects every <p> that is only of its type |
:only-child | p:only-child | Selects every <p> that is only child |
:optional | input:optional | Selects input elements with no "required" attribute |
:required | input:required | Selects input elements with "required" attribute |
:root | :root | Selects the document"s root element |
::selection | ::selection | Selects portion of element selected by user |
:target | #news:target | Selects current active #news element |
:valid | input:valid | Selects input elements with valid value |
:invalid | input:invalid | Selects input elements with invalid value |
CSS Selectors Quick Reference
Selector | Example | Description |
---|---|---|
element | p | Selects all <p> elements |
.class | .intro | Selects all elements with class="intro" |
#id | #firstname | Selects the element with id="firstname" |
* | * | Selects all elements |
element,element | div, p | Selects all <div> and <p> elements |
element element | div p | Selects all <p> elements inside <div> elements |
element>element | div > p | Selects all <p> where parent is <div> |
element+element | div + p | Selects <p> immediately after <div> |
element1~element2 | p ~ ul | Selects every <ul> preceded by <p> |
[attribute] | [target] | Selects elements with target attribute |
[attribute=value] | [target=_blank] | Selects elements with target="_blank" |
[attribute~=value] | [title~=flower] | Selects elements with title containing "flower" |
[attribute|=value] | [lang|=en] | Selects elements with lang attribute equal to "en" or starting with "en-" |
[attribute^=value] | a[href^="https"] | Selects elements whose href attribute value begins with "https" |
[attribute$=value] | a[href$='.pdf'] | Selects elements whose href attribute value ends with ".pdf" |
[attribute*=value] | a[href*="chillucoder"] | Selects elements whose href attribute value contains "chillucoder" |
:active | a:active | Selects the active link |
:checked | input:checked | Selects checked input elements |
:disabled | input:disabled | Selects disabled input elements |
:empty | p:empty | Selects every <p> with no children |
:enabled | input:enabled | Selects enabled input elements |
:first-child | p:first-child | Selects every <p> that is first child |
:first-of-type | p:first-of-type | Selects every <p> that is first of its type |
:focus | input:focus | Selects input element with focus |
:hover | a:hover | Selects links on mouse over |
:last-child | p:last-child | Selects every <p> that is last child |
:last-of-type | p:last-of-type | Selects every <p> that is last of its type |
:not(selector) | :not(p) | Selects every element that is not <p> |
:nth-child(n) | p:nth-child(2) | Selects every <p> that is second child |
:nth-last-child(n) | p:nth-last-child(2) | Selects every <p> that is second child from last |
:nth-last-of-type(n) | p:nth-last-of-type(2) | Selects every <p> that is second of its type from last |
:nth-of-type(n) | p:nth-of-type(2) | Selects every <p> that is second of its type |
:only-of-type | p:only-of-type | Selects every <p> that is only of its type |
:only-child | p:only-child | Selects every <p> that is only child |
:optional | input:optional | Selects input elements with no "required" attribute |
:required | input:required | Selects input elements with "required" attribute |
:root | :root | Selects the document's root element |
::selection | ::selection | Selects portion of element selected by user |
:target | #news:target | Selects current active #news element |
:valid | input:valid | Selects input elements with valid value |
:invalid | input:invalid | Selects input elements with invalid value |
Basic Selectors
Element Selector
This paragraph is styled with element selector
This paragraph is not targeted
Class Selector
ID Selector
Main Title with ID
Regular subtitle
Universal Selector
Often used for CSS resets to remove default margins and padding
Combinators
Descendant Selector
This paragraph is inside a div (styled)
This paragraph is not inside a div (not styled)
Child Selector
- Direct child (styled)
- Direct child (styled)
- Nested child (not styled)
Adjacent Sibling Selector
Section Title
This paragraph is immediately after h4 (styled)
This paragraph is not immediately after h4 (not styled)
General Sibling Selector
Section Title
This paragraph is after h4 (styled)
This paragraph is also after h4 (styled)
Pseudo-classes
:hover
:focus
:nth-child()
Item 1 (odd) |
Item 2 (even) |
Item 3 (odd) |
Item 4 (even) |
:first-child & :last-child
li:last-child { color: blue; }
- First item (red)
- Middle item
- Last item (blue)
Attribute Selectors
[attribute=value]
Additional Selectors
Pseudo-elements
p::first-letter { font-size: 2em; }
This is a paragraph that demonstrates pseudo-elements. The first line is bold and the first letter is larger.
:not() Selector
This is a regular paragraph
This is a special paragraph
This is another regular paragraph
Multiple Selectors
Heading 1
Heading 2
Heading 3
This paragraph is not selected
:only-child Selector
This div is an only child (styled)
This div has siblings (not styled)
This div has siblings (not styled)
CSS Selectors Quick Reference
Selector | Example | Description |
---|---|---|
element | p | Selects all <p> elements |
.class | .intro | Selects all elements with class="intro" |
#id | #firstname | Selects the element with id="firstname" |
* | * | Selects all elements |
element,element | div, p | Selects all <div> and <p> elements |
element element | div p | Selects all <p> elements inside <div> elements |
element>element | div > p | Selects all <p> where parent is <div> |
element+element | div + p | Selects <p> immediately after <div> |
element1~element2 | p ~ ul | Selects every <ul> preceded by <p> |
[attribute] | [target] | Selects elements with target attribute |
[attribute=value] | [target=_blank] | Selects elements with target="_blank" |
[attribute~=value] | [title~=flower] | Selects elements with title containing "flower" |
[attribute|=value] | [lang|=en] | Selects elements with lang attribute equal to "en" or starting with "en-" |
[attribute^=value] | a[href^="https"] | Selects elements whose href attribute value begins with 'https' |
[attribute$=value] | a[href$=".pdf"] | Selects elements whose href attribute value ends with ".pdf" |
[attribute*=value] | a[href*="chillucoder"] | Selects elements whose href attribute value contains 'chillucoder' |
:active | a:active | Selects the active link |
:checked | input:checked | Selects checked input elements |
:disabled | input:disabled | Selects disabled input elements |
:empty | p:empty | Selects every <p> with no children |
:enabled | input:enabled | Selects enabled input elements |
:first-child | p:first-child | Selects every <p> that is first child |
:first-of-type | p:first-of-type | Selects every <p> that is first of its type |
:focus | input:focus | Selects input element with focus |
:hover | a:hover | Selects links on mouse over |
:last-child | p:last-child | Selects every <p> that is last child |
:last-of-type | p:last-of-type | Selects every <p> that is last of its type |
:not(selector) | :not(p) | Selects every element that is not <p> |
:nth-child(n) | p:nth-child(2) | Selects every <p> that is second child |
:nth-last-child(n) | p:nth-last-child(2) | Selects every <p> that is second child from last |
:nth-last-of-type(n) | p:nth-last-of-type(2) | Selects every <p> that is second of its type from last |
:nth-of-type(n) | p:nth-of-type(2) | Selects every <p> that is second of its type |
:only-of-type | p:only-of-type | Selects every <p> that is only of its type |
:only-child | p:only-child | Selects every <p> that is only child |
:optional | input:optional | Selects input elements with no "required" attribute |
:required | input:required | Selects input elements with "required" attribute |
:root | :root | Selects the document's root element |
::selection | ::selection | Selects portion of element selected by user |
:target | #news:target | Selects current active #news element |
:valid | input:valid | Selects input elements with valid value |
:invalid | input:invalid | Selects input elements with invalid value |