Div alignment centering

Hi Guys,

I am not sure why I am still having difficulty trying to get a 1000px div to center on a page.

In fact, it's exactly how the navigation banner or footer works on this experts exchange website.

The 'center' div with width 1000px will remain centered on the page but the background extends all the way to the end on each side.

Thanks again for your kind help.
CFbubuAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Chris StanyonConnect With a Mentor Commented:
Seeing a snippet of code in isolation rarely tells the full story. If you don't set a width on a block level element, such as a DIV then it will automatically stretch to the full width of the parent. If it has no explicit parent, then the window becomes the parent, so if you have only this in your page:

<div style="background-color:red">
     <div style="width:1000px; margin: 0px auto;background-color:blue"></div>
</div>

Open in new window

The red DIV will fill the page and the blue will sit centred inside (assuming your screen is wider than 1000px)
0
 
Chris StanyonConnect With a Mentor Commented:
To centre a DIV horizontally within it's parent, give it a width and set the left and right margins to auto:

<div style="background-color:blue">
     <div style="width:1000px; margin: 0px auto;"></div>
</div>

Open in new window

0
 
GaryCommented:
So is the question about centering (as Chris has answered above) or about how to have the background extend?
0
Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

 
CFbubuAuthor Commented:
Hi,

Thanks for your quick reply. Unfortunately, when I used your code, I do get a blue block centered, but the sides do not extend to the edges of the screen...(See picture below)

diagram1
What I trying to do is like how the navigation bar header and footer works on this website. That is the content stays centered but the background extends all the way to the side edges.(See pciture below)

diagram2
Thanks again!
0
 
CFbubuAuthor Commented:
Hey GaryC123,

Good morning to you! Nice to see you around :)

No...unfortunately, the solution submitted by Chris did not work. :(
0
 
GaryCommented:
Sheesh Chris is on the ball - didn't even have time to read the response!
0
 
CFbubuAuthor Commented:
I am sorry Chris. Let me check the code again and get back to you.

I do appreciate your help.
0
 
Chris StanyonCommented:
Here it is in operation, so I know my code works :)

http://jsfiddle.net/ChrisStanyon/78v9z/
0
 
CFbubuAuthor Commented:
you are right...its something on my page that is messing it up....sorry. I am still trying to figure out what's causing it.

Thanks for jumping in so quick to help me .
0
All Courses

From novice to tech pro — start learning today.