October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk5 min

CSS Selectors Cheat Sheet for Web Developers

Learn how CSS selectors target elements by type, class, attribute, state, and document-tree relationship, with clear examples and support notes.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

A CSS selector is a pattern that matches elements in a document tree. Use a type, class, ID, or attribute selector to identify an element; add combinators to describe its relationship to other elements; and use pseudo-classes or pseudo-elements for states, conditions, or parts. This guide groups the syntax by what it targets so you can choose a selector without treating the reference as a flat list.

Basic selectors: identify elements directly

Selector family Pattern What it matches Example
Universal * Any element. *
Type element Elements with the named tag. button matches button elements.
Class .class Elements carrying that class token. .notice
ID #id The element with that ID value. #main
Attribute [attribute] or [attribute="value"] Elements with the attribute, optionally constrained by its value. [href] or [type="email"]

Use the narrowest selector that expresses the target you mean. The universal selector is useful when a rule genuinely applies to every element in scope, but a type, class, ID, or attribute selector communicates a more specific target.

Attribute operators

Attribute selectors can test more than exact equality. For example, CSS supports operators for matching a value that begins with, ends with, or contains a string, as well as token and language-prefix matching. Consult the MDN CSS selectors reference for operator syntax and examples; choose the exact operator based on whether the attribute value is a whole token or a substring.

Compound selectors and selector lists

Compound selectors: several conditions on one element

Write simple selectors next to each other with no combinator to require that the same element match all of them. button.primary[disabled] matches a button that has the primary class and a disabled attribute.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
#1 Best Overall
Sale
HTML and CSS: Design and Build Websites
  • HTML CSS Design and Build Web Sites
  • Comes with secure packaging
  • It can be a gift option

Selector lists: one rule for multiple patterns

Separate selectors with commas to match elements covered by any of the listed patterns. h1, h2 matches either heading type, so one declaration block can style both. A comma means “or”; it does not require one element to satisfy both selectors.

Combinators: describe relationships in the document tree

Combinator Meaning Example
Space Descendant: the right-hand match appears anywhere inside the left-hand element. article p matches paragraphs nested anywhere in an article.
> Child: the right-hand match is a direct child of the left-hand element. ul > li matches list items whose parent is a ul.
+ Next sibling: the right-hand match immediately follows the left-hand element as a sibling. h2 + p matches a paragraph immediately following an h2.
~ Subsequent sibling: the right-hand match follows the left-hand element among its siblings, not necessarily immediately. h2 ~ p matches paragraphs that follow an h2 as siblings.
|| Column combinator, intended to express a relationship between elements in the same table column. Check compatibility before using it; MDN’s guide currently reports no browser support for this combinator.

Combinators constrain the relationship between selector parts. A space can match through multiple levels of nesting; use > when only a direct parent-child relationship should count. The MDN guide to selectors and combinators describes the syntax and support caveat for the column combinator.

Rank #2
Sale
Web Design with HTML, CSS, JavaScript and jQuery Set
  • Brand: Wiley
  • Set of 2 Volumes
  • A handy two-book set that uniquely combines related technologies Highly visual format and accessible language makes these books highly effective learning tools Perfect for beginning web designers and front-end developers

Pseudo-classes and pseudo-elements

Pseudo-classes: match a state, condition, or position

A pseudo-class adds a condition to an element selector. Common examples include a:hover for a hovered link, :focus-visible for an element matching that focus condition, input:checked for a checked input, and li:nth-child(2) for a list item in the second child position. The selector still targets an element; the pseudo-class narrows which elements qualify.

Pseudo-elements: target an abstract part

A pseudo-element addresses an abstract part associated with an element rather than another element in the document tree. Examples include p::first-line and p::before. The double-colon form distinguishes pseudo-elements from pseudo-classes in modern syntax.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

The full lists are extensive and feature-specific. MDN describes the CSS selector module as providing “more than 60 selectors and five combinators.” For an exhaustive, current index, use the MDN selector reference.

