What are CSS selectors?
CSS selectors are patterns that match HTML elements so you can apply styles to them. Think of them as the "addresses" for elements on a page โ they tell the browser exactly which elements to style.
Without selectors, CSS would be impossible. You'd have no way to say "make all paragraphs blue" or "style only the main header red." Selectors are how CSS finds and targets elements.
{ }) describes how to style them.
In this lesson, you'll learn 8 types of selectors that cover virtually every styling situation you'll encounter.
Element selectors
The simplest selector: target all elements of a specific type by their tag name. p selects all <p> tags, h1 selects all <h1> tags, and so on.
When to use: Element selectors are perfect for applying base styles that should apply to every element of that type on the page. Use them for typography, spacing, and layout defaults.
Limitation: Element selectors can't distinguish between different uses of the same element. If you have 10 paragraphs but only want to style 2 of them, you'll need a more specific selector (like class or ID).
Class and ID selectors
Element selectors are broad. Class and ID selectors let you target specific elements.
Class selectors
Use .classname to target any element with that class. Classes are reusable โ multiple elements can share the same class, and a single element can have multiple classes.
ID selectors
Use #idname to target a single element. IDs are unique โ only one element per page should have a specific ID. Use IDs sparingly, mainly for major layout sections.
Universal selector (*)
The * selector matches every element on the page. It's powerful but use it carefully.
Common use: CSS resets. Most developers start with * { margin: 0; padding: 0; } to remove the browser's default spacing and get a clean slate.
Performance note: Using * has a tiny performance cost on large pages, but it's usually negligible. Don't worry about it unless you're optimizing for milliseconds.
Grouping selectors
Apply the same styles to multiple selectors at once by separating them with commas.
Grouping is purely a convenience โ h1, h2, h3 { ... } is identical to writing three separate rules. It just keeps your CSS DRY (Don't Repeat Yourself).
Combinator selectors
Combinators describe the relationship between elements. They let you target elements based on where they appear in the HTML structure.
Descendant selector (space)
Use a space to select any element that is a descendant of another. div p selects all <p> elements inside any <div>, no matter how deeply nested.
Child selector (>)
Use > to select only direct children. div > p selects only <p> elements that are direct children of <div> โ not grandchildren or deeper.
Difference: Descendant div p matches deeply nested paragraphs. Child div > p matches only the immediate children. Child selectors are more precise.
Adjacent and general sibling selectors
Sibling selectors target elements based on their siblings (elements at the same level in the HTML tree).
Adjacent sibling selector (+)
Use + to select an element that immediately follows another. h1 + p selects only the first <p> immediately after an <h1>.
General sibling selector (~)
Use ~ to select any element that follows another (not just immediately). h1 ~ p selects all <p> elements that come after an <h1>, at the same nesting level.
Attribute selectors
Attribute selectors match elements based on their attributes. You can select links based on their href, inputs based on their type, images based on their src, and more.
Attribute selector types:
[attr]โ Element has the attribute (any value)[attr="value"]โ Exact match[attr^="value"]โ Starts with[attr$="value"]โ Ends with[attr*="value"]โ Contains[attr~="value"]โ Contains word (space-separated)
Try it yourself: Basic selectors
Edit the CSS to style different elements. Try using element, class, ID, and grouping selectors to color-code the different types of content.
Try it yourself: Advanced selectors
Use descendant, child, sibling, and attribute selectors to target nested elements. Notice how each selector type targets elements differently.
Selector specificity
When multiple selectors target the same element, which style wins? The answer is specificity โ a ranking system that CSS uses to determine which rules apply.
Specificity is calculated as a three-part score: (ID, CLASS, ELEMENT).
#idselector = 100 specificity.classselector = 10 specificityelementselector = 1 specificity- Combined selectors add up:
.class element= 11 specificity
Real-world: Building a blog card
Here's how selectors work in a real design. We'll style a blog post card using all the selectors we learned.
Task: Create a styled blog page using CSS selectors (no inline styles). Your page should have:
- Element selectors for base styles (p, h2, a)
- A class selector for reusable "card" components
- An ID selector for the main header
- Descendant selectors to style elements inside cards
- Attribute selectors to style links differently based on href
- At least one child selector (>) and one sibling selector (+)
Build it in an editor above, or use the template code in the "Real-world" section as a starting point.
Hint: Start with the card example above. Add more sections, use h1 with an ID for the main header, create multiple cards with different content, and experiment with descendant and sibling selectors to add visual separation between elements.
Common selector mistakes
- Forgetting the dot for classes:
.classnotclass. Forgetting it means "select all <class> elements" which don't exist. - Forgetting the hash for IDs:
#idnotid. Same problem as classes. - Over-nesting:
.card .button .icon spanis too specific. Use shorter, simpler selectors. If you need something, give it a class. - Mixing up > and space:
div > p(direct children) vsdiv p(any descendants). Know the difference. - Using multiple IDs: IDs should be unique. Don't use the same ID on multiple elements. Use classes instead.
- Assuming order matters in HTML:
a + bmeans "b that immediately follows a." If there's anything between them, it won't match.
Key takeaways
- Element selectors apply styles to all elements of a type. Use for base styles.
- Classes (
.name) are reusable. Use them for components and patterns. Multiple elements can share one class. - IDs (
#name) are unique. Use sparingly, mostly for major layout sections. - Combinators (space,
>,+,~) describe relationships between elements. Powerful for styling nested layouts. - Attribute selectors let you target elements based on their attributes (
[type="text"],[href^="https"], etc.). - Specificity determines which rule wins. Keep it low. Avoid deep nesting.
- Selectors are how CSS finds elements. Master them, and you can style anything.
Test your knowledge
You just mastered CSS selectors.
Imagine what you'll design next.
Join the Best AI Programs โ three 5-month courses, fully online, with placement assistance. Week 1 is free.
Explore our AI-era courses โ