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 withtype="text").

CSS Selectors Quick Reference

SelectorExampleDescription
elementpSelects all <p> elements
.class.introSelects all elements with class="intro"
#id#firstnameSelects the element with id="firstname"
**Selects all elements
element,elementdiv, pSelects all <div> and <p> elements
element elementdiv pSelects all <p> elements inside <div> elements
element>elementdiv > pSelects all <p> where parent is <div>
element+elementdiv + pSelects <p> immediately after <div>
element1~element2p ~ ulSelects 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"
:activea:activeSelects the active link
:checkedinput:checkedSelects checked input elements
:disabledinput:disabledSelects disabled input elements
:emptyp:emptySelects every <p> with no children
:enabledinput:enabledSelects enabled input elements
:first-childp:first-childSelects every <p> that is first child
:first-of-typep:first-of-typeSelects every <p> that is first of its type
:focusinput:focusSelects input element with focus
:hovera:hoverSelects links on mouse over
:last-childp:last-childSelects every <p> that is last child
:last-of-typep:last-of-typeSelects 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-typep:only-of-typeSelects every <p> that is only of its type
:only-childp:only-childSelects every <p> that is only child
:optionalinput:optionalSelects input elements with no "required" attribute
:requiredinput:requiredSelects input elements with "required" attribute
:root:rootSelects the document's root element
::selection::selectionSelects portion of element selected by user
:target#news:targetSelects current active #news element
:validinput:validSelects input elements with valid value
:invalidinput:invalidSelects input elements with invalid value

CSS Selectors Quick Reference

SelectorExampleDescription
elementpSelects all <p> elements
.class.introSelects all elements with class="intro"
#id#firstnameSelects the element with id="firstname"
**Selects all elements
element,elementdiv, pSelects all <div> and <p> elements
element elementdiv pSelects all <p> elements inside <div> elements
element>elementdiv > pSelects all <p> where parent is <div>
element+elementdiv + pSelects <p> immediately after <div>
element1~element2p ~ ulSelects 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"
:activea:activeSelects the active link
:checkedinput:checkedSelects checked input elements
:disabledinput:disabledSelects disabled input elements
:emptyp:emptySelects every <p> with no children
:enabledinput:enabledSelects enabled input elements
:first-childp:first-childSelects every <p> that is first child
:first-of-typep:first-of-typeSelects every <p> that is first of its type
:focusinput:focusSelects input element with focus
:hovera:hoverSelects links on mouse over
:last-childp:last-childSelects every <p> that is last child
:last-of-typep:last-of-typeSelects 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-typep:only-of-typeSelects every <p> that is only of its type
:only-childp:only-childSelects every <p> that is only child
:optionalinput:optionalSelects input elements with no "required" attribute
:requiredinput:requiredSelects input elements with "required" attribute
:root:rootSelects the document's root element
::selection::selectionSelects portion of element selected by user
:target#news:targetSelects current active #news element
:validinput:validSelects input elements with valid value
:invalidinput:invalidSelects input elements with invalid value

CSS Selectors Quick Reference

SelectorExampleDescription
elementpSelects all <p> elements
.class.introSelects all elements with class="intro"
#id#firstnameSelects the element with id="firstname"
**Selects all elements
element,elementdiv, pSelects all <div> and <p> elements
element elementdiv pSelects all <p> elements inside <div> elements
element>elementdiv > pSelects all <p> where parent is <div>
element+elementdiv + pSelects <p> immediately after <div>
element1~element2p ~ ulSelects 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"
:activea:activeSelects the active link
:checkedinput:checkedSelects checked input elements
:disabledinput:disabledSelects disabled input elements
:emptyp:emptySelects every <p> with no children
:enabledinput:enabledSelects enabled input elements
:first-childp:first-childSelects every <p> that is first child
:first-of-typep:first-of-typeSelects every <p> that is first of its type
:focusinput:focusSelects input element with focus
:hovera:hoverSelects links on mouse over
:last-childp:last-childSelects every <p> that is last child
:last-of-typep:last-of-typeSelects 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-typep:only-of-typeSelects every <p> that is only of its type
:only-childp:only-childSelects every <p> that is only child
:optionalinput:optionalSelects input elements with no "required" attribute
:requiredinput:requiredSelects input elements with "required" attribute
:root:rootSelects the document"s root element
::selection::selectionSelects portion of element selected by user
:target#news:targetSelects current active #news element
:validinput:validSelects input elements with valid value
:invalidinput:invalidSelects input elements with invalid value

CSS Selectors Quick Reference

