CSS selectors are patterns that choose which HTML elements receive styles. This hub covers 59 selector tutorials — from basic p and .class to combinators, pseudo-classes, pseudo-elements, and attribute matching.
01
Basic
Tag, class, ID.
02
Combinators
Child, sibling.
03
:pseudo
States.
04
::parts
Fragments.
05
[attr]
Attributes.
06
59 guides
Full index.
Fundamentals
Introduction
CSS selectors are patterns used to select elements on a web page and apply styles. They define the look and layout of a website by targeting specific HTML elements — without changing the HTML structure itself.
What Are CSS Selectors?
A selector tells the browser which elements get the declarations in a rule. You can target by element type (p), class (.card), ID (#header), attributes ([disabled]), structure (div > p), or state (a:hover).
💡
Beginner Tip
Start with .class for reusable styles and element for base typography. Add :hover and :focus when you style buttons and links.
Selectors vs Properties
Selector — who to style — .btn
Property — what to change — background-color
Value — how to change it — #2563eb
Rule — .btn { background-color: #2563eb; }
Foundation
📝 Syntax
Every CSS rule pairs a selector with a declaration block:
CSS
/* Element */p{color:#334155;}/* Class */.highlight{background:#fef08a;}/* ID */#footer{text-align:center;}/* Pseudo-class */a:hover{text-decoration:underline;}
::before creates a virtual element as the first child. The content property is required for it to appear.
Tips
💬 Usage Tips
Prefer classes — Reusable, low specificity, easy to maintain.
Keep selectors short — .card-title beats div div h3 span.
LVHA for links — Style in order: :link, :visited, :hover, :active.
DevTools inspect — See which selectors match and which rules win.
Search this index — Jump to any of 59 tutorials instantly.
Watch Out
⚠️ Common Pitfalls
Overusing IDs — High specificity makes overrides painful.
Deep descendant chains — div div div p is fragile and slow.
Universal selector resets — * affects every element; scope carefully.
:hover-only interactions — Touch devices need :focus and tap targets too.
Missing ::before content — Pseudo-elements need a content value to render.
A11y
♿ Accessibility
:focus styles — Never remove focus outlines without a visible replacement.
:focus-visible — Consider keyboard-only focus rings where supported.
Color-only :invalid — Pair validation color with text or icons.
:visited privacy — Only safe properties can be styled on visited links.
Meaningful labels — Selectors style elements; accessible names come from HTML.
🧠 How CSS Selectors Work
1
Browser parses HTML
The DOM tree is built from your HTML elements, classes, IDs, and attributes.
DOM
2
Selectors match nodes
Each rule’s selector finds matching elements in the tree.
Match
3
Cascade resolves conflicts
Specificity and source order pick the winning declaration for each property.
Cascade
=
🎨
Styled elements
Matched elements receive colors, spacing, layout, and effects.
Compatibility
🖥 Browser Compatibility
Core selectors — element, class, ID, combinators, and common pseudo-classes — have universal browser support. Newer selectors like :has() have dedicated notes on their tutorial pages.
✓ Baseline · Universal support
Core CSS selectors
Element, class, ID, descendant, child, and :hover work in every major browser. Check individual tutorials for newer selectors.
100%Core selectors
Google ChromeAll versions
Full support
Mozilla FirefoxAll versions
Full support
Apple SafariAll versions
Full support
Microsoft EdgeAll versions
Full support
OperaAll modern versions
Full support
Core CSS selectors100% supported
Bottom line:p, .class, #id, div p, and :hover are production-safe everywhere. For :has() and other newer selectors, read the compatibility section on each tutorial page.
Wrap Up
🎉 Conclusion
CSS selectors are fundamental to web development — they let you precisely target and style HTML elements. Master basic selectors first, then explore combinators, pseudo-classes, and attributes as your layouts grow.
Use this hub to compare selector types, then open any of the 59 tutorials for try-it examples and detailed FAQs. Pair selectors with properties and media queries for complete, maintainable stylesheets.
CSS selectors are patterns that tell the browser which HTML elements to style. They appear before the declaration block: selector { property: value; }. Examples include p, .card, #header, and a:hover.
A class selector (.name) can match many elements and is reusable. An ID selector (#name) should match only one element per page. Prefer classes for most styling; reserve IDs for unique landmarks or JavaScript hooks.
Specificity decides which rule wins when multiple selectors target the same element. IDs beat classes, classes beat elements, and inline styles beat stylesheet rules. Lower specificity is easier to maintain.
Pseudo-classes (:hover, :focus) describe element state or position in the tree. Pseudo-elements (::before, ::first-letter) style a specific part of an element. Pseudo-classes use one colon; pseudo-elements use two.
Combinators connect selectors to express relationships: descendant (space), child (>), adjacent sibling (+), and general sibling (~). They let you style elements based on where they sit in the HTML structure.
Read the overview, try the five examples, then open .class or element from Basic Selectors. Use the search box to jump to any of the 59 selector tutorials.