Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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

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

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!

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