CSS Selector Cheat Sheet

Sometimes you need custom CSS. This CSS reference table demonstrates the different selectors and will help you style your content.

Data referenced from w3schools.com.

SelectorExampleExample DescriptionCommon Use
.class.introSelects all elements with class=”intro”Style elements with a specific class, like headings or sections.
.class1.class2.name1.name2Selects all elements with both name1 and name2 set within its class attributeApply combined styling to elements with multiple classes.
.class1 .class2.name1 .name2Selects all elements with name2 that is a descendant of an element with name1Style child elements within a specific parent container.
#id#firstnameSelects the element with id=”firstname”Target a unique element for styling or scripting.
**Selects all elementsApply global styles like resetting margins or padding.
elementpSelects all <p> elementsStyle all instances of a specific HTML tag.
element.classp.introSelects all <p> elements with class=”intro”Style specific elements within a class.
element,elementdiv, pSelects all <div> elements and all <p> elementsApply the same styles to multiple elements.
element elementdiv pSelects all <p> elements inside <div> elementsStyle nested elements.
element>elementdiv > pSelects all <p> elements where the parent is a <div> elementStyle direct children of a parent element.
element+elementdiv + pSelects the first <p> element that is placed immediately after <div> elementsStyle adjacent sibling elements.
:hovera:hoverSelects links on mouse overApply styles when a user hovers over an element.
:focusinput:focusSelects the input element which has focusHighlight form fields when they are active.
[attribute][target]Selects all elements with a target attributeTarget elements with specific attributes for styling.
:checkedinput:checkedSelects every checked <input> elementStyle selected checkboxes or radio buttons.
:defaultinput:defaultSelects the default <input> elementStyle or highlight the default button or form element.
:disabledinput:disabledSelects every disabled <input> elementApply styles to indicate fields that are non-interactive.
:emptyp:emptySelects every <p> element that has no children (including text nodes)Hide or style placeholders for empty elements.
:enabledinput:enabledSelects every enabled <input> elementApply styles to interactive form fields.
:first-childp:first-childSelects every <p> element that is the first child of its parentHighlight the first child of a container.
:first-of-typep:first-of-typeSelects every <p> element that is the first <p> element of its parentApply styles to the first instance of a specific type.
:fullscreen:fullscreenSelects the element that is in full-screen modeAdjust styles for elements in full-screen view.
:has()h2:has(+p)Selects <h2> elements that are immediately followed by a <p> element, and applies the style to <h2>Apply styles based on the presence of specific child elements.
:in-rangeinput:in-rangeSelects input elements with a value within a specified rangeStyle valid numeric inputs within acceptable ranges.
:indeterminateinput:indeterminateSelects input elements that are in an indeterminate stateStyle checkboxes or progress bars with mixed states.
:invalidinput:invalidSelects all input elements with an invalid valueHighlight form fields with validation errors.
:is():is(ul, ol)Selects all <ul> and <ol> elementsCombine selectors to simplify complex queries.
:lang()p:lang(it)Selects every <p> element with a lang attribute equal to “it” (Italian)Style elements based on language settings.
:last-childp:last-childSelects every <p> element that is the last child of its parentHighlight the last child of a container.
:last-of-typep:last-of-typeSelects every <p> element that is the last <p> element of its parentStyle the last instance of a specific type.
:linka:linkSelects all unvisited linksStyle hyperlinks that haven’t been visited yet.
:not():not(p)Selects every element that is not a <p> elementExclude specific elements from styling.
:nth-child()p:nth-child(2)Selects every <p> element that is the second child of its parentApply styles to elements in specific positions within their parent.
:only-of-typep:only-of-typeSelects every <p> element that is the only <p> element of its parentStyle unique elements within their container.
:optionalinput:optionalSelects input elements with no “required” attributeStyle optional form fields differently from required ones.
:read-onlyinput:read-onlySelects input elements with the “readonly” attribute specifiedApply styles to fields that users cannot modify.
:read-writeinput:read-writeSelects input elements with the “readonly” attribute NOT specifiedStyle editable fields.
:requiredinput:requiredSelects input elements with the “required” attribute specifiedHighlight mandatory form fields.
:root:rootSelects the document’s root elementDefine global variables or styles for the document.
:validinput:validSelects all input elements with a valid valueStyle form fields that pass validation.
:visiteda:visitedSelects all visited linksApply different styles to hyperlinks that the user has already clicked.