The float property in CSS allows an element to float, causing it to move left or right until its outer edge touches the border of the containing box or another floating box.
Horizontal floating of an element means that the element can only move left or right and cannot move up or down.
Using float means using block layout, which modifies the computed value of the display property in the case of non-block-level elements.
A floating element will try to move left or right until its outer edge touches the border of the containing box or another floating box.
Floating elements are taken out of the normal flow of the document but not out of the text flow. When floating, they do not affect the surrounding box model, but the text wraps around the floating element. The document flow and text flow can be considered as hierarchical structures, while the floating element box and text element are on the same layer.
Document flow refers to the process of automatically arranging box elements from left to right and top to bottom in the layout.
Text flow refers to the process of automatically arranging text elements from left to right and top to bottom in the layout.
Taking an element out of the document flow means removing it from the normal layout. When positioning other boxes, the element that is taken out of the document flow is considered non-existent.
The document flow and text flow can be considered as layered structures, with floating element boxes and text elements in the same layer. When an element is floated, it is taken out of the document flow but not out of the text flow. Therefore, the rendering of floating elements and text is done together, and floating elements will push the text elements aside, resulting in a text wrapping effect around the floating elements.
Although float was originally designed to achieve text wrapping effects, in some layouts that use float, if you don't want to trigger the text wrapping effect, you can avoid it by triggering the Block Formatting Context (BFC).
Using floats can have some negative effects, such as not being able to support the parent element and causing the background and borders to not display correctly, overlapping with other elements, and lower-level floats still floating on top of higher-level floating elements. In such cases, it is necessary to clear the floats.
<!DOCTYPEhtml><html><head><title>Float</title><styletype="text/css">.container{border: 1px solid #eee;padding: 5px 0;margin: 5px 0;}.t1{margin: 0 5px;float: left;background-color: #EEE;height: 100px;width: 100px;}.clear::after{content:"";display: table;clear: both;}</style></head><body><divclass="container"><divclass="t1">Float</div><divclass="t1">Float</div><divclass="t1">Float</div><divstyle="height: 10px;background-color: blue;"></div><!-- Not clearing the float here will have negative effects --></div><divclass="container"><divclass="t1">Float</div><divclass="t1">Float</div><divclass="t1">Float</div><divstyle="height: 10px;background-color: blue;"></div><!-- Clearing the float --><divclass="clear"></div></div><!-- Clearing the float here is also possible, but it will not be able to support the container height --><divclass="container"><divclass="t1">Float</div><divclass="t1">Float</div><divclass="t1">Float</div><divstyle="height: 10px;background-color: blue;"></div><!-- Clearing the float --><divclass="clear"></div></div></body></html>