Home Tools Blog About

CSS Selectors Cheat Sheet

This CSS selectors cheat sheet lists the selectors you use every day, from classes and IDs to combinators, attribute selectors, and pseudo-classes. To tidy your stylesheet afterwards, use the CSS Beautifier or CSS Minifier.

Basic selectors

SelectorMatches
pEvery element of that type
.classElements with that class
#idThe element with that id
*Every element
a, pEither selector (grouping)

Combinators

SelectorMatches
div pp anywhere inside div (descendant)
div > pp that is a direct child of div
h2 + pp immediately after an h2
h2 ~ pevery p that follows an h2

Attribute selectors

SelectorMatches
[type]Has the attribute
[type="text"]Attribute equals a value
[href^="https"]Value starts with
[href$=".pdf"]Value ends with
[class*="btn"]Value contains

Pseudo-classes and elements

SelectorMatches
:hoverOn mouse over
:focusThe focused element
:first-childFirst child of its parent
:nth-child(2n)Every second child
:not(.x)Everything except .x
::beforeInserted content before
::afterInserted content after

Frequently asked questions

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

A class with a dot can be used on many elements, while an id with a hash should be unique to one element on the page.

What does the > combinator do?

It selects only direct children, so div > p matches a p that sits immediately inside the div, not deeper.

What is the difference between :nth-child and :first-child?

:first-child matches only the first, while :nth-child takes a formula like 2n to match a pattern of positions.

ATV

Written by Nick (ATV Team)

We build and maintain the 600+ free, client-side tools on this site, and every guide is written against the tools themselves: each figure is computed and checked before it is published, and every linked tool is tested in the browser. More about how we work on the about page, and the full library of guides lives on the blog.