HTML Introduction to HTML and CSS Make It Beautiful With CSS Select and Style by Class

Oscar Blanco
PRO
Oscar Blanco
Pro Student 990 Points

Why couldnt we style the <h2> Background </h2> without adding the class=card-title, to it like in the last lesson?

Why did we have to add the class attribute? PLEASE HELP!

1 Answer

Ryan Groom
Ryan Groom
9,568 Points

Oscar Blanco If by styling the <h2> background you mean inline like so: <h2 style="background: red">Heading</h2> then you are more than welcome to do this, but there are many reasons that adding a class would be more beneficial. Imagine you have multiple items on your site that need styled the same way, but the styles include a lot more than just background... for example <h2 style="background:red; font-size: 20px; font-family: Open Sans; font-weight: bold;">Heading</h2>. Now imagine having to type this out for every single element that you wanted to be styled this way. It would be much easier to just add ' class="card-title" ' to each of the elements that you wanted to have all these styles for and then declare the styles inside of a CSS file. There are also other reasons to use classes, but they are on a little more advanced of a topic. If you're interested then you can look into "CSS selector specificity". I hope this helps, if you need any clarification let me know!

Oscar Blanco
Oscar Blanco
Pro Student 990 Points

Wow, got it! This totally makes sense, thank you, Ryan!