tags:

views:

1460

answers:

3

Hello,

I have a small problem. I am trying to align two divs side by side using CSS, however, I would like the center div to be positioned horizontally central in the page, I achieved this by using:

#page-wrap { margin 0 auto; }

Thats worked fine. The second div I would like positioned to the left side of the central page wrap but I can't manage to do this using floats although I'm sure it is possible.

Maybe its best to show the example of what I am describing:

Layout Problem

I would like to push the red div up alongside the white div.

Here is my current CSS concerning these two divs, sidebar being the red div and page-wrap being the white div:

#sidebar    {
    width: 200px;
    height: 400px;
    background: red;
    float: left;
    }

#page-wrap  {
    margin: 0 auto;
    width: 600px;
    background: #ffffff;
    height: 400px;
    }

Any help would be appreciated.

A: 

The easiest method would be to wrap them both in a container div and apply margin: 0 auto; to the container. This will center both the #page-wrap and the #sidebar divs on the page. However, if you want that off-center look, you could then shift the container 200px to the left, to account for the width of the #sidebar div.

Michael Herold
This sounds like a plan. How would I shift the whole container left by 200px? Thanks for the quick reply.
Ronnie
Nick's example does what I was speaking of. Up-vote for Nick!
Michael Herold
+5  A: 

If you wrapped your divs, like this:

<div id="main">
  <div id="sidebar"></div>
  <div id="page-wrap"></div>
</div>

You could use this styling:

#main { 
    width: 800px;
    margin: 0 auto;
}
#sidebar    {
    width: 200px;
    height: 400px;
    background: red;
    float: left;
}

#page-wrap  {
    width: 600px;
    background: #ffffff;
    height: 400px;
    margin-left: 200px;
}

This is a slightly different look though, so I'm not sure it's what you're after. This would center all 800px as a unit, not the 600px centered with the 200px on the left side. The basic approach is your sidebar floats left, but inside the main div, and the #page-wrap has the width of your sidebar as it's left margin to move that far over.

Update based on comments: For this off-centered look, you can do this:

<div id="page-wrap">
  <div id="sidebar"></div>
</div>

With this styling:

#sidebar    {
    position: absolute;
    left: -200px;
    width: 200px;
    height: 400px;
    background: red;    
}

#page-wrap  {
    position: relative;
    width: 600px;
    background: #ffffff;
    height: 400px;
    margin: 0 auto;
}
Nick Craver
Wouldn't this center everything? I want the white div to remain centered and the nav bar to be pushed alongside it. Sorry, didn't explain that well in the original question.
Ronnie
Sorry, didn't see the last paragraph you wrote. Yes, I would like to center just the white div and push the red div alongside it. I would do this by using a negative margin then?
Ronnie
@Ronnie - Added that option as well, this just positions the sidebar to the left 200px to match it's width, making the parent container `relative` and the sidebar itself `absolute` is what allows this to occur.
Nick Craver
Brilliant, thanks Nick, works a treat.
Ronnie
@Ronnie - Yep, you can see a working example of the above here: http://jsfiddle.net/jjRyD/
Nick Craver
the sidebar starts to disappear if the window is smaller than 1000px (600px + 200px for each side), but the content uses only 600px+200px. Is it possible to make it not be centered anymore when the window is between 800px and 1000px? Something like a minimum value for the left margin...
dbarbosa
I got it from buildinternet.com/2009/10/purely-css-faking-minimum-margins (alternate method): Put everything inside a `<div id="wrapper">` and then use: `#wrapper{ text-align:center; padding:0 200px; }`. However, this seems to work only for a minimum left margin... I want to have the sidebar on right.
dbarbosa
+1  A: 

It's also possible to to do this without the wrapper - div#main. You can center the #page-wrap using the margin: 0 auto; method and then use the left:-n; method to position the #sidebar and adding the width of #page-wrap.

body { background: black; }
#sidebar    {
    position: absolute;
    left: 50%;
    width: 200px;
    height: 400px;
    background: red;
    margin-left: -230px;
    }

#page-wrap  {
    width: 60px;
    background: #fff;
    height: 400px;
    margin: 0 auto;
    }

However, the sidebar would disappear beyond the browser viewport if the window was smaller than the content.

Nick's second answer is best though, because it's also more maintainable as you don't have to adjust #sidebar if you want to resize #page-wrap.

Mat Harden