Center main div wrap but use different bg colors

Hi people,

I’ll try to explain this as best i can…

Basically what i want to do is have my content and main div’s of my layout center aligned with different background colors behind the main layout wrapper.

Normally this is no problem:

#main_wrap {margin: 0 auto;} and then set a background color or image for the body etc.

The thing is I want to have different background colors for certain parts of the page that fill the complete browser window.

So for example if it was just one color it would not be a problem as i would assign the color to the body tag in css. However Because i want to use different background colors further down the page this complicates things.

Now I also know I can create like a 1px width image in photoshop with the different colors and then assign that to the body as a background image and repeat X, however that would mean that i would have to set specific heights for each of the main div’s (which i dont really wanna do because the image slider that i’m using is responsive and i think it wont shrink hand in hand with the rest of the layout when adjusting the size of the window).

Im sure what I have done so far is not the correct way of going about this and I’m almost certain it will cause problems later on in the design process.

This is the code for my own work-around to this design issue:

<style type="text/css">
body{
	margin:0px;
	padding:0px;
}
#banner_bg{
	background-color:#FFF;
}
#banner_wrap{
	height:127px;
	margin: 0 auto;
	width: 1187px;
}

#slider_wrap{
	margin:0 auto;
	background-color: #FFF;
	width: 1187px;
}

#info_area_bg{
	background-color:#f8f8f8;
}

#info_area_wrap{
	height:185px;
	width:1187px;
	margin: 0 auto;
	padding-top: 20px;
}

#main_area_bg{
	background-color:#f8f8f8;
}

#main_area_wrap{
	height:400px;
	width:1187px;
	margin: 0 auto;
	
}
#footer_top_area_wrap{
	height:330px;
	width: 1187px;
	margin: 0 auto;
}

#footer_bottom_bg{
	background-color:#666666;
	width: 100%;
}

#footer_bottom_area_wrap{
	height:45px;
	margin: 0 auto;
	width: 1187px;
}

#footer_bg{
	background-color:#333;
}
</style>
</head>

<body>

<div id="banner_bg">
<div id="banner_wrap">BANNER</div><!--end of banner wrap-->
</div><!--end of banner bg -->

<div id="slider_wrap">SLIDER AREA</div><!--end of slider wrap-->

<div id="info_area_bg">
<div id="info_area_wrap">INFO AREA</div><!--end of info area wrap-->
</div><!--end of info area bg-->

<div id="main_area_bg">
<div id="main_area_wrap">MAIN AREA</div><!--end of main area wrap-->
</div><!--end of main area bg -->

<div id="footer_bg">
<div id="footer_top_area_wrap">FOOTER TOP</div><!--end of footer top area wrap-->
</div><!-- end of footer bg-->

<div id="footer_bottom_bg">
<div id="footer_bottom_area_wrap">FOOTER BOTTOM</div><!--end of footer bottom area wrap-->
</div><!--end of footer bottom bg-->
</body>

*Notice how many times I had to use the margin: 0 auto line!

Also I know this will cause issues and is not correct because when i re-size the browser the background colors dont fit the width of the browser window.

I have attached an image to this post of how i want the actual layout to look like and also help you guys understand what it is that i’m trying to do.

I know this is really basic stuff… and to be honest I’m a little apprehensive about posting this thread… I just seem to be banging my head against the wall with real simple stuff lately - Ive lost my mojo!

Please go easy on me guy’s n girls and many many thanks to anyone who can help me out of this spot!

All the best, thanks.

Hi, Stuck_Again. Your attachment is pending approval.

What are you working around?

Your example code is missing some basic structural tags: <!doctype html>, <html>, <head> and </html>

Otherwise there’s nothing wrong with your code that replacing all of those IDs with classes wouldn’t help.


<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>template</title>
<!--
http://www.sitepoint.com/forums/showthread.php?1207712-Center-main-div-wrap-but-use-different-bg-colors
2014.05.06 05:15
Stuck_Again

-->
    <style type="text/css">
*, *:before, *:after {
    -webkit-box-sizing:border-box;
    -moz-box-sizing:border-box;
    box-sizing:border-box;
}

body {
    margin:0px;
    padding:0px;
}
#banner_wrap,
#slider_wrap,
#info_area_wrap,
#main_area_wrap,
#footer_top_area_wrap,
#footer_bottom_area_wrap {
    width:1187px;
    margin:0 auto;
}

