CSS Selector Cheat Sheet

Basic Selectors

SelectorExampleDescription
Universal*Selects all elements
TypedivSelects all <div> elements
Class.classnameSelects all elements with class="classname"
ID#idnameSelects the element with id="idname"
Attribute[attribute]Selects all elements with the specified attribute

Combinators

SelectorExampleDescription
Descendantdiv pSelects all <p> elements inside <div> elements
Childdiv > pSelects all <p> elements where the parent is a <div> element
Adjacent Siblingdiv + pSelects the first <p> element that is placed immediately after <div> elements
General Siblingdiv ~ pSelects all <p> elements that are siblings of <div> elements

Pseudo-classes

SelectorExampleDescription
:hovera:hoverSelects links on mouse over
:activea:activeSelects the active link
:focusinput:focusSelects the input element which has focus
:first-childp:first-childSelects every <p> element that is the first child of its parent
:last-childp:last-childSelects every <p> element that is the last child of its parent
:nth-child(n)p:nth-child(2)Selects every <p> element that is the second child of its parent

Pseudo-elements

SelectorExampleDescription
::beforep::beforeInsert content before every <p> element
::afterp::afterInsert content after every <p> element
::first-letterp::first-letterSelects the first letter of every <p> element
::first-linep::first-lineSelects the first line of every <p> element

Attribute Selectors

SelectorExampleDescription
[attribute][target]Selects all elements with a target attribute
[attribute="value"][target="_blank"]Selects all elements with target="_blank"
[attribute~="value"][title~="flower"]Selects all elements with a title attribute containing the word "flower"
[attribute^="value"]a[href^="https"]Selects every <a> element whose href attribute value begins with "https"
[attribute$="value"]a[href$=".pdf"]Selects every <a> element whose href attribute value ends with ".pdf"
[attribute*="value"]a[href*="example"]Selects every <a> element whose href attribute value contains the substring "example"

Combining Selectors

SelectorExampleDescription
Multiple Selectorsdiv, pSelects all <div> elements and all <p> elements
Chaining Selectorsdiv.classnameSelects all <div> elements with class="classname"

Not Selector

SelectorExampleDescription
:not(selector)p:not(.intro)Selects all <p> elements that do not have class="intro"

Advanced Selectors

SelectorExampleDescription
::slotted()::slotted(p)Selects any <p> element placed inside a slot (used in Shadow DOM)
:host:hostSelects the shadow host element (the element to which the shadow DOM is attached)
:host():host(.special)Selects the shadow host if it matches the selector in parentheses
:host-context():host-context(h1)Selects the shadow host if it or any of its ancestors matches the selector
::part()::part(foo)Selects elements with a matching part attribute within a shadow tree
:is():is(header, main, footer) pSelects any <p> elements inside <header>, <main>, or <footer>
:where():where(header, main, footer) pSimilar to :is(), but with 0 specificity
:has()div:has(> img)Selects <div> elements that contain at least one <img> child
:focus-withinform:focus-withinSelects a <form> if it or any of its descendants have focus
:placeholder-showninput:placeholder-shownSelects <input> elements that are currently showing placeholder text
:fullscreen:fullscreenSelects the element that's currently in fullscreen mode
:dir():dir(rtl)Selects elements based on their directionality (ltr or rtl)
:lang():lang(fr)Selects elements based on their language
:root:rootSelects the document's root element (usually <html>)
:emptyp:emptySelects <p> elements that have no children (including text nodes)
:target#section1:targetSelects the element with id="section1" if it's the current target element
:checkedinput:checkedSelects all checked <input> elements
:indeterminateinput:indeterminateSelects <input> elements that are in an indeterminate state
:validinput:validSelects all <input> elements with a valid value
:invalidinput:invalidSelects all <input> elements with an invalid value
:optionalinput:optionalSelects <input> elements with no "required" attribute
:requiredinput:requiredSelects <input> elements with the "required" attribute specified
:read-onlyinput:read-onlySelects <input> elements with the "readonly" attribute specified
:read-writeinput:read-writeSelects <input> elements that are not read-only
:in-rangeinput:in-rangeSelects <input> elements with a value within the specified range
:out-of-rangeinput:out-of-rangeSelects <input> elements with a value outside the specified range