Skip to main content

The CSS Selector

The CSS selector is the most crucial part of any CSS ruleset. Its job is simple: to identify which specific HTML element(s) on the page the declared styles should be applied to.

Think of the selector as the name or address of the element you want to style.


The CSS Ruleset Structure

Every piece of CSS is organized into a ruleset. The ruleset begins with the selector, followed by a declaration block (the curly braces {}) containing the actual styles.

styles.css
/* This is a complete CSS Ruleset */
selector {
property: value;
}

In this structure:

  • Selector: Targets the element (e.g., h1, .card, #logo).
  • Declaration Block: Holds all the style declarations for that selector.
  • Declaration: The specific style instruction (a property and a value).

Example Ruleset Breakdown

In the example below, the word h2 is the selector.

styles.css
/* h2 is the selector */
h2 {
color: midnightblue;
font-size: 24px;
}

This ruleset tells the browser: "Find all the <h2> tags on the page and make their text color midnightblue with a 24px font size."

Types of Selectors

There are many ways to select an element, ranging from broad targeting (selecting all elements) to extremely specific targeting (selecting a unique element). In later lessons, we will dive deep into each one, but here are the foundational types:

Selector TypeExampleTargetsSpecificity Impact
Elementp, div, aAll instances of that HTML tag.Low (Score: 1)
Class.highlight, .buttonAll elements with the matching class attribute.Medium (Score: 10)
ID#header, #main-contentThe one unique element with the matching id attribute.High (Score: 100)
Universal*Every element on the page.Very Low (Score: 0)

Why the Selector is So Important

The selector isn't just about applying a style; it's about control and management:

  1. Isolation: A good selector ensures you only apply styles to the intended elements, avoiding accidental changes across your site.
  2. Maintainability: Using common selectors like classes (.class-name) means you can change the visual style of dozens of elements by modifying a single CSS ruleset.
  3. Performance: Efficient, well-written selectors (like a simple class selector) are faster for the browser to process than overly complex or deeply nested selectors.
  4. Specificity Control: As you learned in the Cascade lesson, the type of selector you choose directly affects its Specificity score, determining whether it will override or be overridden by other styles.

Interactive Selector Demo

In the live editor, change the selector from h2 to p or to the class .info-box and watch which elements get styled.