Utilumo
LightDarkSystem

Updated June 29, 2026

CSS selectors cheat sheet

Selectors decide which elements a CSS rule applies to. They range from simple type and class selectors to combinators and pseudo-classes that match by relationship or state.

Basic selectors

SelectorWhat it matches
*Every element.
pAll <p> elements (type selector).
.btnElements with class "btn".
#mainThe element with id "main".
[type="email"]Elements with that attribute value.
a, buttonBoth <a> and <button> (grouping).

Combinators

SelectorWhat it matches
div pAny <p> inside a <div> (descendant).
div > pA <p> that is a direct child of a <div>.
h2 + pThe <p> immediately after an <h2>.
h2 ~ pAll <p> siblings after an <h2>.

Pseudo-classes

SelectorWhat it matches
:hoverAn element under the pointer.
:focusA focused element.
:first-childThe first child of its parent.
:last-childThe last child of its parent.
:nth-child(2n)Every second child.
:not(.active)Elements without class "active".

Pseudo-elements

SelectorWhat it matches
::beforeInserted content before an element.
::afterInserted content after an element.
::placeholderAn input's placeholder text.
::selectionThe portion the user has highlighted.
Specificity, brieflyWhen rules conflict, more specific selectors win: an id beats a class, which beats a type selector. Keep selectors as simple as the design allows to avoid specificity battles.

References

Questions

What is the difference between a class and an id selector?

A class (.name) can apply to many elements, while an id (#name) should be unique on the page. Ids also have higher specificity, so they override class rules.

What does :nth-child do?

It matches elements by their position among siblings. :nth-child(2n) selects every second element, and :nth-child(3) selects the third.