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

Use a CSS selector to match elements by tag, class, ID, attribute, state, or position in the document tree. Combine selectors when one condition is not enough: a space means “inside,” > means “direct child,” and + and ~ select following siblings. This cheat sheet groups the syntax by what it matches so you can choose a target without guessing.

Basic CSS selectors

A selector is a pattern that matches elements in a document tree. In a CSS rule, it identifies which elements receive the declarations. The same selector patterns are also used by JavaScript DOM matching and query APIs.

Selector type Pattern What it matches Example
Universal * Any element. *
Type Tag name Elements with that tag name. button matches button elements.
Class .name Elements with that class token. Classes can be shared by many elements. .notice
ID #name An element with the matching ID. #main
Attribute [attribute] or [attribute="value"] Elements with an attribute, or with a matching value. [type="email"]
Selector list Selectors separated by commas Elements matching any selector in the list. h1, h2

A selector list shares the rule’s declarations across its matches. A compound selector, by contrast, adds conditions that must all match the same element: button.primary[disabled] means a button with the primary class and a disabled attribute.

Attribute selectors

Attribute selectors let you target elements based on whether an attribute exists or what its value contains. These operators are useful for matching groups of links, form controls, and other elements without adding a class.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
#1 Best Overall
Elebase USB to USB C Adapter for iPhone 18 Pro Max,USBC Car Charger Adapter
  • Read Before You Buy — No Video Output: These adapters support charging and USB 2.0 data transfer, but cannot transmit video signals. Except for standard USB webcams (which use USB data only), they are not compatible with HDMI/DisplayPort cables, video-capable USB-C hubs, or docking stations with video output.
  • Convert USB-A Ports to USB-C: Designed to connect USB-C earphones, cables, flash drives, card readers, and other USB-C accessories to standard USB-A ports. Plug-and-play with no drivers or software required.
  • Aluminum Alloy Housing: Built with a sturdy aluminum alloy shell that aids in heat dissipation and protects against daily wear and scratches. Designed to maintain a stable and secure connection.
  • Compact & Travel-Friendly: The ultra-compact design allows the adapter to stay plugged into your device without blocking adjacent ports or adding bulk, reducing wear and tear on your original USB ports.
  • 12-Month Warranty: Backed by a 12-month manufacturer warranty for peace of mind. Designed to meet strict quality control standards for reliable everyday performance.
Pattern Meaning Example
[href] Has the attribute, regardless of its value. a[href] matches links with an href.
[type="email"] Attribute value equals the quoted value. input[type="email"]
[class~="notice"] Attribute contains the value as a whitespace-separated token. Useful when matching one class token in a class attribute.
[href^="https"] Attribute value begins with the given text. Links whose href starts with https.
[href$=".pdf"] Attribute value ends with the given text. Links whose href ends with .pdf.
[href*="docs"] Attribute value contains the given substring. Links whose href contains docs.

Attribute matching is literal pattern matching, not a check that a link works or that a file is actually a PDF. Use the narrowest attribute condition that expresses the target you need.

Combinators: match by relationship

A combinator connects selectors and describes how the matching elements relate in the document tree. The selector after the combinator is the target; the selector before it constrains that target by its relationship.

Rank #2
Anker USB-C Hub, 5-in-1 USB Hub for Laptops, 4K HDMI Multiport Adapter
  • 5-in-1 USB-C Hub: Experience comprehensive connectivity featuring a Power Delivery input, two USB-A 2.0 ports, a USB-A 3.0 port, and an HDMI port. (Note: The USB-C power delivery input port is only for connecting an external wall charger to power your laptop and cannot power peripheral devices.)
  • 90W Pass-Through Charging: Achieve optimal charging with 90W pass-through power to your laptop, supported by a total input of 100W, with the hub reserving 10W for operational efficiency. (Note: Wall charger not included.)
  • Quick Data Transfers: Accelerate your productivity with rapid data transfers using a high-speed 5Gbps USB 3.0 port and two 480Mbps USB 2.0 ports.
  • 4K HDMI Display: Enhance your visual experience with a hub capable of delivering 4K resolution at 30Hz in both mirror and extend modes. Please note that this hub is compatible with MacBook (macOS 12 and newer), Windows 10 and 11, ChromeOS, and laptops equipped with DP Alt Mode and Power Delivery. Note: This device is not compatible with Linux.
  • What You Get: Anker USB-C Hub (5-in-1, 4K HDMI), welcome guide, 18-month warranty, and our friendly customer service.