How to choose a selector

  1. Start with what identifies the target. Use a type for all elements of a tag, a class for a reusable category, an ID for a specific ID value, or an attribute selector for an attribute-based condition.
  2. Add conditions on the same element if needed. Combine simple selectors without spaces, as in button.primary[disabled].
  3. Add a combinator only when a relationship matters. Choose descendant, child, next sibling, or subsequent sibling syntax according to the structure you need.
  4. Use a pseudo-class for state or structure, and a pseudo-element for a part. Check the selector-specific reference if the syntax is unfamiliar.
  5. Verify browser support for less established selectors. A selector appearing in a specification does not mean it works in every browser.

Selectors in CSS and JavaScript

In a CSS rule, the selector identifies which elements receive the declarations. Selector patterns are also used by DOM matching and query APIs in JavaScript, so the same general syntax can help locate elements in code. The MDN guide covers both selector concepts and their use with the DOM.

Specificity is not matching

Selector matching answers which elements qualify. Specificity is a separate part of the cascade that helps resolve competing declarations when rules apply to the same element. A selector can match an element regardless of whether its declaration ultimately wins. For the full cascade and specificity model, consult the MDN specificity guide.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Browser support and standards status

The W3C Selectors Level 4 document is a Working Draft dated 22 January 2026, not a final Recommendation. It describes selector syntax and concepts, but support varies by feature and browser. MDN’s selector guide reports that the column combinator (||) currently has no browser support. For any less familiar selector, check current compatibility data for the browsers your project supports rather than inferring support from its presence in the specification.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Best Value
Sale
Charlotte's Web: A Newbery Honor Award Winner – The Beloved Classic Novel About a Pig, a Spider, and the Power of Friendship
  • These are the words in Charlotte's web, high in the barn
  • Her spiderweb tells of her feelings for a little pig named Wilbur, as well as the feelings of a little girl named Fern … who loves Wilbur, too
  • Their love has been shared by millions of readers

Or skip the browser setup

If you are creating a screenshot to inspect a page’s layout or selector-targeted element, you can capture it with one API request instead of setting up a browser. ScreenshotNeo is a website screenshot API and MCP server; its capture options include selecting one element by CSS selector.

ScreenshotNeo API documentation

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp

Cookie banners are accepted before capture and removed along with known consent platforms, newsletter popups, and chat widgets. Bot checks, blank pages, failed loads, timeouts, and cache hits are not billed. An MCP server provides screenshot tools for AI agents, and the free plan includes 1,000 screenshots a month with no card; paid plans start at $5 for 3,000 screenshots. Sign up for free.

Further reading

For a portable general CSS reference, CSS Pocket Reference, 5th Edition by Eric A. Meyer includes a selectors section and covers CSS3-era material. For current selector details, use the MDN reference and the W3C specification draft linked above.

Quick Recap

SaleBestseller No. 1
HTML and CSS: Design and Build Websites
HTML and CSS: Design and Build Websites
HTML CSS Design and Build Web Sites; Comes with secure packaging; It can be a gift option
$14.18
SaleBestseller No. 2
Web Design with HTML, CSS, JavaScript and jQuery Set
Web Design with HTML, CSS, JavaScript and jQuery Set
Brand: Wiley; Set of 2 Volumes
$35.05
SaleBestseller No. 5
Charlotte's Web: A Newbery Honor Award Winner – The Beloved Classic Novel About a Pig, a Spider, and the Power of Friendship
Charlotte's Web: A Newbery Honor Award Winner – The Beloved Classic Novel About a Pig, a Spider, and the Power of Friendship
These are the words in Charlotte's web, high in the barn; Their love has been shared by millions of readers
$6.13

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. Shenzhen desk3 min
    HONOR Expands Beyond Smartphones With Humanoid Robot RevealHONOR said it unveiled its first humanoid robot at MWC 2026 and named shopping assistance, workplace inspections, and supportive companionship as intended uses. Later Robotics D1 claims and a reported…
  2. Cupertino desk5 min
    Apple Unveils AirPods Max 2: The Upgrade That Should Have Happened Years AgoAirPods Max 2 adds H2-powered audio features and Apple claims up to 1.5× more effective ANC, but its design, Smart Case, and 20-hour battery rating are unchanged. Wired lossless audio…
  3. Cupertino desk4 min
    Apple’s OLED Touch MacBooks Are Coming—but the Dynamic Island Is the Real GambleApple has not announced an OLED touchscreen MacBook, but reports point to high-end models arriving in late 2026 or early 2027. The reported Mac Dynamic Island could be useful, but…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.