Selectors
Tell CSS exactly which HTML elements you want to find, target, and style.
.note { … }
#welcome { … }
element
class
id
>_ What Is a CSS Selector?
A CSS selector tells the browser which HTML element — or group of elements — should receive a set of CSS styles.
Think of a selector as CSS saying: “Find this part of the webpage.” The declarations inside the curly braces then say: “Now style it like this.”
Element Selector
Targets every HTML element with a particular tag name.
Class Selector
Targets every element that uses a particular class.
ID Selector
Targets an element using its unique ID.
>_ Element Selectors
An element selector uses the HTML tag name itself.
For example, this selector targets every paragraph on the page:
color: #087b49;
}
If the page contains five <p> elements, all five will receive this color.
>_ Class Selectors
A class selector begins with a period followed by the class name.
font-weight: bold;
color: #0077cc;
}
This rule only affects elements that contain class=”important”.
>_ ID Selectors
HTML also has an id attribute. An ID is used to identify a specific element.
CSS selects an ID using the # symbol.
Welcome to mC0de
</h1>
#welcome {
color: #071a35;
text-align: center;
}
>_ Spot the Difference
p
An element selector targets every matching HTML tag.
.message
A class selector targets elements using class=”message”.
#welcome
An ID selector targets the element using id=”welcome”.
{ ... }
The declarations inside the braces determine how the selected elements look.
>_ Using Selectors Together
A stylesheet can contain many different selectors. Each selector can target a different part of the page.
color: #52677a;
}
.highlight {
background-color: #e9fff3;
}
#welcome {
text-align: center;
}
The symbols matter:
p selects an element,
.note selects a class, and
#welcome selects an ID.
One tiny symbol can completely change
what your CSS targets.
Target the Page
Use three different selector types to style specific parts of a mini webpage.
-
✓
Use an element selector
to style every
<p>. -
✓
Create a class selector named
.highlight. -
✓
Give
.highlighta background color. -
✓
Create an ID selector named
#welcome. -
✓
Use
#welcometo center and color the main heading. -
✓
Apply
class="highlight"to only one paragraph.
Selectors Challenge
What You’ve Learned
p target HTML tags.
.highlight.
#welcome.
Nice work!
Finished this lesson? Mark it complete to track your progress.