SelectorExampleDescription
elementpSelects all <p> elements
.class.introSelects all elements with class="intro"
#id#firstnameSelects the element with id="firstname"
**Selects all elements
element,elementdiv, pSelects all <div> and <p> elements
element elementdiv pSelects all <p> elements inside <div> elements
element>elementdiv > pSelects all <p> where parent is <div>
element+elementdiv + pSelects <p> immediately after <div>
element1~element2p ~ ulSelects 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"
:activea:activeSelects the active link
:checkedinput:checkedSelects checked input elements
:disabledinput:disabledSelects disabled input elements
:emptyp:emptySelects every <p> with no children
:enabledinput:enabledSelects enabled input elements
:first-childp:first-childSelects every <p> that is first child
:first-of-typep:first-of-typeSelects every <p> that is first of its type
:focusinput:focusSelects input element with focus
:hovera:hoverSelects links on mouse over
:last-childp:last-childSelects every <p> that is last child
:last-of-typep:last-of-typeSelects 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-typep:only-of-typeSelects every <p> that is only of its type
:only-childp:only-childSelects every <p> that is only child
:optionalinput:optionalSelects input elements with no "required" attribute
:requiredinput:requiredSelects input elements with "required" attribute
:root:rootSelects the document's root element
::selection::selectionSelects portion of element selected by user
:target#news:targetSelects current active #news element
:validinput:validSelects input elements with valid value
:invalidinput:invalidSelects input elements with invalid value

Basic Selectors

Element Selector

p { color: blue; }

This paragraph is styled with element selector

This paragraph is not targeted

Class Selector

.highlight { background: yellow; }
This text has highlight classRegular text

ID Selector

#main-title { font-size: 2em; color: purple; }

Main Title with ID

Regular subtitle

Universal Selector

* { margin: 0; padding: 0; box-sizing: border-box; }

Often used for CSS resets to remove default margins and padding

Combinators

Descendant Selector

div p { color: green; }

This paragraph is inside a div (styled)

This paragraph is not inside a div (not styled)

Child Selector

ul > li { border: 1px solid red; }
  • Direct child (styled)
  • Direct child (styled)
    • Nested child (not styled)

Adjacent Sibling Selector

h4 + p { font-weight: bold; }

Section Title

This paragraph is immediately after h4 (styled)

This paragraph is not immediately after h4 (not styled)

General Sibling Selector

h4 ~ p { color: orange; }

Section Title

This paragraph is after h4 (styled)

A div in between

This paragraph is also after h4 (styled)

Pseudo-classes

:hover

button:hover { background: #007bff; color: white; }

:focus

input:focus { border-color: blue; box-shadow: 0 0 5px blue; }

:nth-child()

tr:nth-child(odd) { background: #f0f0f0; }
Item 1 (odd)
Item 2 (even)
Item 3 (odd)
Item 4 (even)

:first-child & :last-child

li:first-child { color: red; }
li:last-child { color: blue; }
  • First item (red)
  • Middle item
  • Last item (blue)

Attribute Selectors

[attribute]

a[target] { color: purple; }

[attribute=value]

input[type="text"] { border: 2px solid blue; }

[attribute^=value]

a[href^="https" { color: green; }

[attribute$=value]

a[href$=".pdf"] { color: red; }

Additional Selectors

Pseudo-elements

p::first-line { font-weight: bold; }
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

p:not(.special) { opacity: 0.7; }

This is a regular paragraph

This is a special paragraph

This is another regular paragraph

Multiple Selectors

h1, h2, h3 { font-family: sans-serif; }

Heading 1

Heading 2

Heading 3

This paragraph is not selected

:only-child Selector

div:only-child { background: lightblue; }

This div is an only child (styled)

This div has siblings (not styled)

This div has siblings (not styled)

CSS Selectors Quick Reference

SelectorExampleDescription
elementpSelects all <p> elements
.class.introSelects all elements with class="intro"
#id#firstnameSelects the element with id="firstname"
**Selects all elements
element,elementdiv, pSelects all <div> and <p> elements
element elementdiv pSelects all <p> elements inside <div> elements
element>elementdiv > pSelects all <p> where parent is <div>
element+elementdiv + pSelects <p> immediately after <div>
element1~element2p ~ ulSelects 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'
:activea:activeSelects the active link
:checkedinput:checkedSelects checked input elements
:disabledinput:disabledSelects disabled input elements
:emptyp:emptySelects every <p> with no children
:enabledinput:enabledSelects enabled input elements
:first-childp:first-childSelects every <p> that is first child
:first-of-typep:first-of-typeSelects every <p> that is first of its type
:focusinput:focusSelects input element with focus
:hovera:hoverSelects links on mouse over
:last-childp:last-childSelects every <p> that is last child
:last-of-typep:last-of-typeSelects 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-typep:only-of-typeSelects every <p> that is only of its type
:only-childp:only-childSelects every <p> that is only child
:optionalinput:optionalSelects input elements with no "required" attribute
:requiredinput:requiredSelects input elements with "required" attribute
:root:rootSelects the document's root element
::selection::selectionSelects portion of element selected by user
:target#news:targetSelects current active #news element
:validinput:validSelects input elements with valid value
:invalidinput:invalidSelects input elements with invalid value
< PreviousNext >