Lecture 19 — CSS Class Selectors⌗

Instructor: Laksh Budhrani


Objectives⌗

By the end of this lesson, you will be able to:

  1. Understand the purpose of CSS class selectors.
  2. Assign classes to HTML elements.
  3. Use the same class to style multiple elements.
  4. 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⌗

TagWhat It Does
<div>Groups larger sections of content
<span>Groups a small part of text

CSS Properties⌗

PropertyWhat It Does
background-colorChanges the background color
font-familyChanges the font
colorChanges the text color
font-sizeChanges the text size
text-alignAligns text horizontally
text-decorationAdds or removes decorative lines
widthControls the width of an element
heightControls the height of an element
borderAdds an outline around an element
paddingAdds space inside the border
marginAdds space outside the border
displayControls 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;
}

CSS Class Selector Example


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 named highlight to the <div>.
  • .highlight in 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!

Same Class Example


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:

  • .card styles the first element.
  • .notice styles the second element.

Even though both elements are <div> tags, they have different styles because they belong to different classes.

Different Classes Example

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.

JP2 Academic Departments Class Example


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.

Taylor Swift Class Selector Example


Summary: CSS Class Selectors Learned Today⌗

ConceptWhat It Does
class attributeAssigns a class name to an HTML element
.cardSelects all elements assigned the card class
.noticeSelects 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.