Skip to main content

CSS Selectors Reference

v1.0.0

Searchable reference for CSS selectors, combinators, pseudo-classes, and pseudo-elements.

36 entries found

UniversalBasic Selectors

*

Selects all elements.

* { box-sizing: border-box; }
TypeBasic Selectors

element

Selects all elements of the given type.

p { color: blue; }
ClassBasic Selectors

.class

Selects all elements with the given class.

.card { padding: 1rem; }
IDBasic Selectors

#id

Selects the element with the given ID.

#hero { min-height: 100vh; }
AttributeBasic Selectors

[attr]

Selects elements with the specified attribute.

[disabled] { opacity: 0.5; }
Attribute ValueBasic Selectors

[attr='value']

Selects elements where attribute equals value.

[type='text'] { border: 1px solid; }
Attr Contains WordBasic Selectors

[attr~='value']

Selects elements where attribute contains the word.

[class~='active'] {}
Attr Starts WithBasic Selectors

[attr^='value']

Selects elements where attribute starts with value.

[href^='https'] { color: green; }
Attr Ends WithBasic Selectors

[attr$='value']

Selects elements where attribute ends with value.

[src$='.png'] {}
Attr ContainsBasic Selectors

[attr*='value']

Selects elements where attribute contains value as substring.

[href*='github'] {}
DescendantCombinators

A B

Selects B elements that are descendants of A.

nav a { text-decoration: none; }
ChildCombinators

A > B

Selects B that are direct children of A.

ul > li { list-style: disc; }
Adjacent SiblingCombinators

A + B

Selects B immediately following A.

h2 + p { margin-top: 0; }
General SiblingCombinators

A ~ B

Selects all B siblings that follow A.

h2 ~ p { color: gray; }
:hoverPseudo-classes

:hover

Element is being hovered by a pointing device.

a:hover { color: blue; }
:focusPseudo-classes

:focus

Element has received focus.

input:focus { outline: 2px solid blue; }
:activePseudo-classes

:active

Element is being activated (e.g. clicked).

button:active { transform: scale(0.98); }
:visitedPseudo-classes

:visited

Link has been visited.

a:visited { color: purple; }
:first-childPseudo-classes

:first-child

Element is the first child of its parent.

li:first-child { font-weight: bold; }
:last-childPseudo-classes

:last-child

Element is the last child of its parent.

li:last-child { border-bottom: none; }
:nth-child(n)Pseudo-classes

:nth-child(n)

Matches every nth child element.

tr:nth-child(even) { background: #f5f5f5; }
:not(selector)Pseudo-classes

:not(selector)

Negates the match.

li:not(.active) { opacity: 0.6; }
:is()Pseudo-classes

:is(selector)

Matches any of the given selectors.

:is(h1, h2, h3) { font-weight: bold; }
:where()Pseudo-classes

:where(selector)

Like :is() but contributes zero specificity.

:where(h1, h2) { margin-top: 0; }
:has()Pseudo-classes

:has(selector)

Matches if any of the selectors match descendants.

article:has(img) { display: grid; }
:checkedPseudo-classes

:checked

Input element is checked.

input:checked + label { color: green; }
:disabledPseudo-classes

:disabled

Form element is disabled.

button:disabled { opacity: 0.4; }
:placeholder-shownPseudo-classes

:placeholder-shown

Input whose placeholder is currently visible.

input:placeholder-shown { border-color: gray; }
:emptyPseudo-classes

:empty

Element has no children.

p:empty { display: none; }
:rootPseudo-classes

:root

The root element of the document.

:root { --color: #fff; }
::beforePseudo-elements

::before

Inserts content before the element.

p::before { content: '→ '; }
::afterPseudo-elements

::after

Inserts content after the element.

p::after { content: ' ←'; }
::placeholderPseudo-elements

::placeholder

Styles the placeholder text of an input.

input::placeholder { color: gray; }
::selectionPseudo-elements

::selection

Styles the highlighted text selection.

::selection { background: yellow; }
::first-linePseudo-elements

::first-line

Styles the first line of a block element.

p::first-line { font-weight: bold; }
::markerPseudo-elements

::marker

Styles the list item marker.

li::marker { color: red; }