What is a CSS selector?
A pattern that identifies elements in an HTML document by tag, class, id, attribute, or position, used by stylesheets and (heavily) by web scrapers.
CSS selectors started as the targeting mechanism for stylesheets: .price styles every element with class "price". The same syntax doubles as the de facto query language for the DOM, exposed through document.querySelectorAll in browsers and through every major scraping library (BeautifulSoup, Cheerio, Scrapy, Playwright, Puppeteer).
Selectors compose well. article.featured > h2 a[href^="/blog"] reads as: an a tag whose href starts with /blog, inside an h2, that is a direct child of an article with class featured. Combinators (> child, + adjacent sibling, ~ general sibling) and attribute selectors ([type="email"], [data-id*="user"]) cover most extraction patterns.
Where CSS falls short is positional logic and text matching. There is no "element whose text contains X" in CSS, and :nth-child works on element index rather than filtered subsets. Scrapers that need those reach for XPath or post-filter the matches in code.
In the wild
h1.product-titleto grab the headline of a product pagemeta[property="og:image"]to pull the Open Graph image URL out of<head>a.btn-primary[href*="checkout"]to find a checkout CTA reliably across page variants