CSS Selector Tester
Test CSS selectors against HTML and see matching elements.
CSS Selector Guide
CSS selectors are patterns used to select and style HTML elements. Understanding selectors is essential for web development, web scraping, automated testing, and DOM manipulation. Whether you're writing styles, using querySelector in JavaScript, or extracting data, mastering CSS selectors is a fundamental skill. This tool helps you test and validate your selectors against real HTML markup.
Basic Selectors
| Selector | Description | Example | Matches |
|---|---|---|---|
* |
Universal selector | * |
All elements |
element |
Type selector | div |
All <div> elements |
.class |
Class selector | .button |
Elements with class="button" |
#id |
ID selector | #header |
Element with id="header" |
[attr] |
Attribute selector | [disabled] |
Elements with disabled attribute |
Combinators
| Combinator | Name | Example | Selects |
|---|---|---|---|
A B |
Descendant | div p |
All <p> inside <div> (at any level) |
A > B |
Child | div > p |
<p> that are direct children of <div> |
A + B |
Adjacent sibling | h1 + p |
<p> immediately after <h1> |
A ~ B |
General sibling | h1 ~ p |
All <p> that follow <h1> |
Attribute Selectors
| Selector | Description | Example |
|---|---|---|
[attr=value] |
Exact match | [type="text"] |
[attr^=value] |
Starts with | [href^="https"] |
[attr$=value] |
Ends with | [href$=".pdf"] |
[attr*=value] |
Contains | [class*="btn"] |
[attr~=value] |
Contains word | [class~="active"] |
[attr|=value] |
Starts with value or value- | [lang|="en"] |
Pseudo-Classes
Structural Pseudo-Classes
:first-child- First child of its parent:last-child- Last child of its parent:nth-child(n)- Nth child of its parent:nth-of-type(n)- Nth element of its type:only-child- Only child of its parent:empty- Elements with no children
State Pseudo-Classes
:hover- Mouse pointer over element:focus- Element has focus:active- Element being activated:disabled- Disabled form elements:checked- Checked checkboxes/radios:not(selector)- Elements not matching selector
Practical Examples
Common Patterns
/* All links in navigation */
nav a
/* Direct child paragraphs of articles */
article > p
/* First paragraph after any heading */
h1 + p, h2 + p, h3 + p
/* All elements with data-active attribute */
[data-active]
/* Links to external sites (starting with http) */
a[href^="http"]
/* PDF download links */
a[href$=".pdf"]
/* Every other table row (zebra striping) */
tr:nth-child(even)
/* All inputs except submit buttons */
input:not([type="submit"])
/* Paragraphs that are first children */
p:first-child
Specificity
When multiple selectors target the same element, specificity determines which styles apply. Specificity is calculated as:
- Inline styles: Highest specificity (1,0,0,0)
- IDs: (0,1,0,0) per ID
- Classes, attributes, pseudo-classes: (0,0,1,0) each
- Elements, pseudo-elements: (0,0,0,1) each
Examples:
#header= (0,1,0,0).button= (0,0,1,0)div.button= (0,0,1,1)#nav .menu-item= (0,1,1,0)
Best Practices
- Keep selectors simple: Overly specific selectors are hard to override and maintain
- Avoid deep nesting: Long selector chains are fragile and slow
- Use classes over IDs for styling: Classes are reusable and have lower specificity
- Prefer direct children over descendants:
>is more specific and performant than space - Be semantic: Use meaningful class names that describe purpose, not appearance
- Avoid !important: It makes debugging and overriding styles difficult
Use Cases
1. Styling with CSS
The primary use of selectors - applying styles to elements matching specific patterns.
2. JavaScript Selection
// Select single element
const element = document.querySelector('.button');
// Select all matching elements
const elements = document.querySelectorAll('div > p');
3. Web Scraping
Extract data from HTML pages using selectors with tools like BeautifulSoup or Cheerio.
4. Automated Testing
Locate elements in test frameworks like Selenium, Playwright, or Cypress.
Common Mistakes
- Multiple classes without space:
.btn.primaryrequires both classes,.btn .primaryis descendant - Forgetting specificity: More specific selectors override less specific ones
- Using IDs for repetitive styles: IDs should be unique; use classes instead
- Over-qualifying selectors:
div.containeris usually unnecessary,.containersuffices
Quick Reference
Basic:
.class- Class#id- IDelement- Tag[attr]- Attribute
Combinators:
A B- DescendantA > B- ChildA + B- AdjacentA ~ B- Sibling
Common Examples
nav a
All links in nav
div.active
Divs with class active
#main > p
P children of #main
input[type="text"]
Text inputs