tags:

views:

49

answers:

3

In this example http://jsbin.com/inoka4 no width is defined for parent element

if i want to wrap red boxes in container border.

then we can make this in 5 ways

  • to giving float also to <div class="container">
  • overflow:hidden or overflow:auto
  • any clearfix hack to <div class="container clearfix">
  • Giving height to <div class="container">
  • adding one more html element (for example another div or <br >) after 2 boxes in <div class="container"> enter code hereand give clear:leftor:bothor:right` to that element

my question is any other option except float do not make any changes in <div class="container"> and inner boxes width. but if we use float:left or right to parent box then it's shrink the whole box and inner-boxes as well.

Why?

example link: http://jsbin.com/inoka4

Edit: My question is not about which method i should use, the question is why Float shrink the width

A: 

If you dont' use float on the container it's width is set to 100%. If you add a floating, it only takes the space it needs. In this case the width is calculated by the two divs inside.

To wrap the red boxes in the container border there is not other option except adding float to the container. The only other option would be to absolutely position all the elements but in this case you have to know the width and height of all elements in advance. So that really isn't an option.

So my advice is to use float on the container and add a clear: both on the element after the container.

Kau-Boy
"If you add a floating, it only takes the space it needs. In this case the width is calculated by the two divs inside." pls explain this more.
metal-gear-solid
Letting a elements float, you make it like an element with "display: inline-block". And as an element with "inline-block" has no fixed width (like an inline element) it automatically takes only the width an height it needs for its content.
Kau-Boy
ok then why it's leaving this space http://shup.com/Shup/382470/11061222727-My-Desktop.png
metal-gear-solid
Because you have set the width to 45% of the container DIV. The text in the div would take 50% of each side and that's how the width of the container div is calculated. But after that using 45% the text will wrap. The width of the container div stays the same width as it would have if the width of the inner divs are still 50%.
Kau-Boy
A: 

Your best bet is to always clear your floats. Just after you close the div with class .right, and just before you close the div with class .container, add a new div like this:

<div class="clear"></div>

.clear is just {clear:both;} in your stylesheet. That's what I use all day long, and works like a treat.

The final markup would be:

<div class="container">
    <div class="left"> ... </div>
    <div class="right"> ... </div>
    <div class="clear"></div>
</div>

Edit: Just like your last example, apparently. :)

Stephen
my question is why float shrink the width?
metal-gear-solid
+2  A: 

I think the better option is to use overflow:hidden. It is a simple one line change and it works.

div#container {
    ...
    overflow: hidden;
}

Adding extra divs for clear fix requires changes in html for something that is really css. Alternatively, when using clear fix by doing hacks like...

div:after {
    content:....
    ...
}

your css just gets bigger and messier. But it still is a good option (especially when you need to have things that overflow the box)

Reference: http://net.tutsplus.com/tutorials/html-css-techniques/css-fudamentals-containing-children/

Matt
Wow. I had no idea that would work. I actually thought it would NOT work, and had to go test if for myself. I'm off to kill most if not all of my `<div class="clear"></d>`
Stephen
Glad I could help, see the new link I've added for more info
Matt
my question is why float shrink the width?
metal-gear-solid
I would keep the clearing div until every browser is supporting content:after. And that will be in many years from now :(
Kau-Boy