CSS Selectors Reference

A comprehensive cheat sheet for CSS selectors, combinators, and pseudo-classes.

No matching selectors found. Try a different search term.

Basic Selectors

Universal Selector
*
Selects all elements in the document.
Matches: <div>, <p>, <span>...
Type Selector
element
Selects elements by their HTML tag name.
Example: p matches all <p> elements.
Class Selector
.class
Selects elements with a specific class attribute.
Example: .btn matches <button class="btn">
ID Selector
#id
Selects a single element with a specific ID attribute.
Example: #header matches <div id="header">

Combinators

Descendant
A B
Selects elements that are descendants (children, grandchildren) of the first element.
Example: div p matches <p> inside a <div>
Direct Child
A > B
Selects elements that are direct children of the first element.
Example: ul > li matches <li> directly inside <ul>
General Sibling
A ~ B
Selects all elements B that follow element A and share the same parent.
Example: h1 ~ p matches all <p> after an <h1>
Adjacent Sibling
A + B
Selects an element B that immediately follows element A.
Example: h2 + p matches the first <p> right after <h2>

Attribute Selectors

Has Attribute
[attr]
Selects elements with the specified attribute, regardless of its value.
Example: [disabled] matches <input disabled>
Exact Value
[attr="value"]
Selects elements where the attribute exactly matches the given value.
Example: [type="submit"] matches <button type="submit">
Starts With
[attr^="val"]
Selects elements whose attribute value begins with the specified string.
Example: [href^="https://"] matches secure links
Ends With
[attr$="val"]
Selects elements whose attribute value ends with the specified string.
Example: [src$=".png"] matches PNG images
Contains
[attr*="val"]
Selects elements whose attribute value contains the specified substring anywhere.
Example: [class*="grid-"] matches "grid-row" or "my-grid-col"

Pseudo-classes

:hover
:hover
Matches when a user hovers over an element with a pointer device.
Example: a:hover styles links on hover
:focus
:focus
Matches when an element receives focus (e.g., input field clicked).
Example: input:focus styles active inputs
:nth-child()
:nth-child(an+b)
Matches elements based on their position among a group of siblings.
Example: li:nth-child(odd) matches 1st, 3rd, 5th items
:first-child
:first-child
Matches an element that is the first child of its parent.
Example: p:first-child targets the first paragraph
:not()
:not(selector)
Matches elements that do not match a list of selectors.
Example: div:not(.active) matches divs without active class
:has()
:has(selector)
The relational pseudo-class. Matches elements if the condition passed as argument is true. Often called the "parent selector".
Example: a:has(> img) matches anchor tags containing images

Pseudo-elements

::before
::before
Creates a pseudo-element that is the first child of the selected element. Requires the content property.
Example: h1::before { content: "★ " }
::after
::after
Creates a pseudo-element that is the last child of the selected element. Requires the content property.
Example: a::after { content: " ↗" }
::selection
::selection
Styles the portion of a document that has been highlighted by the user (such as clicking and dragging).
Example: ::selection { background: #818cf8; }

Understanding CSS Selectors

CSS selectors are the patterns used to select and target HTML elements for styling. They form the foundation of CSS (Cascading Style Sheets). Whether you are looking to style a single unique element on a page or thousands of elements at once, knowing the right selector is essential.

Why Combinators Matter

Combinators allow you to define relationships between selectors. Instead of adding a class to every single element, you can use a descendant (space) or child (>) combinator to target elements based on their position in the DOM. This keeps your HTML clean and your CSS efficient.

Pseudo-classes vs. Pseudo-elements

It's easy to confuse the two:

  • Pseudo-classes (starting with a single colon :) select an element based on its state. For example, :hover applies styles when the mouse pointer is over the element, and :first-child targets an element only if it's the very first element inside its parent.
  • Pseudo-elements (starting with a double colon ::) target specific parts of an element. For instance, ::before allows you to insert and style content before the element's actual DOM content.

Optimizing Selector Performance

While modern browsers are incredibly fast, overly complex selectors can still cause rendering bottlenecks on large applications. The browser evaluates selectors from right to left (from the key selector to the ancestors). Therefore, prioritizing classes (like .card) over deeply nested descendant selectors (like div ul li a span) can yield better performance.