Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to align 3 divs (left/center/right) inside another div?

I want to have 3 divs aligned inside a container div, something like this:

[[LEFT]       [CENTER]        [RIGHT]] 

Container div is 100% wide (no set width), and center div should remain in center after resizing the container.

So I set:

#container{width:100%;} #left{float:left;width:100px;} #right{float:right;width:100px;} #center{margin:0 auto;width:100px;} 

But it becomes:

[[LEFT]       [CENTER]              ]                               [RIGHT] 

Any tips?

like image 382
serg Avatar asked Apr 08 '10 21:04

serg


People also ask

How do I align 3 divs side by side?

Three or more different div can be put side-by-side using CSS. Use CSS property to set the height and width of div and use display property to place div in side-by-side format. float:left; This property is used for those elements(div) that will float on left side.

How do I align two divs side by side?

The most common way to place two divs side by side is by using inline-block css property. The inline-block property on the parent placed the two divs side by side and as this is inline-block the text-align feature worked here just like an inline element does.

How do I arrange divs horizontally?

To horizontally center a block element (like <div>), use margin: auto; Setting the width of the element will prevent it from stretching out to the edges of its container.


1 Answers

With that CSS, put your divs like so (floats first):

<div id="container">   <div id="left"></div>   <div id="right"></div>   <div id="center"></div> </div> 

P.S. You could also float right, then left, then center. The important thing is that the floats come before the "main" center section.

P.P.S. You often want last inside #container this snippet: <div style="clear:both;"></div> which will extend #container vertically to contain both side floats instead of taking its height only from #center and possibly allowing the sides to protrude out the bottom.

like image 195
dkamins Avatar answered Sep 20 '22 09:09

dkamins