Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS side by side div's auto equal widths

Tags:

html

css

<div id="wrapper" style="width:90%;height:100px;background-color:Gray;">     <div id="one" style="height:100px;background-color:Green;float:left;"></div>     <div id="two" style="height:100px;background-color:blue;float:left;"></div>     <div id="three" style="height:100px;background-color:Red;float:left;"></div> </div> 

I have a parent div which will contain 2 or 3 child divs. I want child divs to take equal widths automatically.

Thank You

like image 913
Faizal Balsania Avatar asked Mar 18 '11 11:03

Faizal Balsania


People also ask

How do I keep two side by side div elements the same width?

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 align items side by side in 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. float:right; This property is used for those elements(div) that will float on right side.

How do I set auto width in CSS?

Using width, max-width and margin: auto; Then, you can set the margins to auto, to horizontally center the element within its container. The element will take up the specified width, and the remaining space will be split equally between the two margins: This <div> element has a width of 500px, and margin set to auto.

How do I make two divs display on the same line?

You can use display: inline to put the two div elements inline. Explanation: div elements are block elements, so their display style is usually display: block . You can wrap both the div elements in a span tag. Explanation: span works the same way as the div , to organize and group elements.


1 Answers

It's not impossible. It's not even particularly hard, with the use of display: table.

This solution will work in modern browsers. It won't work in IE7.

http://jsfiddle.net/g4dGz/ (three divs)
http://jsfiddle.net/g4dGz/1/ (two divs)

CSS:

#wrapper {     display: table;     table-layout: fixed;      width:90%;     height:100px;     background-color:Gray; } #wrapper div {     display: table-cell;     height:100px; } 

HTML:

<div id="wrapper">     <div id="one">one one one one one one one one one one one one one one one one one one one one one</div>     <div id="two">two two two two two two</div>     <div id="three">three</div> </div> 
like image 59
thirtydot Avatar answered Sep 17 '22 18:09

thirtydot