Combinator Relationship Example What matches
Space Descendant at any depth article p Paragraphs anywhere inside an article.
> Direct child ul > li List items whose parent is a ul.
+ Immediately following sibling h2 + p A paragraph that immediately follows an h2 as a sibling.
~ Following sibling, not necessarily adjacent h2 ~ p Paragraph siblings that follow an h2.
|| Column relationship col || td A column combinator syntax defined by the specification.

Do not confuse descendant and child matching: article p also reaches paragraphs nested in other elements inside the article, while article > p only matches paragraphs that are direct children.

Pseudo-classes: match a condition or state

A pseudo-class begins with a colon and selects an element when a condition is true, such as a user interaction state, a form state, or a structural position. It is still the element itself that is selected.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Rank #3
Sale
Anker USB C Hub, 7in1 Multi-Port USB Adapter, 4K@60Hz USBC to HDMI Splitter
  • Sleek 7-in-1 USB-C Hub: Features an HDMI port, two USB-A 3.0 ports, and a USB-C data port, each providing 5Gbps transfer speeds. It also includes a USB-C PD input port for charging up to 100W and dual SD and TF card slots, all in a compact design.
  • Flawless 4K@60Hz Video with HDMI: Delivers exceptional clarity and smoothness with its 4K@60Hz HDMI port, making it ideal for high-definition presentations and entertainment. (Note: Only the HDMI port supports video projection; the USB-C port is for data transfer only.)
  • Double Up on Efficiency: The two USB-A 3.0 ports and a USB-C port support a fast 5Gbps data rate, significantly boosting your transfer speeds and improving productivity.
  • Fast and Reliable 85W Charging: Offers high-capacity, speedy charging for laptops up to 85W, so you spend less time tethered to an outlet and more time being productive.
  • What You Get: Anker USB-C Hub (7-in-1), welcome guide, 18-month warranty, and our friendly customer service.
Kind Examples Typical use
Interaction state a:hover, :focus-visible Style an interactive element while hovered or when it has a visible keyboard focus indicator.
Form state input:checked Match a checked form control.
Structural position li:nth-child(2) Match a list item in a particular sibling position.

For example, li:nth-child(2) matches an li that is the second child among its siblings; it does not mean “the second list item of this type” if other element types are mixed among those siblings. Check the current selector reference for the full set of pseudo-classes and the exact behavior of each function.

Pseudo-elements: target an abstract part

Pseudo-elements begin with a double colon and target an abstract part associated with an element, rather than selecting another element in the document tree.

Rank #4
Sale
UGREEN USB to USB C Adapter Combo 4-Pack, 10Gbps USB C Converter Space Gray
  • Dual Converters, Infinite Potential:Includes 2× USB C male to USB A female adapters and 2× USB A male to USB C female adapters. Perfect for a wide range of uses—tablets with Bluetooth keyboards, expand USB ports on macbook, and more. Two different converters for all your daily needs
  • Next-Level 10Gbps & 3A Charging: No more slow 480Mbps, this usb to usb c adapter has a transfer speed of up to 10Gbps, allowing you to do more transferring in less time. This usb adapter fits both USB A and USB C charger, supporting up to 3A fast charging
  • Upgraded Exquisite Craftsmanship: With an aluminum alloy housing and metal connector, the usbc to usb adapter is extremely durable and sturdy. Rigorously tested to withstand more than 10,000 times of plugging and unplugging, ensuring long-lasting performance
  • Broad Compatible: The usb c to usb adapter widely supports all USB C/ USB A devices like laptops, tablets, cellphones, car chargers, and phone chargers. Such as compatible with MacBook Pro/Air 2023/2022, Thunderbolt 4/3 Devices,Apple MagSafe Watch 9/8/7/SE/Ultra, iPad Pro 2022/2021, Samsung Galaxy S23/S20/S10, and iPhone 17/16/15 Pro. Plug and play
  • Please Note: To reach 10Gbps speed, keep the cable under 3.3 ft. For USB A Male to USB C adapters, try flipping the USB C connector. USB C Male to USB A adapters support bidirectional 10Gbps transfer within 3.3 ft
Pattern Target
p::first-line The first line of text in a paragraph.
p::before A generated before-content part associated with a paragraph.

Keep the distinction clear: :hover is a condition on an element; ::before addresses a part associated with an element. Consult a current reference before relying on less common pseudo-elements or pseudo-classes.

How to combine selectors without changing the target

  • Put simple selectors next to each other to require all conditions on one element: button.primary[disabled].
  • Use a combinator when the target depends on another element’s position in the tree: nav a or ul > li.
  • Separate alternatives with commas when the same rule should apply to different targets: h1, h2.
  • Use pseudo-classes for an element’s state or position, and pseudo-elements for an abstract part: a:focus-visible versus a::before.

