GadgetForge

GadgetForge

CSS Selectors Cheat Sheet - Complete Reference


CSS Selectors Cheat Sheet

Quick reference table of all major CSS selectors — basic combinators, attribute selectors, pseudo-classes, and pseudo-elements.

SelectorExampleDescription
.class.introSelects all elements with class="intro"
.class1.class2.name1.name2Selects elements that have both name1 and name2 in their class attribute
.class1 .class2.name1 .name2Selects elements with class name2 that are descendants of elements with class name1
#id#firstnameSelects the element with id="firstname"
**Selects all elements
elementpSelects all <p> elements
element.classp.introSelects all <p> elements with class="intro"
element,elementdiv, pSelects all <div> and all <p> elements
element elementdiv pSelects all <p> elements inside <div> elements (descendant)
element > elementdiv > pSelects all <p> elements whose direct parent is a <div> (child combinator)
element + elementdiv + pSelects the first <p> element placed immediately after a <div> (adjacent sibling)
element1 ~ element2p ~ ulSelects every <ul> element preceded by a <p> element (general sibling combinator)
[attribute][target]Selects all elements that have a target attribute
[attribute=value][target="_blank"]Selects elements with target="_blank"
[attribute~=value][title~="flower"]Selects elements whose title attribute contains the word "flower"
[attribute|=value][lang|="en"]Selects elements with lang equal to "en" or starting with "en-" (e.g. "en-US")
[attribute^=value]a[href^="https"]Selects <a> elements whose href begins with "https"
[attribute$=value]a[href$=".pdf"]Selects <a> elements whose href ends with ".pdf"
[attribute*=value]a[href*="w3schools"]Selects <a> elements whose href contains the substring "w3schools"
:activea:activeSelects the link being clicked (active state)
::afterp::afterInserts content after the content of each <p> element
::beforep::beforeInserts content before the content of each <p> element
:checkedinput:checkedSelects every checked <input> (checkbox/radio)
:defaultinput:defaultSelects the default form element (e.g. default submit button)
:disabledinput:disabledSelects every disabled <input> element
:emptyp:emptySelects <p> elements that have no children (including text nodes)
:enabledinput:enabledSelects every enabled <input> element
:first-childp:first-childSelects every <p> that is the first child of its parent
::first-letterp::first-letterSelects the first letter of every <p> element
::first-linep::first-lineSelects the first line of every <p> element
:first-of-typep:first-of-typeSelects every <p> that is the first <p> of its parent
:focusinput:focusSelects the <input> element that currently has focus
:fullscreen:fullscreenSelects the element currently in full-screen mode
:has()h2:has(+ p)Selects <h2> elements that have a <p> immediately after them
:hovera:hoverSelects links when the mouse is over them
:in-rangeinput:in-rangeSelects <input> elements with value within a specified range
:indeterminateinput:indeterminateSelects <input> elements in an indeterminate state (e.g. progress bar, checkbox group)
:invalidinput:invalidSelects <input> elements with an invalid value
:lang()p:lang(it)Selects <p> elements with lang="it" (Italian)
:last-childp:last-childSelects every <p> that is the last child of its parent
:last-of-typep:last-of-typeSelects every <p> that is the last <p> of its parent
:linka:linkSelects all unvisited links
::marker::markerSelects the markers (bullets/numbers) of list items
:not():not(p)Selects every element that is not a <p>
:nth-child()p:nth-child(2)Selects every <p> that is the 2nd child of its parent
:nth-last-child()p:nth-last-child(2)Selects every <p> that is the 2nd child counting from the end
:nth-last-of-type()p:nth-last-of-type(2)Selects every <p> that is the 2nd <p> counting from the end
:nth-of-type()p:nth-of-type(2)Selects every <p> that is the 2nd <p> of its parent
:only-of-typep:only-of-typeSelects <p> elements that are the only <p> of their parent
:only-childp:only-childSelects <p> elements that are the only child of their parent
:optionalinput:optionalSelects <input> elements with no required attribute
:out-of-rangeinput:out-of-rangeSelects <input> elements with value outside a specified range
::placeholderinput::placeholderStyles the placeholder text of <input> and <textarea>
:read-onlyinput:read-onlySelects <input> elements with the readonly attribute
:read-writeinput:read-writeSelects <input> elements without the readonly attribute
:requiredinput:requiredSelects <input> elements with the required attribute
:root:rootSelects the document's root element (<html>)
::selection::selectionStyles the portion of an element selected by the user
:target#news:targetSelects the element whose ID matches the current URL fragment (anchor target)
:validinput:validSelects <input> elements with a valid value
:visiteda:visitedSelects all visited links