Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

floating element with same width

Tags:

html

css

I have three divs and am floating the first left. The other two wrap around it to the right fine if only the floated element has a width set. But if I set a width for the other 2 divs too, they no longer wrap around the first, just stack up below as in normal flow fashion.

I understand I would need to add the same float class to divs 2 and 3 to get them to float inline, but I was curious as to why this behavoir occuers if all three have widths (even if the widths add up to less than the available broswer window width). Here is the code:

<!DOCTYPE html>
<html>

    <head>
        <style>
            .one {
                background-color: steelblue;
                padding: 10px;
                width: 200px;
            }
            .two {
                background-color: orange;
                padding: 10px;
                width: 200px;
            }
            .three {
                background-color: red;
                padding: 10px;
                width: 200px;
            }
            .float {
                float: left;
            }
        </style>
    </head>

    <body>
        <div class="one float">
            <p>I am paragraph one</p>
        </div>
        <div class="two">
            <p>I am paragraph two</p>
        </div>
        <div class="three">
            <p>I am paragraph three</p>
        </div>
    </body>

</html>
like image 945
Alan Avatar asked Mar 22 '23 02:03

Alan


2 Answers

This might be best explained with a few pictures. First let’s get rid of your third div as it really isn't needed to explain what’s going on.

When you float your first div and don’t give the second div a width, you get this:

enter image description here

The first (floated) div is taken from the normal flow and placed along the left side of its container, where text and inline elements will wrap around it, as floats are supposed to do. What actually then happens is the second div acts like it’s placed behind the first div as you can see when you inspect the document:

enter image description here

Notice how the second div doesn't start at the right edge of the first div – it actually exists in the same space as the first div (appearing as if it was behind it); however the text in the second div begins where the first div ends. The second div then proceeds to take up 100% of the width of its container since it's a block level element. Only the text inside the div is being manipulated by the first floated div.

Now, what happens if we then set a width on the second div? Well you get the following:

enter image description here

So the question is, why does something as simple as setting the width on the second div appear to nullify the float rule on the first div? Well, it’s not. Here’s what’s going on. Just like in the first example, the second div appears to exist behind the first div, however this time you’re explicitly limiting the amount of room the text has to exist. Again if we highlight the second div in the document you’ll see the space it occupies:

enter image description here

Since in this case you’re explicitly setting a width of 200px, there is no space to the right of the floated div for the text to exist, so it gets pushed down below the floated div. Here’s an image that might make it all clearer. Let’s say we increase the width of the second div from 200px to 250px. We then get this:

enter image description here

Now that there’s room to the right of the first div, the text will begin next to it, and drop down below it once it runs out of room horizontally. Continue to increase the width of the second div and you’ll end up with the text of both divs existing next to each other horizontally.

What you want to take away from this is that setting a width on the second div doesn't kill the float rule of the first div, it just limits the amount of room for content to exist.

like image 198
j08691 Avatar answered Apr 01 '23 17:04

j08691


To quench your curiosity....div's are by default, block level elements....since you haven't clear'ed the float after the 1st block, they still have the effect on the container....

and since block-level-elements occupy the whole width,and the float effect still exist you have them wrapping around the floated div...

clear the divs and u'll see a different version quench your curiosity here

like image 31
NoobEditor Avatar answered Apr 01 '23 16:04

NoobEditor