CSS float's question: when both div are floated, why are they displayed on the same line?

look at the code

first.
MDN:


:MDN,
.box2?

MDN: how floating elements are positioned

CSS():" , "
float  none ,  display  block ,div?
May.27,2022

but why did the .box2 element move up?

Box2 does not move up because of its float. After .box1 is out of the document stream, .box2 is on the first line.

When
and float is not none, the display calculated value of the element is block, so the two div should not be on the same line?

Block-level elements occupy a row only when there is a normal stream.


  • explains it shallowly: didn't people also say, or until you hit the border of another floating element ?
  • A slightly more professional explanation: from your question, you can see that your mind is still in a static position, while floating elements will deviate from the document flow. What do you think of breaking away from the document stream ?
    if you don't understand, you can remove the floats in your code and change the positioning to absolute positioning.

Hello, the layout principle goes like this:
first, it is arranged from top to bottom by block, so div1 is on the first line, div2 is on the second line, and it is placed vertically. This is the basic flow.
secondly, if there is a floating flow, the floating flow is arranged before the basic flow, that is, it occupies the top part of the page
again, the floating flow is discharged horizontally, for example, if the floating flow flows to the left, the first floating element is arranged on the far left. On the second left, if the line is full, you have to change lines. The whole floating stream is similar to the composition we usually write on paper, from left to right and from top to bottom
again, the original width of div is 100%, but the width of float is automatically reduced to the minimum required width
above for reference


.

CSS detaches from the document stream, that is, the element is removed from the normal layout, and other boxes are positioned as if the element out of the document stream does not exist.
Note that when you use float to detach from the document stream, other boxes will ignore this element, but the text in other boxes will still make room for this element and surround it.
for elements that use position:absolute to detach from the document stream, the text in other boxes and other boxes will ignore it.

look at the example:
two floating div, the third div without floating ignores these two elements that leave the document stream, but the text in div does not ignore

clipboard.png

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        body {
            padding: 0;
            margin: 0;
            background-color: rgba(0, 0, 0, 0.1)
        }
        .div-fl {
            float: left;
            width: 50px;
            height: 50px
        }

        .div1 {
            background: rgba(241, 110, 9, 0.8);
        }

        .div2 {
            background: -sharp0000ffa1;
        }

        .div3 {
            width: 500px;
            height: 20px;
            background: green;
        }

        .div-pos-ab {
            width: 500px;
            height: 20px;
            background: -sharpffff0075;   
            position: absolute;
            color: red;
        }
    </style>
</head>
<body>
<div class=" div-fl div1"></div>
<div class="div-fl  div2"></div>

<div class="div3">this a div </div>
<span>this is some text,this is some text,this is some text,this is some text</span>

<div class="div-pos-ab">this is a absolute dom</div> <span>this is some text,this is some text,this is some text,this is some text,this is some text,this is some text</span> </body> </html>

both div are separated from the document flow, which means that the two div are rearranged according to the upper-left origin of the page. You can understand that if the second layer


is separated from the document flow, both div will be in the first line and float to the left, and both div will translate to the left or right until they hit the frame of the container, or encounter another floating element

.
Menu