Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

two divs side by side, one 100% width others width depended on content

Tags:

html

css

I want to place two DIV tags side by side without using fixed width. The first div expands to its content and the second div should fill the remaining space. Also the div must NOT sit on top of the other div, because they have a transparent background image so if they intersect it's noticeable. I tried all possibilities that i could think off but couldn't find a solution using DIV tags.

I can do this using a TABLE, but is it possible to do it using DIV's? Or is this one more thing DIV's can't do?

Here's the code:

            #right{               background: green;                    width: 100%;             }             #left {               margin-top: 5px; /* to test if they intersect*/               background: red;             }               #container {                width: 800px;             }             <div id="container">                <div id="left"> This div is as big as it's content</div>                <div id="right"> rest of space</div>             </div>  

Thanks for the replies!

like image 802
blejzz Avatar asked Jul 24 '11 19:07

blejzz


People also ask

Is a div 100% width by default?

auto automatically computes the width such that the total width of the div fits the parent, but setting 100% will force the content alone to 100%, meaning the padding etc. will stick out of the div, making it larger than the parent. so setting the 'width' to 'auto' would be better? Yes, but that's the default anyway.

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 you make a div 100 width?

What you could do is set your div to be position: absolute so your div is independent of the rest of the layout. Then say width: 100% to have it fill the screen width. Now just use margin-left: 30px (or whatever px you need) and you should be done.

How do I change the width of a div according to content?

You could try using float:left; or display:inline-block; . Both of these will change the element's behaviour from defaulting to 100% width to defaulting to the natural width of its contents. However, note that they'll also both have an impact on the layout of the surrounding elements as well.


1 Answers

See: http://jsfiddle.net/kGpdM/

#left {     background: #aaa;     float: left } #right {     background: cyan;     overflow: hidden } 

This works in all modern browsers and IE7+.

The left column will be exactly as wide as the content inside it. The right column will take the remaining space.

The overflow: hidden "trick" behind this answer is explained here.

like image 174
thirtydot Avatar answered Sep 20 '22 18:09

thirtydot