Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

HTML Introduction to HTML and CSS 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!