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
Matthew Smart
12,567 PointsChange the background colour on the body for each section (multiple body background colors)
Basically i have a website that i want the background colour to be different as you scroll down. For example(not me real code but for an example):
<div id="wrapper">
<div id="header">
<!-- (This is 400px height) -->
</div>
<div id="content">
<!-- (This is 400px height) -->
</div>
<div id="footer">
<!-- (This is 400px height) -->
</div>
</div>
Now imagine that is my website, i want the bodys background to be black on the left and right side of the #header Div container.
I want the bodys background to be white on the right and left side of the #content Div contrainer.
I want the bodys background to be grey on the right and left side of the #footer Div container.
Can i do this without making a background image, as this would not change if the content changed in the page. Meaning if i added content i would have to redo the image to match it. This cannot be the only way.
Can someone help please? :)
2 Answers
James Christensen
24,170 PointsYou could nest your divs in divs with different background colors.
Example below:
<style>
body {margin:0;padding:0;}
.background {margin:0;padding:0;}
.bg-color1 {background: red;}
.bg-color2 {background: blue;}
.content {width:80%;margin:0 auto; background:white;}
.footer {width:80%;margin:0 auto; background:white;}
</style>
<div class="background bg-color1">
<div id="content">
<!-- (This is 400px height) -->
</div>
</div>
<div class="background bg-color2">
<div id="footer">
<!-- (This is 400px height) -->
</div>
</div>
Matthew Smart
12,567 PointsThanks for your answer, i actually resulted to doing this shortly after posting my question but just realised you answered. :)