Home > Article > Web Front-end > Introduction to the method of setting floating elements using CSS float attribute
A floating box can be moved left or right until its outer edge touches the border of the containing box or another floating box.
Because the float is not in the document's normal flow, blocks in the document's normal flow behave as if the float does not exist.
float effect display
Basic settings
Add 3 p color blocks to the web page normally:
HTML code:
<p class="p1"> p1 </p> <p class="p2"> p2 </p> <p class="p3"> p3 </p>
Simply set some sizes and colors:
CSS code:
.p1 { width: 100px; height: 100px; background-color: cyan; } .p2 { width: 100px; height: 100px; background-color: skyblue; } .p3 { width: 100px; height: 100px; background-color: green; }
.p1 { width: 100px; height: 100px; background-color: cyan; float: rightright; }
.p1 { width: 100px; height: 100px; background-color: cyan; float: left; } .p2 { width: 100px; height: 100px; background-color: skyblue; float: left; } .p3 { width: 100px; height: 100px; background-color: green; float: left; }
.p1 { width: 100px; height: 130px; background-color: cyan; float: left; } .p2 { width: 300px; height: 100px; background-color: skyblue; float: left; } .p3 { width: 100px; height: 100px; background-color: green; float: left; }
Use overflow to clear floats
Example:
.overflow-clear-float {overflow:hidden;}or
overflow-clear-float {overflow:auto;}
Note compatibility issues:
The concept of Block Formatting Contexts was proposed within the CSS 2.1 specification. Therefore, it is not supported in IE6/7. This is because previous IE versions only fully implemented the CSS 1 specification standard, and part of the CSS 2.0 specification. In IE 7, the hasLayout attribute can be triggered when the overflow value is non-visible. This also allows IE 7 to also make containers contain floating elements.