CSS Pseudo cheat sheet

CSS Pseudo cheat sheet

Photo by Boitumelo on Unsplash

Simple selectors

SelectorSyntaxExample
Elementelementdiv {
Class.class.alpha { }
ID#id#alpha { }
Universal** { }

Variations of simple selectors

ElementsSyntaxExampleDescription
Two classes.first-class.second-class.alpha.beta { }All elements with classes alpha and beta
Element and classelement. classp.alpha { }All alpha class elements inside <p>
Two elementselement, elementp, div { }All <p> and <div> elements
Two elementselement elementp div { }All <div> elements inside <p>

Descendant selectors/combinators

SelectorSyntaxExampleDescription
Descendantelement elementdiv p { }All <p> descendants of <div>
Childelement>elementdiv > p { }All <p> direct descendants of <div>
Adjacent Siblingelement+elementdiv + p { }<p> element directly after <div>
General Siblingelement~elementdiv ~ p { }All <p> element iterations after <div>

Attribute selectors

SelectorSyntaxExample
[attribute][href] {Selects all elements with a href attribute
[attribute=value][lang="fr"] {Selects all elements with lang attribute that have a value of "fr"
[attribute~=value][input~=hello] {Elements with input attribute containing the whitespace-separated substring "hello"
[attribute\=value]**[lang
[attribute^=value]a[href^="https"] {Every <a> element with href attribute value begins with "https"
[attribute$=value]a[href$=".docx"] {Every <a> element with href attribute value ends with ".docx"
[attribute*=value]a[href*="meta"] {Every <a> element with href attribute value has substring "meta"
Pseudo-classExampleDescription of selection
: activea: active { }All active links
:checkedinput: checked { }All the checked <input> elements
: defaultinput: default { }All default <input> elements
:disabledinput: disabled { }All disabled <input> elements
: emptydiv: empty { }All the <div> elements with no children
: enabledinput: enabled { }All the enabled <input> elements
:first-childp:first-child { }All the <p> elements who are the first child of a parent element
:first-of-typep:first-of-type { }All the <p> elements who are the first <p> element of a parent element
: focusinput: focus { }Input element under focus
: fullscreen: fullscreen { }The element in full-screen mode
: hoverp: hover { }Action effect on mouse hover
:invalidinput: invalid { }Input elements with an invalid value
:last-childp:last-child { }All the <p> elements who are the last child of a parent element
:last-of-typep:last-of-type { }All the <p> elements who are the last <p> element of a parent element
: linka: link { }All unvisited links
:not(selector):not(div) { }All the elements that are not a <div> element
:nth-child(n)div:nth-child(3) { }All the <p> elements that are the third child of a parent element
:nth-last-child(n)div:nth-last-child(3) { }All the <div> elements which are the third child of a parent element, counting from the last child element
:nth-last-of-type(n)p:nth-last-of-type(2) { }The second sibling from the last child of a parent element.
:nth-of-type(n)p:nth-of-type(2) { }The second sibling of a parent element.
:only-of-typep:only-of-type { }All the <p> elements which are only <p> elements inside its parent
:only-childp:only-child { }All the <p> elements which are the only child of a parent element
: optionalinput: optional { }The input elements with no "required" attribute
: requiredinput: required { }Selects input elements with the "required" attribute specified
: root: root { }The Root element of the document
::selection::selection { }The portion of an element that is selected by a user
: validinput: valid { }All the input elements with a valid value
: visiteda: visited { }Select all visited links

Pseudo-element selectors

SyntaxExampleDescription
::afterp::after { }Inserts content after content of <p> element
::beforep::before { }Inserts content before content of <p> element
::first-letterp::first-letter { }Select the first letter of every <p> element
::first-linep::first-line { }Select the first line of every <p> element
::placeholderinput::placeholder { }Select input elements with the "placeholder" attribute specified
::marker::marker { }Selects markers in a list