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
Terrance Corley
11,990 PointsShould a css class or an element selector be used in this case?
Say I have a green border color that is displayed multiple times on my page. Should I create a class with a border-color property set to green and then apply that class to any html element that needs the green border, OR, should I just select each html element using css selectors in one rule and give them all the green border color?
I basically want to know if there is a best practice to this. Should I create a class and include that a bunch of times in the markup, or should I just target all the needed elements in an external css file which would make the css file longer but keep all the "styling" in the css file?
Ex:
<header class="border">Header</header>
<footer class="border">Footer</footer>
.border {
border-color: green;
}
OR
<header>Header</header>
<footer>Footer</footer>
header,
footer {
border-color: green;
}
1 Answer
Abraham Juliot
47,353 PointsHi Terrance,
Use a class when you want your styles to only be applied to the matching elements containing the class. Select the elements directly when you want to apply styles to all matching elements that may appear on the page.
In general, elements should be selected for base styles (default styles on elements).
In short, use element selectors for default styles, and use class selectors for custom styles that supersede default styles.
Terrance Corley
11,990 PointsTerrance Corley
11,990 PointsWow, I feel like such a beginner. Your answer made immediate sense and I can't believe I didn't realize that on my own haha. Thanks a bunch, Abraham!