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.
Recommended Free Tools
#1 Best Overall
- 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
- 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.
The Tool Desk
Outbyte Driver Updater FREEScan for outdated or missing drivers - takes under a minuteDriver Scan →Outbyte PC Repair FREERepair Windows errors before they cause bigger problemsFix Now →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
- 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.
- Add conditions on the same element if needed. Combine simple selectors without spaces, as in
button.primary[disabled]. - Add a combinator only when a relationship matters. Choose descendant, child, next sibling, or subsequent sibling syntax according to the structure you need.
- 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.
- 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.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.
Best Value
- 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
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.




