views:

609

answers:

4

I'm looking for a way to force floated or absolutely positioned elements to stay in the flow in css. I'm pretty much thinking css is stupid for not having something like flow:on flow:off to keep it in the flow or take it out.

The issue is that I want to have a div element with a variable height, I have a floated image on the left in the div, and I want the div to be at least the height of the picture. I also want it to be at least big enough to hold all the text that IS in the flow (this obviously isn't a problem).

I need the picture to be able to vary in size. I am currently using a jQuery solution, but its acting up. Since I don't feel like debugging, and I feel like there should be some kind of CSS solution, i'm asking.

Anyone know how I can do this?

A: 

A hack that may work in your situation is to add another element inside your div after the rest of the content that has the CSS clear property set to "both" (or left, since your image is on the left). eg:

<br style="clear: both" />

This will force the element below the floated elements, which will stretch the containing div.

Laurence Gonsalves
This causes problems when the element is already positioned next to floating content. It doesn't solve the problem for me.
B T
A: 

Instead of using a new element to clear the div at the end, you can add this onto the absolute div css;

overflow: auto;

Obviously IE likes to play differently so you need to supply a width to it too. I am assuming the absolute div has a set width... so you can just set it to that width.

.abs-div {
    position: absolute;
    overflow: auto;
    width: 160px; /* Replace with your width */
}
danrichardson
A: 

YOu can also set a min-height to the div to get expand. Then give <br style="clear: both" />, so that the next div will come down.

webdevelopertut

Webdevelopertut
A: 

I usually go with overflow: hidden or overflow: auto.

Here are three solutions.

rpflo
Perfect. Works exactly like I expect the hypothetical "flow:on" to work. I'd use overflow:auto, because then it won't cut off content I *want* to be outside of the box (if that ever comes up).
B T