Lesson 115 +5 XP

CSS Selector Reference

CSS Selector Reference

Every selector you need, in one table.

Universal & element selectors

SelectorMeaningExample
*Every element* { box-sizing: border-box; }
p, h1...All of that tagp { }

Class & id

SelectorMeaningExample
.classElements with that class.center { }
#idUnique element by id#header { }
p.center<p> that ALSO has .centerp.center { }

Grouping

SelectorMeaningExample
h1, h2, h3One rule, many elementsh1, h2 { color: navy; }

Attribute selectors

SelectorMatchesExample
[attr]With the attribute[disabled] { }
[attr=value]Exact valueinput[type="text"] { }
[attr^=value]Starts with[href^="https"] { }
[attr$=value]Ends with[href$=".pdf"] { }
[attr*=value]Contains[title*="card"] { }
[attr~=value]Space-separated word[class~="box"] { }

Combinators

CombinatorMeaningExample
a b (space)Descendant (any depth)div p { }
a > bDirect child onlyul > li { }
a + bNext sibling after ah2 + p { }
a ~ bAny following siblingh2 ~ p { }

Pseudo-classes (state)

SelectorMatches a stateExample
:hoverMouse overa:hover { }
:focusKeyboard focusinput:focus { }
:activeBeing clickeda:active { }
:visitedVisited linksa:visited { }
:first-childFirst childli:first-child { }
:last-childLast childli:last-child { }
:nth-child(n)Nth childli:nth-child(2) { }
:nth-of-type(n)Nth of its tagp:nth-of-type(2) { }
:not(x)Everything except:not(p) { }
:checkedTickled gemsinput:checked { }
:disabled / :enabledForm statesinput:disabled { }
:emptyNo childrendiv:empty { }
:rootThe html element:root { --x: 1; }
:is()/:where()`List of selectors:is(h1,h2,h3) { }
:has()"Parent of" (modern)div:has(> img) { }

Pseudo-elements (virtual parts)

SelectorStylesExample
::beforeContent before element.badge::before { content: "★"; }
::afterContent after element.link::after { content: "→"; }
::first-lineFirst line of textp::first-line { }
::first-letterFirst letterp::first-letter { }
::selectionUser-selected text::selection { }
::markerList bullet/numberli::marker { }
::placeholderInput placeholderinput::placeholder { }
::backdropBehind modalsdialog::backdrop { }

TL;DR

  • Know .class, #id, element, and grouping cold.
  • Attribute selectors query by attribute.
  • Combinators: space, >, +, ~.
  • States: :hover, :focus, :nth-child, :not.
  • Pieces: ::before, ::after, ::selection...
  • Bookmark; this page is your selector dictionary.