Skip to content
Tutorials Center
My learning

Topics

Lesson 6 of 139 ยท Selectors

Selector Basics

Learn the five basic CSS selectors (type, class, ID, universal and comma grouping), when to use each one, and how to combine them into precise, readable rules.

Every CSS rule starts with a question: which elements should this apply to? The answer is the selector, the part before the curly braces. A declaration like color: crimson does nothing on its own; the selector is what points it at real elements on the page.

CSS has dozens of selectors, but most stylesheets lean on a small core of five: the type selector, the class selector, the ID selector, the universal selector and selector lists (grouping with commas). This lesson covers each of them, shows how to combine them, and gives you a feel for which to reach for first. The rest of this chapter builds on these basics with combinators, attribute selectors and pseudo-classes.

Type selectors

A type selector is just the name of an HTML element. It matches every element of that type in the document.

CSS
p {
  line-height: 1.6;
}

h2 {
  color: #1e3a8a;
}

Type selectors are case-insensitive for HTML, so P and p match the same elements, but lowercase is the universal convention. They are perfect for base styles: how a paragraph, a heading or a link should look by default, everywhere. They are a poor fit for anything more specific, because you rarely want every <div> or every <ul> to look the same.

Type selectors set the defaults

Class selectors

A class selector is a dot followed by a class name. It matches every element whose class attribute contains that name.

HTML
<p class="intro">Welcome!</p>
<div class="card featured">โ€ฆ</div>
CSS
.intro { font-size: 1.2em; }
.card { border: 1px solid #ddd; }
.featured { border-color: gold; }

An element can carry several classes, separated by spaces in the class attribute. Above, the <div> matches both .card and .featured. That lets you build styles from small, reusable pieces: a base class for the component, plus extra classes for variants.

Classes are the workhorse of CSS because they are:

  • Reusable. Any number of elements can share a class.
  • Independent of the HTML structure. .warning works on a <p>, a <div> or an <li>.
  • Light. One class has low specificity, so it is easy to override later.

Class names are case-sensitive: .Card does not match class="card". Pick a convention (lowercase with hyphens is the most common) and stick with it.

Chaining classes

Write two or more classes with no space between them to require all of them on the same element:

CSS
.button.large { padding: 14px 28px; }

This matches <button class="button large"> but not an element that has only one of the two classes. Chaining is useful, but don't overdo it; each extra class adds weight to the selector.

Combining reusable classes

ID selectors

An ID selector is a hash (#) followed by an ID. It matches the element whose id attribute equals that value.

CSS
#site-header {
  background: #0f172a;
}

An id must be unique in the document, so an ID selector should match at most one element. IDs are essential in HTML: they are targets for links like href="#pricing", they connect <label for> to inputs, and JavaScript uses them to find elements.

In CSS, though, IDs are best used sparingly. An ID selector has much higher specificity than a class (you will learn exactly how much in the Specificity lesson), so a rule like #sidebar a is hard to override without another ID. Most teams style with classes and leave IDs for linking and scripting.

If you need to style one unique element, give it a class anyway. .site-header does the same job as #site-header without the specificity cost, and you can reuse it if a second header ever appears.

The universal selector

The universal selector * matches every element.

CSS
* {
  margin: 0;
}

On its own, * is most often seen in resets, such as the popular box-sizing rule covered in box-sizing:

CSS
*,
*::before,
*::after {
  box-sizing: border-box;
}

The universal selector adds nothing to specificity, and selectors that use it are not slow in modern browsers. Still, use it deliberately: a rule on * touches every element, including ones you forgot about, like <option> or <svg> children.

Grouping with commas

If several selectors need the same declarations, list them with commas instead of repeating the block. This is called a selector list.

CSS
h1,
h2,
h3 {
  font-family: Georgia, serif;
  line-height: 1.2;
}

Each item in the list works independently. The rule above means the same as three separate rules for h1, h2 and h3. Putting one selector per line, as shown, makes long lists easy to scan and to edit.

There is one trap: if any selector in a plain list is invalid, the browser throws away the whole rule. A typo or an unsupported selector in one item breaks all the others.

One bad selector spoils the list

The :is() and :where() functions accept "forgiving" selector lists that skip invalid items instead of dropping the rule. You will meet them at the end of this chapter.

Combining selector types

You can attach classes, IDs and other simple selectors to a type selector with no spaces. The element must satisfy every part:

Selector Matches
p.lead <p> elements with the class lead
button.primary.wide <button> elements with both classes
section#faq the <section> whose id is faq
.card.is-active elements with both card and is-active

The type, if present, must come first: p.lead is valid, .leadp is just a class named leadp. A space changes the meaning completely: p .lead (with a space) means "a .lead element inside a <p>". That is a combinator, the topic of the next lesson.

In practice, avoid qualifying classes with types unless you need to. .lead is simpler than p.lead, works if the element changes to a <div>, and has lower specificity.

Choosing the right selector

A sensible default order of preference:

  1. Type selectors for base styles of plain elements: body, h1, p, a, table.
  2. Class selectors for components and variants: .card, .button-primary, .is-open.
  3. Grouping to avoid repeating the same declarations.
  4. IDs almost never in CSS; keep them for links, labels and scripts.
  5. The universal selector for a handful of global resets.

You can always see which selectors matched an element, and which won, in the Styles panel of your browser's DevTools.

All five basics in one page

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which selector matches every element that has the class price?

    A dot starts a class selector. #price is an ID selector, and price would be a type selector for a (nonexistent) <price> element.
  2. 2. What does .card.featured match?

    Chained classes with no space between them must all be present on the same element.
  3. 3. In the rule h1, .title, #@@ { color: red; }, what happens?

    In a plain selector list, one invalid selector makes the browser drop the entire rule.
  4. 4. Why do many teams avoid ID selectors in CSS?

    ID selectors work fine, but their specificity outweighs any number of classes, which makes later overrides awkward.
  5. 5. Which statement about the universal selector * is true?

    * matches all elements and contributes zero specificity.

Summary

  • A selector decides which elements a rule applies to.
  • Type selectors (p) match elements by tag name and are great for base styles.
  • Class selectors (.card) are reusable, structure-independent and light: your default choice.
  • ID selectors (#main) match one element but carry heavy specificity; prefer classes.
  • The universal selector (*) matches everything and is mostly used in resets.
  • Selector lists (h1, h2) share one declaration block, but one invalid item drops the whole rule.
  • Simple selectors written together (p.lead, .a.b) must all match the same element.

Next: Combinators โ€” selecting elements by where they sit in relation to others.