CSS Selectors Cheat Sheet

Basics selectors

.css3-class {
  font-weight: bold;
}
SelectorDescription
*All elements
divElement
.classClass
#idID
[disabled]Attribute
[role="dialog"]Attribute

Combinators

SelectorDescription
.parent .childDescendant
.parent > .childDirect descendant
.child + .siblingAdjacent sibling
.child ~ .siblingFar sibling
.class1.class2Have both classes
.class1,.class2.class1 or .class2

Attribute selectors

SelectorDescription
[role="dialog"]= Exact
[class~="box"]~= Has word
[class|="box"]|= Exact or prefix (eg, value-)
[href$=".doc"]$= Ends in
[href^="/index"]^= Begins with
[class*="-is-"]*= Contains

Pseudo-classes

SelectorDescription
:targeteg, h2#foo:target
:focus
:active
:nth-child(3)3rd child.
:nth-child(3n+2)2nd child in groups of 3. Using a formula (an + b). Description: a represents a cycle size, n is a counter (starts at 0), and b is an offset value.
:nth-child(-n+4)4, 3, 2, 1 less than 5.
:nth-last-child(2)
:nth-of-type(2)
:checkedChecked inputs
:disabledDisabled elements
:enabledEnabled elements
:defaultDefault element in a group
:emptyElements without children

Pseudo-class variations

Selector
:first-of-type
:last-of-type
:nth-of-type(2)
:only-of-type
:first-child
:last-child
:nth-child(2)
:only-child
::first-letter
::first-line