Test CSS selectors against HTML and see matching elements.
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.
| 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 |
| 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> |
| 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"] |
: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: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/* 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
When multiple selectors target the same element, specificity determines which styles apply. Specificity is calculated as:
Examples:
#header = (0,1,0,0).button = (0,0,1,0)div.button = (0,0,1,1)#nav .menu-item = (0,1,1,0)> is more specific and performant than spaceThe primary use of selectors - applying styles to elements matching specific patterns.
// Select single element
const element = document.querySelector('.button');
// Select all matching elements
const elements = document.querySelectorAll('div > p');
Extract data from HTML pages using selectors with tools like BeautifulSoup or Cheerio.
Locate elements in test frameworks like Selenium, Playwright, or Cypress.
.btn.primary requires both classes, .btn .primary is descendantdiv.container is usually unnecessary, .container sufficesBasic:
.class - Class#id - IDelement - Tag[attr] - AttributeCombinators:
A B - DescendantA > B - ChildA + B - AdjacentA ~ B - Siblingnav a
All links in nav
div.active
Divs with class active
#main > p
P children of #main
input[type="text"]
Text inputs