CSS CSS Basics (2014) Basic Selectors Reusing Classes

Why can't I copy "border-top: 2px solid lightgrey" to "primary-content" and "footer"? Why should I create a new class?

Why can't I copy "border-top: 2px solid lightgrey" to "primary-content" and "footer"? Why should I create a new class?

2 Answers

jared eiseman
jared eiseman
29,023 Points

The short answer would be re-usability. If you ever want to give another element that same top border, all you have to do is add the class to the html element, bing bang boom, done. Just good practice, not a requirement. You can totally do what you suggested, just might make things simpler in the future if you have it separated out.

Szymon Madeja
Szymon Madeja
707 Points

Doesn't this break the separation of structure and presentation, though? Shouldn't classes be semantic and define what elements are, rather than what they should look like? If we create classes that are only used to apply certain styles, we're moving presentation back into HTML. I don't think it's good practice.

Hi Yawei, I second what Jared said. You'll be repeating a lot of the same css code over and over again when you don't have to. It's far easier to add the same class name to elements in your html. :)