CSS Selectors 101: Targeting Elements with Precision
Why CSS Selectors Are Needed
When you write HTML, you create structure — like headings, paragraphs, and buttons. But structure by itself doesn’t have style. That’s where CSS selectors come in:
Selectors tell CSS which elements to style.
Think of it like this:
If HTML is the building, CSS selectors are the address labels telling the browser where to apply the paint.
Selectors let you define:
What to style
Which grouping of elements you want CSS to affect
Without selectors, your CSS wouldn’t know which elements to apply rules to.
Element Selector
The element selector targets all elements of a specific type.
Example
p {
color: blue;
}
That means:
“Style all <p> elements with blue text.”
Here’s the idea:
You’re choosing all elements of a certain tag
Useful for global, basic styling
This is the simplest selector.
Class Selector
A class selector targets elements with a specific class attribute.
Element with class: <p class="highlight">This is important.</p>
CSS:
.highlight {
background-color: yellow;
}
This will style all elements with class "highlight", no matter the tag.
You can use the same class on many elements - that’s why class selectors are so common in CSS.
ID Selector
The ID selector targets a single, unique element on the page.
HTML: <h1 id="page-title">Hello World</h1>
CSS:
#page-title {
font-size: 32px;
}
Only the element with id="page-title" gets styled.
IDs should be unique in a document - you shouldn’t reuse the same ID on multiple elements.
Group Selectors
Sometimes you want to apply the same style to several selectors. Instead of repeating yourself, you can group them with commas.
h1, h2 {
font-family: Arial, sans-serif;
}
This rule:
- Styles both
<h1>and<h2>
Helps keep CSS clean and less repetitive.
Descendant Selectors
A descendant selector targets elements inside another element. You write two selectors separated by a space.
div p {
color: red;
}
This means: “Make only paragraphs inside <div> elements red.”
Paragraphs outside a <div> won’t be affected.
This is useful when HTML structure matters - for example, navigation lists inside a header vs content paragraphs inside a section.
Basic Selector Priority (High‑Level)
Sometimes more than one selector matches an element. When that happens, CSS uses priority (also called specificity) to decide which rule wins.
From lowest to highest priority:
Element selector (e.g.,
p { ... })Class selector (e.g.,
.highlight { ... })ID selector (e.g.,
#main-header { ... })Inline styles (defined directly on the element) are the strongest
In general:
More specific selectors override less specific ones.
If two selectors have the same specificity, the one later in your CSS wins.
Before/After Example
Without any CSS
<p>Hello world</p>
<h1>Greetings!</h1>
All elements look default - plain and unstyled.
Using Selectors to Style
p {
color: blue;
}
#main-title {
text-align: center;
font-size: 2rem;
}
.highlight {
background: yellow;
}
Now:
All
<p>text is blueThe element with the id
main-titlegets big centered textAny element with class
highlightget a yellow background
Selectors decide which pieces of HTML get which styles - that’s the core job.
Real‑World Analogy: Addressing People
Think of selectors like instructions in a room:
Element selector: “Everyone with blue shirts, gather here.”
Class selector: “Everyone in group A, listen up.”
ID selector: “Alice, come here.”
Group selector: “Alice, Bob, and Charlie - stand together.”
Descendant selector: “Everyone sitting in front row, raise your hand.”
All of these are ways of targeting specific people or groups - just like CSS selectors target elements in the DOM.
Summary
CSS selectors are the foundation of styling on the web. They tell CSS who to apply style rules to.
You learned:
Element selector- targets all elements of a type
Class selector - targets reusable groups
ID selector - targets a specific unique element
Group selector - applies same rules to multiple selectors
Descendant selector - targets elements nested inside another
Basic selector priority - determines which rule wins when multiple match