Forum Moderators: not2easy

Message Too Old, No Replies

Floated Lists and the box that won't inflate to contain them

         

mdholt

4:00 pm on Apr 11, 2007 (gmt 0)

10+ Year Member



Here's the code:

<style>

body,ul,li {
margin: 0px;
padding: 0px;
}

div#box {
margin: auto;
width: 500px;
background: green;
border: 1px solid #000;
}

ul {
margin: auto;
list-style-type: none;
width: 450px;
display: inline;
background: red;
border: 1px solid #000;
}

li {
float: left;
display: block;
width: 210px;
height: 300px;
margin: 10px;
background: #ddd;
}

</style>

<div id="box">
<ul>
<li>this little piggy went to</li>
<li>the market</li>
<li>urban outfitters</li>
<li>dubai</li>
<li>six arms</li>
<li>the arboretum</li>
</ul>
</div>

- I know it is not correct to expect div#box to extend down to contain this floated list. -
But I really want it to! haha. Is there any way to get div#box to contain the floated list within it?

Thanks for any help!

DanA

5:02 pm on Apr 11, 2007 (gmt 0)

10+ Year Member



You can add overflow:auto; or overflow:hidden; in #box for modern browsers or you can clear your floats.

jetboy

5:03 pm on Apr 11, 2007 (gmt 0)

10+ Year Member



Float the <ul> left instead of making it display inline. Then clear the floated elements. My preferred method would be a <br /> with styles of clear: left; and height: 0; applied to it, placed directly after the closing </ul>.

There are other methods of clearing floats, but this one's rock solid on any browsers you're likely to come across in the wild.

mdholt

5:26 pm on Apr 11, 2007 (gmt 0)

10+ Year Member



Oh thank god-

That is the simple solution that I knew was out there.

Thank you very much for your help!

Long live good ole' <br />

DanA

6:00 pm on Apr 11, 2007 (gmt 0)

10+ Year Member



but this one's rock solid on any browsers

It doesn't always work with IE 7.

[edited by: DanA at 6:01 pm (utc) on April 11, 2007]

Robin_reala

6:41 pm on Apr 11, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Dan - got an example? Although it’s not particularly respectful of the separation of content and style, I can’t think of a situation where a <br style="clear:both" /> won’t work in IE7 (or any other post-NN4 browser for that matter).

DanA

8:18 pm on Apr 11, 2007 (gmt 0)

10+ Year Member



Helping someone at sitepoint, I had to replace it by another method for ie 7 as it was the only browser in which it didn't work for clearing a float in one column. I kept the source code (20k) but never found time to make a test case.

SuzyUK

8:51 pm on Apr 11, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



DanA I think you're right, but imho, it's not just IE7 it's all of IE!

I have a feeling it's maybe simple over-reliance on haslayout, mostly in these clear float situations the "wrapper" or float container has a width - if it does then clearance is not provided by the <br> but by the layout on the container.

you can try a simple example by removing the width off the container then try clearing the floats with a <br>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>Untitled</title>
<style type="text/css" media="screen">
html, body {margin: 0px; padding: 0px;}
#wrapper {background: green;}
#left {float: left; width: 300px; background: #f00;}
#right {float: right; width: 300px; background: #dad;}
.clear {clear: both;}
</style>
</head>
<div id="wrapper">
<div id="left">left content<br />left content<br />left content</div>
<div id="right">right content<br />right content<br />right content</div>
<br class="clear" />
</div>
</body>
</html>

if you have any real content after the br then you're good to go, but the br alone won't do it.

[edited by: SuzyUK at 8:57 pm (utc) on April 11, 2007]

DanA

4:08 pm on Apr 13, 2007 (gmt 0)

10+ Year Member



I have a feeling it's maybe simple over-reliance on haslayout

You are right. I did some testing with the layout which wasn't cleared by <br style="clear:both"> in IE 7. It was cleared in IE 6 but the reason is not clear (removing padding or adding a comment and the floats weren't cleared).
In certain conditions, <div style="clear:both"></div> worked in IE 7 but I removed a few lines of code and it stopped working.
The clearfix method worked in any browser : in Mozilla with the .clearfix:after rule and in Internet Explorer 6 and 7 as .clearfix{display:inline-block;}
.clearfix{display: block; } set hasLayout