Class attribute

Hi, new to coding here.
Can anyone explain to me what is class means actually?
When to use it in our html?
I actually searched online but I still can’t get it… :sob:
HELP

Hey there, if you’ve taken the Intro to Webpages course, you learned about the id attribute, which allows you to give a specific element an id, and then give that element its own style using CSS, like this:

<h1 id='title'>Welcome to my webpage!</h1>
#title {
    color: blue;
}

The class attribute is very similar, but you can use it on multiple elements. Then, in CSS, you can give styles to that entire group of element.

For example, say you have a blog where you use a lot of <p> tags, but for different purposes, such as text for blog posts and text for an About Me section. You could use classes like this:

<p class='blog-post'>blog blog blog blog text text text</p>
<p class='blog-post'>yada yada yada humdee dumdee doodah</p>
<p class='about-me'>text text text some stuff about me yada yada</p>
<p class='about-me'>more content about me</p>

Then, in CSS, you can select and style each class using a period followed by the class name, like this:

.blog-post {
    color: red;
}

.about-me {
    color: blue;
}

Hope that clears things up!
Ben

1 Like