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.

Key concept:
A selector describes which elements to style. A declaration block (inside { }) 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.

styles.css Element selectors target all elements of a type

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.

styles.css Class selectors are reusable and flexible

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.

styles.css ID selectors target unique elements
Best practice:
Prefer classes over IDs. Classes are more reusable and have lower specificity, making your CSS easier to manage. IDs should be reserved for unique, major sections.

Universal selector (*)

The * selector matches every element on the page. It's powerful but use it carefully.

styles.css The universal selector matches all elements

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.

styles.css Grouping selectors avoid repetition

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.

styles.css Descendant selectors target nested elements

Child selector (>)

Use > to select only direct children. div > p selects only <p> elements that are direct children of <div> โ€” not grandchildren or deeper.

styles.css Child selectors target direct children only

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>.

styles.css Adjacent siblings follow directly

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.

styles.css General siblings can be anywhere after

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.

styles.css Attribute selectors match based on attributes

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 โ€” Basic Selectors
Preview

Try it yourself: Advanced selectors

Use descendant, child, sibling, and attribute selectors to target nested elements. Notice how each selector type targets elements differently.

โœ๏ธ Try It Yourself โ€” Advanced Selectors
Preview

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).

  • #id selector = 100 specificity
  • .class selector = 10 specificity
  • element selector = 1 specificity
  • Combined selectors add up: .class element = 11 specificity
specificity.css Higher specificity wins
Pro tip:
Keep specificity low. Avoid excessive nesting and ID selectors. Write CSS that's easy to override and extend.

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.

blog-card.html + css Using selectors to style a realistic component
Mini Challenge: Style a Blog Page

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: .class not class. Forgetting it means "select all <class> elements" which don't exist.
  • Forgetting the hash for IDs: #id not id. Same problem as classes.
  • Over-nesting: .card .button .icon span is too specific. Use shorter, simpler selectors. If you need something, give it a class.
  • Mixing up > and space: div > p (direct children) vs div 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 + b means "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

Question 1 of 5
How do you select all elements with the class "active"?
Question 2 of 5
What does div > p select?
Question 3 of 5
Which selector targets a unique, one-of-a-kind element?
Question 4 of 5
What does [href^="https"] select?
Question 5 of 5
What does h1 + p select?
0
/5
Great work! You understand CSS selectors.
Africa & India's Best AI Academy
๐Ÿค– AI-first ๐ŸŽ“ Placement Assistance

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 โ†’