CHAPTER 02
Beginner
CSS Selectors and Specificity
Updated: May 10, 2026
5 min read
# CSS Selectors and Specificity
1. Introduction
Welcome to Chapter 2! CSS Selectors are the foundation of styling. They are the rules you use to target exactly which HTML elements you want to design. If CSS is a paintbrush, selectors are how you choose the canvas.2. Learning Objectives
By the end of this chapter, you will understand:- How to select elements by tag, class, and ID.
- Advanced selectors like descendant and child selectors.
- How to use pseudo-classes and pseudo-elements.
- The concept of CSS Specificity (the cascade).
3. Detailed Explanations
Element Selectors
The simplest way to target elements is by their HTML tag name.
css
Class Selectors
Class selectors target elements with a specificclass attribute. They start with a dot .. You can use a class on multiple elements.
css
ID Selectors
ID selectors target a single, unique element using theid attribute. They start with a hash #.
css
Universal and Group Selectors
The universal selector* targets *everything*. Group selectors let you apply the same styles to multiple selectors by separating them with commas.
css
Descendant and Child Selectors
Target elements based on their relationships.- Descendant (Space): Targets any element inside another, no matter how deep.
-
Child (
>): Targets only direct children.
css
Pseudo-classes
Pseudo-classes target an element in a specific *state*, like when a user hovers over it.
css
CSS Specificity
When two rules conflict, which one wins? CSS uses a points system called Specificity:- 1. Inline styles (Highest)
- 2. IDs
- 3. Classes, Attributes, Pseudo-classes
- 4. Elements (Lowest)
css
4. Mini Project: Blog Article Styler
Let's build a styled article using these selectors!
html
css
5. MCQs
Q1: Which selector is used to select an element with a specific ID? A).
B) #
C) *
D) >
*Answer: B*