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
Derick Brown
7,361 PointsUse of inline CSS styling
I'm pretty good with CSS and it's best practices but there's always been one thing I was curious about.
Let's say you had 4 DIVs that all share the same class "section".
<div class="section">This is Section 1</div>
<div class="section">This is Section 2</div>
<div class="section">This is Section 3</div>
<div class="section">This is Section 4</div>
Here is the CSS for the sections
.section {
padding: 30px 0;
background-color: #fff;
}
The above code gives you 4 DIVs with 30px of padding on the top and bottom along with a white background.
Lets say you wanted the second DIV to have a black background but still have the same 30px padding. This is the ONLY DIV on this page (possibly the whole site) that will have a black background as a section.
Would this be an appropriate time to use the "style" attribute? Or would you give the DIV an ID or class and change it via your external CSS file?
I never understood what to do in these situations haha. Create a few more lines in my CSS for this ONE DIV or just use the style attribute and change it real quick?
1 Answer
james south
Front End Web Development Techdegree Graduate 33,271 Pointspart of the rationale for using an external stylesheet is maintainability through separation of content and styling. regardless of how quick and easy it might be to just slip a style tag in your HTML, it would violate this practice. in this case you don't need another class to style the black div, you could use the nth-child or nth-of-type selectors.
Derick Brown
7,361 PointsDerick Brown
7,361 PointsAh okay cool! Thanks for the answer! This was always something I was curious about haha thanks for the insight