Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Aligning two divs side-by-side [duplicate]

Tags:

css

css-float

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; } 

That's 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.

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; } 
like image 576
Ronnie Avatar asked Apr 26 '10 21:04

Ronnie


People also ask

How do I align two divs on the same line?

To display multiple div tags in the same line, we can use the float property in CSS styles. The float property takes left, right,none(default value) and inherit as values. The value left indicates the div tag will be made to float on the left and right to float the div tag to the right.

How do I keep two side by side divs the same height?

Basically what you do is make both divs/columns very tall by adding a padding-bottom: 100% and then "trick the browser" into thinking they aren't that tall using margin-bottom: -100% .

How do I keep my div on the same line?

Set size and make inline Because they're block elements, when reducing the size of Div one to make room for the other div, you're left with space next to Div one and Div two below Div one. To move the div up to the next line both div's need to have the inline-block display setting as shown below.

Can two divs overlap?

You can use the CSS position property in combination with the z-index property to overlay an individual div over another div element. The z-index property determines the stacking order for positioned elements (i.e. elements whose position value is one of absolute , fixed , or relative ).


2 Answers

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; } 
like image 196
Nick Craver Avatar answered Sep 17 '22 23:09

Nick Craver


I don't understand why Nick is using margin-left: 200px; instead off floating the other div to the left or right, I've just tweaked his markup, you can use float for both elements instead of using margin-left.

Demo

#main {     margin: auto;     width: 400px; }  #sidebar    {     width: 100px;     min-height: 400px;     background: red;     float: left; }  #page-wrap  {     width: 300px;     background: #0f0;     min-height: 400px;     float: left; }  .clear:after {     clear: both;     display: table;     content: ""; } 

Also, I've used .clear:after which am calling on the parent element, just to self clear the parent.

like image 32
Mr. Alien Avatar answered Sep 20 '22 23:09

Mr. Alien