CSS Selectors Cheat Sheet

Basics selectors

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

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, role = "dialog"
[class~="box"]~= Has word, class contains word "box"
[class|="box"]|= Exact or prefix (eg, value-)
[href$=".doc"]$= Ends in, href ends in ".doc"
[href^="/index"]^= Begins with, href begins with "/index"
[class*="-is-"]*= Contains, class contains string "-is-"

Pseudo-classes

SelectorDescription
:targetRepresents a unique element (the target element) with an id matching the URL's fragment. eg, h2#foo:target
:focusFocused element
:activeActive element
: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)Elements based on their position among a group of siblings, counting from the end.
:nth-of-type(2)Elements based on their position among siblings of the same type (tag name).
:checkedChecked inputs
:disabledDisabled elements
:enabledEnabled elements
:defaultDefault element in a group
:emptyElements without children

Pseudo-class variations

SelectorDescription
:first-of-typeRepresents the first element of its type among a group of sibling elements.
:last-of-typeRepresents the last element of its type among a group of sibling elements.
:only-of-typeRepresents an element that has no siblings of the same type.
:first-childRepresents the first element among a group of sibling elements.
:last-childRepresents the last element among a group of sibling elements.
:only-childRepresents an element without any siblings. This is the same as :first-child:last-child or :nth-child(1):nth-last-child(1), but with a lower specificity.
::first-letterApplies styles to the first letter of the first line of a block container, but only when not preceded by other content (such as images or inline tables).
::first-lineApplies styles to the first line of a block container.

Test your css selector skill with Pagetual

Picker