views:

192

answers:

2

When I have three divs that all have float left I want the sections to expand or contract based on how long the data inside them is.

For instance, if the 1st address is very long I want the 1st box to expand and push the 2nd one over. In Firefox it is doing this but in Internet Explorer it requires a width to look right. In IE7 it doesn't display right at all - each div has a table with two columns and in IE7 it shows the 2nd column way over to the far side of the page instead of snug with the 1st column despite setting align=left on the 2nd column and setting a small width on the 1st column. I have the doc type specified in the master page and I've tried adding clear:both with no luck.

Why does the width totally change how the float section is displayed in IE and how can I fix this? The page must look nice in IE7.

.FloatingSection
{
    float:left;
    padding-bottom:10px;
    padding-right:10px;
}

<div id="FirstPerson" class="FloatingSection">
</div>
<div id="SecondPerson" class="FloatingSection">
</div>
<div id="ThirdPerson" class="FloatingSection">
</div>   

I noticed that in IE8 this looks just fine.

+1  A: 

You should almost always include an explicit width when you're floating elements.

If you don't, browsers have to guess what you mean. In this case, Firefox is guessing better than IE, but you shouldn't make them guess. Be explicit where you can.

edit: If you want all three boxes to expand with the content, I'd suggest setting percentage-based widths. In general, you're going to want to look up techniques for fluid column layouts.

Triptych
An explicit width would defeat what I am trying to do though. I want the width to change dynamically based on content rather than be fixed.
hyprsleepy
After looking at this some more I've decided I can either setup browser hacks to act differently for IE 7 vs. IE 8 and Firefox or just deal with the fact that it will never look right in IE 7 and go fixed width. I decided on the latter.
hyprsleepy
A: 

Have you considered display:inline-block instead of floating? I don't think you can do what you want with floats AND support IE without using some JS to explicitly define width on each floated element.

Another note, you have to declare a second separate rule for block level elements in IE:

HTML:

<div class="foo">test</div><div class="foo">bar</div>

CSS:

.foo { display:inline-block; }
.foo { display:inline; } /* Feed this to IE below 9 only with a CC. Don't feed it to modern browsers */
meder