0Pricing
HTML for Kids · Lesson

Class Selector

Class Selector

Class Selector is a free HTML for Kids lesson on CoddyKit — lesson 1 of 1. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the HTML for Kids learning path, one of 1 lessons in the course, and your progress syncs across the web and the CoddyKit app.

1

Hello,

We came to class selectors, one of the most critical issues in CSS.

We know the class selector because we have used the class selector frequently until now.

In this lesson, let's both consolidate our knowledge and learn about its advantages over other selectors.

10

So let's start.

We use the dot (.) selector to define a class type selector. Below is a class selector named my-class and the font-size style is 10px; has been passed.

.my-class {

font-size: 10px;

}

We can also define more than one style for a class selector.

.my-class {

font-size: 10px;

}

Multiple Selector

We can assign more than one class to the same element. 

In the example below, two-class selectors named el1 and el2 are defined.

Two or more classes can be added to the class attribute of the element created with the p tag, leaving a space between them.

You can see that the styles from the two classes are reflected in the element at the same time in the preview section.

<style>
.el1{
     color:red;
}
.el2{
     font-size:30px;
}
</style>

<p class="el1 el2">
     Hello Class Selector
</p>

So why should we choose the class selector over the tag and id selector?

So why should we choose the class selector over the tag and id selector?

When we use the tag selector, it can lead to unwanted results because it affects all the elements created with this tag. Therefore, we usually use these tags if there is a common feature that all elements should have.

For example, if we want each font family of an element to be 'Lato' we make a definition as follows.

a{
   font-family:  Lato,sans-serif
}

So why should we choose the class selector over the tag and id selector? 2

We could make an element-specific definition with the id selector.

However, since all our style definitions are valid for only one element, we had to rewrite the codes for another element that we wanted to show the same properties

Because there can only be one element in an HTML document with the same id.

CSS Specificity

CSS Specificity

Now let's learn the concept of CSS Specificity.

Let's create a problem again to understand the subject better, and we will learn this concept while solving this problem.

CSS Specificity 2

In the example below,

A style definition has been made for the p tag.

A style definition is made with a class selector named .first.

Both selectors chose the same element in our HTML document

Both selectors have defined styles to replace the same color property. In this case, there will be a conflict.

<style>
.first{
   color:blue;
}

p{
   color:red;
}
</style>

<p class="first">
Enroll Now And Start Learn Codding?
</p>

CSS Specificity 3

When we look at the example, we can think that the color will be red based on our previous knowledge.

Because the concept of cascade tells us that the last place in the code order will win.

However, there is a small nuance. If they have the same specificity, the last incoming code wins.

It is an evaluation that shows the importance of the specificity selector on the relevant element or elements.

Regardless of its rank in the code with high specificity rank, it will be evaluated by the browser.

Warning

The class selector is a more specific definition than the element selector. Therefore, the Specificity rank is higher.

16

Finding out which selector is more specific is actually very simple.

Since there is only 1 ID assigned to an element and there is only 1 on the web page, we expect it to be the top priority selector.

Class selectors, on the other hand, can be evaluated more specifically than tag selectors because they choose a more specific group than the tag.

We will see that the id selector wins in the example below by selecting the same element with 3 different selectors.

<style>
   #first{
   color:green;
   }
   .first{
   color:blue;
   }
   p{
   color:red;
   }
</style>
<p class="first" id="first">
   Enroll Now And Start Learn Codding?
</p>

17

You can access the online Specificity calculator from this link.

18

So do I have to use these calculations all the time?

Of course not. Conflicts will not occur when we write our codes according to the standards.

So you don't have to do mathematical calculations all the time, but when there is a problem, you may need this information to solve the problem.

Congratulations

Congratulations. 🥳🥳🥳

You have finished the Class Selector and CSS Specificity lesson.

In the next section, we will learn the Selector Types.

You can click to continue in the next section.

Class Selector — illustration 13

Frequently asked questions

Is the “Class Selector” lesson free?

Yes — the full text of “Class Selector” is free to read here on the web, and the HTML for Kids course includes 1 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the HTML for Kids course, upgrade to CoddyKit PRO.

What will I learn in “Class Selector”?

Class Selector You practise HTML for Kids with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start HTML for Kids?

No prior experience is required. HTML for Kids on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 1, so you can start here or from the beginning and move at your own pace.

How long does the “Class Selector” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this HTML for Kids lesson?

Yes. Every HTML for Kids lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

← Back to HTML for Kids