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

CSS

Backgrounds Question

Hey, everybody. How's it going? I've gone through a lot of the CSS track and so far so good, generally speaking. But I've been wondering recently how some well done websites I see have these nice margins (sometimes with colors) running up and down the sides of their sites, the main body of the site with all the interactive stuff in what looks like a narrower center div on top of another div with the color -it looks to me like it might be a z-index thing with two backgrounds, one narrower on top of the other one. But I'm not sure. Does anybody know? What have I missed. The Boston Herald homepage has this type of design if anybody wants to look. Thanks. -Demian

2 Answers

Dead simple Demian Arend - don't over complicate things with z-index.

All it is is this. A div with a width of say 70%. This is then given a margin of 0 15%. 15% either side of the 70%, that'll plonk it in the middle of your page.

For the background, there's two ways to go. If the background/sidebars/borders/margins either side of the main content field is to have active content like login, cart whatever, then you'll need to make two 15% width divs. One before the main container, one after. No margins as 15 + 15 + 70 =100. Float them all to the left and you'll have three divs going 15%, 70%, 15%.

The next step is the same for both. Background-image. If you just want a background and not active areas either side of your content, then use background-image to insert an image that'll act as the background. Difficult one to do, go for a pattern or an image with a height of about 1440px. This is so you're not left with issues either stretching, repeating or centering the image with space left above and below. If it's just the background to the whole page, then grab the body selector in css and apply it there like this:

body{
    background-image:

This same process is applied to any div you want a background image on. Just if you want to put stuff in your sidebars you need to do a bit more work.

Solid answer, but be careful with percentage-based widths. You'll probably want to either set a max-width on your content container so that is doesn't just keep stretching forever or use a fixed width container (set with px or em) with margin: 0 auto to keep it centered until you hit a reasonable breakpoint.

Thanks, Scott and Mathew. Appreciate it. I will try this stuff. -Demian