Class Attribute in HTML and CSS


#1

I’m having a hard time understanding when to use the class attribute and why I would need to use it. I have seen it used with the div tag. I have seen it used with an a tag. And I am currently learning that it can be referenced in CSS, through the Learn CSS selectors and Visual Rules session.

The thing is I still don’t understand why I would need to use the class attribute at all, as this wasn’t fully explained in the introduction to HTML course. I’ve checked the W3School website but there it mostly shows me how to use it and not why I would use it. Could anyone please explain this to me?


#2

as your webpage grows, you might end up with multiple div elements. What if you want to apply certain styling to some of the div elements but not all? This is where class comes in:

<div class="red">i am red</div>
<div class="red">i am red</div>
<div class="red">i am red</div>
<div>we are not</div>
<div>we are not</div>
<div>we are not</div>

bit of a lame example, but i hopeful it shows the point

the issue you run into is something you see a lot with new programmers, you are taught a concept which at first doesn’t seem to make much sense. Why would you? Hopefully in the future, you will build a webpage/website and see how useful classes are


#3

This actually does make sense…and I can now see how useful they are as I progress through the classes. Thanks!


#4

now this of course does not only apply div elements, but all other elements as well

keep this in mind for the further as well, sometimes a lesson just teaches a concept first, so its value might not be obvious at first sight