These forms can be combined. For instance, article.notice > p a[href] selects links with an href inside paragraphs that are direct children of an article carrying the notice class.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Best Value
Anker USB C Hub, 5-in-1 USBC to HDMI Splitter with 4K Display
  • 5-in-1 Connectivity: Equipped with a 4K HDMI port, a 5 Gbps USB-C data port, two 5 Gbps USB-A ports, and a USB C 100W PD-IN port. Note: The USB C 100W PD-IN port supports only charging and does not support data transfer devices such as headphones or speakers.
  • Powerful Pass-Through Charging: Supports up to 85W pass-through charging so you can power up your laptop while you use the hub. Note: Pass-through charging requires a charger (not included). Note: To achieve full power for iPad, we recommend using a 45W wall charger.
  • Transfer Files in Seconds: Move files to and from your laptop at speeds of up to 5 Gbps via the USB-C and USB-A data ports. Note: The USB C 5Gbps Data port does not support video output.
  • HD Display: Connect to the HDMI port to stream or mirror content to an external monitor in resolutions of up to 4K@30Hz. Note: The USB-C ports do not support video output.
  • What You Get: Anker 332 USB-C Hub (5-in-1), welcome guide, our worry-free 18-month warranty, and friendly customer service.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Specificity and browser support

Specificity helps the cascade decide which declaration wins when multiple rules apply. It does not change which elements a selector matches. Treat selector choice and cascade debugging as separate questions: first confirm that the intended elements match, then inspect competing rules and the cascade.

Selectors Level 4 is a W3C Working Draft dated 22 January 2026, not a final Recommendation. Selector support is feature-specific; MDN’s CSS selectors guide states that the column combinator (||) currently has no browser support in its guide. Check compatibility for the particular selector and browsers you need before using less-established syntax.

Test a selector against the rendered page

  1. Identify the element and the condition that distinguishes it: tag, class, ID, attribute, state, or relationship.
  2. Write the narrowest selector that expresses that condition, then check whether it also matches unintended elements.
  3. For CSS, put it before a temporary declaration in a stylesheet or the browser’s developer tools to see the matched elements and styling effect.
  4. For a DOM query, use the same selector pattern with the relevant query API and check the returned element or elements.
  5. If a match differs from what you expect, inspect the actual attributes and parent/sibling structure in the rendered DOM, not just the source template.

Common selector mistakes

  • No match: Check punctuation and spelling: a class starts with ., an ID with #, and an attribute selector uses square brackets.
  • Too many matches: A space reaches descendants at any depth. Replace it with > if only direct children should match, or add a meaningful class or attribute constraint.
  • Sibling selector misses: + only matches the immediately next sibling; use ~ for later siblings.
  • Grouped rule behaves unexpectedly: Separate selectors in a list with commas. Without a comma, adjacent simple selectors are conditions on the same element, and a space means a descendant relationship.
  • Selector works in one browser but not another: Verify support for the exact feature in your target browsers rather than assuming that every syntax in the specification is implemented.

Or skip the browser setup

If the goal is to capture a rendered page or a specific element for documentation or review, ScreenshotNeo is a website screenshot API with CSS-selector element capture. Its API can return PNG, JPEG, WebP, or PDF; the example below makes one GET request for a WebP screenshot. See the ScreenshotNeo API documentation for request options.

cURL

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

Python

import requests
r = requests.get("https://api.screenshotneo.com/v1/shot", params={"access_key": "YOUR_API_KEY", "url": "https://stripe.com"}, timeout=90)
open("shot.webp", "wb").write(r.content)

Node.js

const q = new URLSearchParams({ access_key: 'YOUR_API_KEY', url: 'https://stripe.com' });
const res = await fetch(`https://api.screenshotneo.com/v1/shot?${q}`);

ScreenshotNeo accepts consent banners like a visitor and removes more than 60 known consent platforms, newsletter popups, and chat widgets before capture; those steps can be turned off. Bot checks or CAPTCHAs, blank pages, failed loads, timeouts, and cache hits are not billed, and response headers report the page verdict and billing status. Its MCP server provides take_screenshot, get_page_info, and capture_pdf tools for AI agents. The Free plan includes 1,000 shots per month with no card; paid plans start at $5 for 3,000 shots.

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

Sign up for ScreenshotNeo’s free plan: 1,000 screenshots a month, no card required.

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.