About the floating of css.

Why the floating element on the left is affected by the block element margin on the right.
when the Left is not floating.

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">  
    <style> 
    -sharpleft { 
        width: 20%;
        height: 80px;
        background-color: -sharp5555da;  
        text-align: center;
        color: -sharpfff;
        padding-top: 40px;
    } 
    -sharpcontent { 
        margin-left: 22%; 
    }
    body {
        margin: 0;
    }
    </style>
</head>
<body> 
    <div id = "left"> 
    </div>
    <div id = "content">
        <h2></h2> 
    </div>
   
</body>
</html>

clipboard.png

left


clipboard.png

the floating element on the left will be affected by H2. Margin appears and a blank line appears. This is why and how to solve it.

Jul.04,2022

mainly because H2 has an up and down margin. Just set the margin of H2 to 0.

  

elements with floating attributes are wrapped, and all elements with floating attributes combine the advantages of block elements and inline elements, so that elements can not only set width and height, but also be arranged horizontally.
elements with floating attributes are arranged and laid out from the standard flow, and the elements that leave the standard stream are not in the same stream as the block elements, making the elements with floating attributes float above the normal block elements. However, although the floating block is divorced from the normal document flow, it still occupies the text space of the normal document flow. So the text written after it will not be overwritten by the floating element but will continue to be arranged horizontally beyond the newline.

Menu