#banner_bg {
    background-color:#fed;
}
#banner_wrap {
    outline:1px solid #fb8;
    height:127px;
}
#slider_wrap {
    background-color:#def;
}
#info_area_bg {
    background-color:#fdd;
}
#info_area_wrap {
    outline:1px solid #f88;
    height:185px;
    padding-top:20px;
}
#main_area_bg {
    background-color:#ddf;
}
#main_area_wrap {
    outline:1px solid #88f;
    height:400px;
}
#footer_bg {
    background-color:#fdf;
}
#footer_top_area_wrap {
    outline:1px solid #f8f;
    height:330px;
}
#footer_bottom_bg {
    background-color:#dfd;
}
#footer_bottom_area_wrap {
    outline:1px solid #8f8;
    height:45px;
}
</style>
</head>
<body>

<div id="banner_bg">
    <div id="banner_wrap">BANNER</div> <!--end of banner wrap-->
</div> <!--end of banner bg -->
<div id="slider_wrap">SLIDER AREA</div> <!--end of slider wrap-->
<div id="info_area_bg">
    <div id="info_area_wrap">INFO AREA</div> <!--end of info area wrap-->
</div> <!--end of info area bg-->
<div id="main_area_bg">
    <div id="main_area_wrap">MAIN AREA</div> <!--end of main area wrap-->
</div> <!--end of main area bg -->
<div id="footer_bg">
    <div id="footer_top_area_wrap">FOOTER TOP</div> <!--end of footer top area wrap-->
</div> <!-- end of footer bg-->
<div id="footer_bottom_bg">
    <div id="footer_bottom_area_wrap">FOOTER BOTTOM</div> <!--end of footer bottom area wrap-->
</div> <!--end of footer bottom bg-->

</body>
</html>

You might want to take a look at a simpler example in this post. It includes a clearfix to prevent text margins from escaping their containers. You’ll likely need that, too.

Thanks for your reply Ronpat,

Yeah… i just left the doctype etc out as I was certain that that part of my code was correct.

Thanks for the link, I will check it out in a sec.

As for the ID’s… force of habit im afraid!

I know i overuse the ID tag instead of Classes but it was something i just knocked up quickly to give you guys the general idea.

Since posting my original thread I’ve been thinking that maybe a background image might just do the job after all, although ive not tested it yet.

What I was thinking is, that if you look at the colors, the background color for the first two sections of the layout is white (including the slider area) so maybe i should create a bg image containing the other colors and just attach it horizontally to everything under the slider area div.

Too tied to try it right now… been a long day, but i will let you know if it works.

But of course, any feedback or variations on how to do this would be most welcome.

Thanks guys… I love this forum!

If you mean you want one background image with multi colours then I dont think that will work because you will have to set the height for the containers and is too rigid to work for a fluid design or indeed if someone changes there text size.

The method Ron suggests is the safest method to use even if it uses a little more mark up.

You can do it more simply for IE9+ only by using box-shadow to make the full width strips. Here’s an example.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
<style type="text/css">
#outer {
	max-width:960px;
	margin:auto;
	background:#f2f2f2;
}
.stripe {
	background:red;
	padding:1em 0;
	color:#fff;
	/* 960px matches the max-width of the element - it can't be bigger than the element or the shadow moves away */
	-webkit-box-shadow: 960px 0px red, -900px 0px 0px red;
	-moz-box-shadow:   960px 0px 0px red, -900px 0px 0px red;
	box-shadow:        960px 0px 0px red, -900px 0px 0px red;
}
.stripe2{
	background:blue;
	padding:1em 0;
	color:#fff;
	/* 960px matches the max-width of the element - it can't be bigger than the element or the shadow moves away */
	-webkit-box-shadow: 960px 0px blue, -900px 0px 0px blue;
	-moz-box-shadow:   960px 0px 0px blue, -900px 0px 0px blue;
	box-shadow:        960px 0px 0px blue, -900px 0px 0px blue;
}
p { margin:1em }
</style>
</head>

<body>
<div id="outer">
		<h1>Test IE9+ wide background strip no extra mark up</h1>
		<div class="stripe">
				<p>lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test </p>
				<p>lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test </p>
				<p>lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test </p>
		</div>
		<div class="stripe2">
				<p>lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test </p>
				<p>lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test </p>
		</div>
			<div class="stripe">
				<p>lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test </p>
				<p>lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test </p>
				<p>lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test </p>
		</div>
		<div class="stripe2">
				<p>lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test </p>
				<p>lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test lorem ipsum test </p>
		</div>
</div>
</body>
</html>


Thanks for all your help guys. I played around with your suggestions and tried some other things on my own and now everything seems to be working nicely.

Thank you so much for all your help. 10/10

We’d enjoy seeing how you accomplished your goal. In the spirit of “each one teach one”, feel free to add a link to your site to this thread once your site is “presentable”. :stuck_out_tongue:

Cheers

Will do!

Many thanks again