Lecture 19 — CSS Class Selectors
Lecture 19 — CSS Class Selectors⌗
Instructor: Laksh Budhrani
Objectives⌗
By the end of this lesson, you will be able to:
- Understand the purpose of CSS class selectors.
- Assign classes to HTML elements.
- Use the same class to style multiple elements.
- Use different classes to apply different styles to elements.
Quick Recall: Previous Lectures Review⌗
Let’s quickly recall what we have learned so far!
HTML Tags⌗
| Tag | What It Does |
|---|---|
<div> | Groups larger sections of content |
<span> | Groups a small part of text |
CSS Properties⌗
| Property | What It Does |
|---|---|
background-color | Changes the background color |
font-family | Changes the font |
color | Changes the text color |
font-size | Changes the text size |
text-align | Aligns text horizontally |
text-decoration | Adds or removes decorative lines |
width | Controls the width of an element |
height | Controls the height of an element |
border | Adds an outline around an element |
padding | Adds space inside the border |
margin | Adds space outside the border |
display | Controls how elements are arranged |
Objective 1 — Understanding Class Selectors⌗
So far, we have used HTML tags as CSS selectors.
For example:
p {
color: blue;
}
This applies the styling to all <p> elements on the webpage.
But what happens when we want to style only specific elements instead of every element of the same type?
This is where class selectors are useful!
What it does: A class selector allows us to select and style specific HTML elements based on their class name.
A class selector begins with a period (.) followed by the class name.
Example:
<p class="important">Important Message!</p>
<p>Regular Message</p>
.important {
background-color: yellow;
}

Objective 2 — Assigning Classes to HTML Elements⌗
We assign a class to an HTML element using the class attribute.
For example:
<div class="highlight">
Special Announcement!
</div>
Here:
class="highlight"assigns the class namedhighlightto the<div>..highlightin CSS selects that element and applies the styling.
Remember:
- In HTML, we write
class="highlight". - In CSS, we write
.highlight. - The period (
.) is used only in the CSS selector.
Objective 3 — Using the Same Class for Multiple Elements⌗
One of the biggest advantages of classes is that we can apply the same styling to multiple elements.
Example:
<div class="card">First Card</div>
<div class="card">Second Card</div>
<div class="card">Third Card</div>
.card {
background-color: lightblue;
border: 2px solid blue;
width: 150px;
height: 80px;
padding: 10px;
}
All three <div> elements have the same class. The .card selector applies the same styling to all three!

Objective 4 — Using Different Classes for Different Elements⌗
We can also create different classes when elements need different styles.
Example:
<div class="card">First Card</div>
<div class="notice">
Important Notice!
</div>
.card {
background-color: lightblue;
border: 2px solid blue;
}
.notice {
background-color: lightyellow;
border: 2px solid orange;
}
Here:
.cardstyles the first element..noticestyles the second element.
Even though both elements are <div> tags, they have different styles because they belong to different classes.

Remember: A class selector allows us to style specific groups of elements independently.
Guided Activity — Explore JP2 Academic Departments!⌗
The Story⌗
John Paul II Catholic School wants to showcase its academic departments on its website.
Let’s create three cards featuring:
- Science & Technology
- Fine Arts
- Humanities
Your Task⌗
Open jp2_website.html and create three cards for these departments.
Use the same class to style all three cards. Add a separate school announcement with a different class.

Independent Activity — What Makes Your Favorite Person Special?⌗
Open your existing favorite_website.html file.
Your Task⌗
Create three cards highlighting what makes your chosen person special.
For example:
- Singer → Storytelling, creativity, connection with fans
- Actor → Acting skills, memorable roles, awards
- Athlete → Talent, achievements, sportsmanship
Also, add a separate Fun Fact section about your chosen person.
Use the same class for all three cards and a different class for the Fun Fact section.

Summary: CSS Class Selectors Learned Today⌗
| Concept | What It Does |
|---|---|
class attribute | Assigns a class name to an HTML element |
.card | Selects all elements assigned the card class |
.notice | Selects all elements assigned the notice class |
Remember: We use class selectors to style specific groups of elements instead of applying the same styling to every element of a particular HTML tag.