tags:

views:

1283

answers:

2

I suppose a DIV with "clear:both" style can make its parent containing DIV doesn't wrap, but below HTML seems not work in that way.

If the browser window is narrow, the second DIV "OK OK" still wraps to next line.

 <div style="overflow: hidden;">
  <div style="float: left; overflow: hidden; white-space: nowrap">
                         Hello world 1 Hello world 2 Hello world 3
                    </div>
  <div style="float: left; overflow: hidden; white-space: nowrap">
    OK OK OK OK OK OK OK OK
                    </div>
  <div style="clear: both;">
  </div>
 </div>

Did I miss something?

A: 

Because the two inner divs are floated left, they will wrap when the browser window becomes too narrow. I would suggest that you specify a width on the outer div (the one you have overflow: hidden on). That way the two inner divs can float, but they won't wrap when the browser shrinks.

    <div style="overflow: hidden; width: 600px;">
            <div style="float: left; overflow: hidden; white-space: nowrap">
                     Hello world 1 Hello world 2 Hello world 3
                </div>
            <div style="float: left; overflow: hidden; white-space: nowrap">
                            OK OK OK OK OK OK OK OK
                </div>
            <div style="clear: both;">
            </div>
    </div>

If you want more control, you might have to implement the min-width hack using expressions in IE; min-width: works in FF.

Cory Larson
there's always a chance the divs will wrap as long as no width for the inner divs is specified. If you don't specify a width a floated div will go as wide as it needs to in order to hold its content, up to a maximum of the width of its container.
wheresrhys
+5  A: 

I think you might be misunderstanding the "clear" property. It doesn't control wrapping specifically, it controls the placement of floating elements that come afterwards. There is nothing in your html above that is preventing the second div from flowing to the next line.

Here is a pretty good rundown on clearing elements: http://www.builderau.com.au/program/css/print.htm?TYPE=story&amp;AT=339278136-339028392t-320002011c

In order to make the layout not flow, you will likely have to specify a hard width on your container div.

womp