CSS Pseudo Cheat Sheet
CSS Pseudo Cheat Sheet
CSS Pseudo Cheat Sheet
Simple selectors
div {
Element element
}
ID #id #alpha { }
Universal * *{}
Two classes .first-class.second-class .alpha.beta { } All elements with classes alpha and beta
Element and class element.class p.alpha { } All alpha class elements inside <p>
Two elements element, element p, div { } All <p> and <div> elements
Two elements element element p div { } All <div> elements inside <p>
Descendant selectors/combinators
General Sibling element~element div ~ p { } All <p> element iterations after <div>
Attribute selectors
[href] {
[attribute] Selects all elements with a href attribute
}
[lang="fr"] {
[attribute=value] Selects all elements with lang attribute that has a value of "fr"
}
[attribute~=value] [input~=hello] { Elements with input attribute containing the whitespace separated substring "
Selector Syntax Example
[lang|=en] {
[attribute|=value] Elements with lang attribute value equal to "en" or "en-"(en hyphen)
}
a[href^="https"] {
[attribute^=value] Every <a> element with href attribute value begins with "https"
}
a[href$=".docx"] {
[attribute$=value] Every <a> element with href attribute value ends with ".docx"
}
a[href*="meta"] {
[attribute*=value] Every <a> element with href attribute value has substring "meta"
}
:first-child p:first-child { } All the <p> elements who are the first child of a parent element
:first-of-type p:first-of-type { } All the <p> element who are the first <p> element of a parent element
:last-child p:last-child { } All the <p> elements who are the last child of a parent element
:last-of-type p:last-of-type { } All the <p> elements who are the last <p> element of a parent element
:not(selector) :not(div) { } All the elements that are not a <div> element
Selector Syntax Example
:nth-child(n) div:nth-child(3) { } All the <p> elements that are the third child of a parent element
div:nth-last-child(3) All the <div> elements which are the third child of a parent element, counting f
:nth-last-child(n)
{} child element
:nth-last-of- p:nth-last-of-type(2)
The second sibling from the last child of a parent element.
type(n) {}
:only-of-type p:only-of-type { } All the <p> elements which are only <p> elements inside its parent
:only-child p:only-child { } All the <p> elements which are only child of a parent element
:required input:required { } Selects input elements with the "required" attribute specified
Pseudo-element selectors