Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make outer div be automatically the same height as its floating content

I want the outer div, which is black to wrap its divs floating within it. I dont want to use style='height: 200px in the div with the outerdiv id as I want it to be automatically the height of its content (eg, the floating divs).

<div id='outerdiv' style='border: 1px solid black;background-color: black;'> <div style='width=300px;border: red 1px dashed;float: left;'>     <p>xxxxxxxxxxxxxxxxxxxxxxxxxxxxx</p> </div>  <div style='width=300px;border: red 1px dashed;float: right;'>     <p>zzzzzzzzzzzzzzzzzzzzzzzzzzzzz</p> </div> 

How to achieve this?

like image 275
Jase Whatson Avatar asked Apr 30 '09 00:04

Jase Whatson


People also ask

How do I keep my divs the same height?

The two or more different div of same height 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. The used display property are listed below: display:table; This property is used for elements (div) which behaves like table.

How do I Auto adjust height in css?

If height: auto; the element will automatically adjust its height to allow its content to be displayed correctly. If height is set to a numeric value (like pixels, (r)em, percentages) then if the content does not fit within the specified height, it will overflow.

How do I keep two side by side div elements 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% .


1 Answers

You can set the outerdiv's CSS to this

#outerdiv {     overflow: hidden; /* make sure this doesn't cause unexpected behaviour */ } 

You can also do this by adding an element at the end with clear: both. This can be added normally, with JS (not a good solution) or with :after CSS pseudo element (not widely supported in older IEs).

The problem is that containers won't naturally expand to include floated children. Be warned with using the first example, if you have any children elements outside the parent element, they will be hidden. You can also use 'auto' as the property value, but this will invoke scrollbars if any element appears outside.

You can also try floating the parent container, but depending on your design, this may be impossible/difficult.

like image 52
alex Avatar answered Oct 15 '22 12:10

alex