1

I am learning floating elements in CSS and i encountered the following peculiar behavior.

Here is the code

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>

        #one { background-color: red; width: 200px; height: 200px; margin: 10px; }
        #two { background-color: yellow; width: 200px; height: 205px; margin: 10px; }
        #three { background-color: lightpink; width: 200px; height: 200px; margin: 10px; }
        #four { background-color: green; width: 200px; height: 200px; margin: 10px; }
        #five { background-color: coral; width: 200px; height: 200px; margin: 10px; }
        #six { background-color: #b1ffea; width: 200px; height: 200px; margin: 10px; }

        div{
            /*display: inline-block;*/
            float: left;
            vertical-align: central;
        }


    </style>

</head>
<body>

   <div id="one">
       First div
   </div>

   <div id="two">
      Second div
   </div>

   <div id="three">
       three div
   </div>

    <div id="four">
       four div
   </div>

    <div id="five">
       five div
    </div>

    <div id="six">
       six div
   </div>

</body>
</html>

enter image description here

My question is why "four div" is placed below the "third div" not first and second ?

On the other hand, if i resize the browser (see image below) why in this case it is working perfectly ?

enter image description here

3 Answers 3

1

Because the second div element is taller than the first div, the forth div collides with the second div. When the forth div drops to the next line, it starts on the right and slides left, beginning under the third div, until it collides with the second div. If you look at your code at with 4 div elements per line, the fifth div drops to the next line. It begins below the fourth div and beginning moving left until it collides with the second div, because the second div is longer that the third or fourth div elements.

enter image description here

Sign up to request clarification or add additional context in comments.

5 Comments

how it slides ? pls elaborate or give url to the resource you are referring to
I added another example using 4 div elements per line. I'll try to find a resource to point you to, but this is how float positioning works when elements have different heights.
If the div elements have a predetermined width, this is where you would use CSS Media Queries to add a clear: left; to the appropriate :nth-of-type to make the rows uniform.
This site does a decent job of explaining float positioning: designshack.net/articles/css/…
@Cody In your example, if you increase the height of div 3 to the height of div 2, div 4 should be positioned under div 1.
1

This is perfectly working as usual. Height of div2 is 205px instead of 200px. Change it to 200px and it will work fine.

Comments

0

I adjusted the CSS margins of #two using Inspect to this...

enter image description here

After that change, the div elements all seemed to behave as you wanted them to when the browser's window width was adjusted.

4 Comments

I know how to that. But i am not getting get why "four div" is not placed below the first div
w3schools.com has this warning regarding the CSS box model... Important: When you set the width and height properties of an element with CSS, you just set the width and height of the content area. To calculate the full size of an element, you must also add padding, borders and margins. ( I hope that answers your question. )
@Cody as I said in my answer, div 4 is colliding with div 2, due to div 2 being taller than div 3.
@JohnH That does not explain to him how CSS float positioning works. I'm not really sure how the warning from w3schools.com is relevant at all.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.