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!
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With