A rule is only useful if it reaches the right elements. Selectors are the patterns that choose them, and understanding them is the key skill in CSS.

Basic Selectors

An element selector uses a tag name, such as p, and chooses every element of that type. A class selector begins with a dot, for example .note, and chooses every element whose class attribute includes that name. An id selector begins with a hash sign, for example #top, and chooses the one element with that id. The universal selector * chooses every element.

Definition: Selector. The part of a CSS rule that chooses which elements to style.

Table 2.3: Basic selectors

Selector Example Chooses
Element p Every paragraph
Class .note Every element with class note
Id #top The one element with id top
Universal * Every element
Grouping h1, h2 Both headings
Descendant nav a Links inside a nav element

Grouping and Combining

A comma lets one rule serve several selectors: h1, h2, h3 { color: navy; }. A space between two selectors chooses descendants, for example nav a chooses only the links that sit inside a nav element. Combining a type and a class, such as p.note, chooses only paragraphs with that class.

Pseudo-classes and Pseudo-elements

A pseudo-class is added to a selector with a colon and chooses an element in a certain state. The selector a:hover chooses a link while the pointer is over it. Other examples are :focus, :first-child and :nth-child(2). A pseudo-element is written with two colons and chooses a part of an element, for example p::first-line or p::first-letter.

Definition: Pseudo-class. A keyword added to a selector that targets an element in a certain state, such as hover.

Specificity

Several rules can reach the same element. Specificity is the weight that decides which one wins. An id selector is heavier than a class selector, and a class selector is heavier than an element selector. An inline style is heavier than all of them. When the weights are equal, the rule that comes last wins.

Definition: Specificity. The weight that decides which rule wins when several rules style the same element.

Example 2.3: Predicting the winner

Three rules reach the same paragraph. The element rule is lightest, the class rule is heavier and the id rule is heaviest.

<style>
  p      { color: blue; }
  .note  { color: green; }
  #alert { color: red; }
  a:hover { color: orange; }
</style>
<p id="alert" class="note">I am red.</p>
<p class="note">I am green.</p>
<p>I am blue.</p>
<a href="#">Hover over this link</a>

Output in the browser:

Common mistakes

  • Using the same id on several elements in one page.
  • Forgetting the dot before a class name or the hash before an id name.
  • Expecting a later rule to win over a more specific earlier rule.
  • Writing a descendant selector with a comma, which instead groups the selectors.

Key takeaways

  • Element selectors choose by tag, class selectors by .name and id selectors by #name.
  • A comma groups selectors, and a space chooses descendants.
  • Pseudo-classes such as hover choose a state, and pseudo-elements choose a part of an element.
  • Specificity rises from element to class to id to inline style.

Practice questions

  1. Which symbol begins a class selector?
  2. What does a:hover choose?
  3. Which wins, an id selector or a class selector?

Answers to the practice questions

  1. A dot.
  2. A link while the pointer is over it.
  3. The id selector, because its specificity is higher.

Sources

  • Official BCA syllabus (NEP 2025 scheme), Web Application Development, course code 25BCA405DS04, Unit II