HTML Introduction to HTML and CSS Getting Familiar with HTML and CSS Building Web Pages with HTML and CSS

Waqas Khan
PRO
Waqas Khan
Pro Student 2,661 Points

why we use "class element" in html, and what is the purpose of class element?

why we use "class element" in html, and what is the purpose of class element?

2 Answers

Hi Waqas,

I believe you mean class attribute. Class attribute is used to point to a class in your stylesheet. For example, you would give an html element a class when you want to target it with specific css.

Classes can also be used by JavaScript to make changes to HTML elements with that specific class.

Hope this answers your question.

Brent Pfefferle
Brent Pfefferle
3,641 Points

In html, we give an element the class attribute when we want to style more than one element. For example, let's say you have multiple paragraph tags that you want to color blue. If so, assign <p class="blue-paragraph></p> to each paragraph.

Then, in your CSS, you can refer to each paragraph tag with .blue-paragraph.

Overall, it becomes easier to style multiple elements.