CSS Selectors
Master the art of targeting HTML elements with precision using CSS selectors. Learn element, class, ID, attribute, and advanced selector techniques.
What are CSS selectors?
A selector is the pattern that chooses which HTML elements a rule styles. The basic ones are the type selector (p), the class selector (.card), the ID selector (#main) and the universal selector (*). You can combine them, select by attributes ([type="email"]), by state (:hover) and by relationship (nav a).
p { } /* every <p> */
.card { } /* class="card" */
#header { } /* id="header" */
nav a { } /* <a> inside <nav> */
ul > li { } /* direct children only */
a:hover { } /* state */
input[type="email"] { } /* attribute */CSS Selector Types
CSS Selectors are patterns used to select and target specific HTML elements for styling. They define which elements in the document tree will be affected by the CSS rules, ranging from simple element names to complex attribute-based patterns.
Element
BasicTarget all elements of a specific HTML tag
Class
CommonTarget elements with specific class attributes
ID
UniqueTarget a single element with unique ID
Attribute
AdvancedTarget elements based on attributes
Pseudo-class
InteractiveTarget elements in specific states
Combinator
RelationshipTarget based on element relationships
CSS Selectors in Action
This comprehensive example demonstrates all major CSS selector types working together. Interact with elements to see how different selectors target specific parts of the HTML.
Code Editor
<ul class="menu">
<li><a href="#" class="active">Home</a></li>
<li><a href="#">Courses</a></li>
<li><a href="#" data-new>Playground</a></li>
</ul>
<p id="note">Change a selector in the CSS tab and watch what updates.</p>Preview
Basic Selectors
- •Element selectors (h1, p, div)
- •Class selectors (.card, .highlight)
- •ID selectors (#header, #footer)
- •Universal selector (*)
Advanced Selectors
- •Attribute selectors [data-type]
- •Pseudo-classes (:hover, :first-child)
- •Descendant selectors (.card p)
- •Child selectors (.container > .card)
Interactive Features
- •Hover effects and transitions
- •Click interactions via JavaScript
- •Structural pseudo-classes
- •Combined selector techniques
CSS Specificity
When multiple CSS rules target the same element, specificitydetermines which rule wins. Think of it as a scoring system where more specific selectors get higher scores.
Universal & Combinators
Score: 0Lowest specificity - these selectors add no weight to the specificity calculation.
Element Selectors
Score: 1 eachEach HTML element in the selector adds 1 point to the specificity score.
Class, Attribute & Pseudo-class
Score: 10 eachClasses, attributes, and pseudo-classes each add 10 points to specificity.
ID Selectors
Score: 100 eachID selectors have high specificity - each ID adds 100 points.
Inline Styles
Score: 1000Inline styles have the highest specificity and will override external CSS.
Important Exception: !important
The !important declaration overrides normal specificity rules and should be used sparingly.
Selector Best Practices
Good Practices:
- Use classes for reusable styles across multiple elements
- Keep selectors as simple as possible for better performance
- Use semantic class names that describe purpose, not appearance
- Prefer classes over IDs for styling (save IDs for JavaScript)
- Use descendant selectors to create component-scoped styles
Avoid These Mistakes:
- Overusing !important - it makes CSS hard to maintain
- Creating overly specific selectors that are hard to override
- Using inline styles for anything beyond quick testing
- Styling based on tag names alone for complex layouts
- Creating deeply nested selector chains (limit to 3-4 levels)
Your Learning Path
Build expertise in CSS selectors systematically
What You've Learned
- ▶Basic selector types (element, class, ID)
- ▶Advanced selectors (attribute, pseudo-class)
- ▶Combinator relationships and targeting
- ▶CSS specificity and cascade rules
- ▶Best practices for maintainable CSS
What's Next
- ▶CSS Colors - Master color values and schemes
- ▶CSS Backgrounds - Add visual depth and style
- ▶CSS Typography - Control fonts and text styling
- ▶CSS Box Model - Understand spacing and layout
- ▶CSS Positioning - Master element placement
Pro Learning Tip
Practice combining different selector types to create precise targeting. Try recreating popular website layouts using only CSS selectors to master your skills.
CSS Selectors — step by step
Element, Class, and ID Selectors
Element selectors (p, h1) target all matching tags. Class selectors (.card) target elements with that class — reusable across many elements. ID selectors (#header) target one unique element and should be reserved for JavaScript hooks, not styling.
🎯 Quick Check
Which combinator selects only the direct children of an element, not deeper descendants?
CSS Selectors: summary
Key points
- Classes are the workhorse of CSS: reusable, low specificity and easy to override.
- Avoid styling with IDs. Their high specificity makes later overrides painful.
:is()and:where()group selectors;:where()adds zero specificity.:has()selects a parent based on its children, for example.card:has(img).
Common mistakes
- Writing very long selectors like
body div.main ul li a. They're slow to read and hard to override. - Confusing
.a.b(one element with both classes) with.a .b(a.binside an.a).
Frequently asked questions
›What is the difference between a class and an ID selector?
A class (.name) can be used on many elements and has low specificity. An ID (#name) must be unique on the page and has much higher specificity, which makes it harder to override.
›What is CSS specificity?
Specificity decides which rule wins when several target the same element. Inline styles beat IDs, IDs beat classes, attributes and pseudo-classes, and those beat element selectors. If specificity is equal, the later rule wins.
›Is there a parent selector in CSS?
Yes, :has(). For example, li:has(> a.active) selects a list item that contains an active link. It's supported in all modern browsers.