In the process of front-end development, we often use float, an attribute that we both love and hate. Love it because we can easily layout through floating; hate it because there are too many problems left to solve after floating. This chapter will introduce to you why CSS clears floats and how to clear floats; let you know the problems that will occur after elements are floated, as well as several ways to eliminate floats in CSS. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
1. Why does it float?
The most fundamental reason for floating (float) is to achieve the text wrapping effect; later someone found that it is quite good to use it for layout, which can make up for some deficiencies in traditional layout. Very convenient.
2. Why do we need to clear floats? What is the role of css to clear floats?
Float (float) can control the floating box to move left and right until it encounters another floating box or the containing box on its outer edge. The floating box does not belong to the ordinary flow in the document flow. When the element is floated, it will not affect the layout of block-level elements, but will only affect the layout of inline elements. At this time, the normal flow in the document flow will show that the floating box does not have the same layout mode. When the height of the containing box is smaller than the floating box, "height collapse" will occur:
The height of the parent element in the above picture is the effect of padding, and the parent element is not set high.
When the height of the parent element is not set:
If the child element in the parent element is not set to float, the height of the parent element will also automatically If it is stretched, the height value will appear;
If the child element in the parent element is set to float, then the height of the parent element will not be automatically stretched, and there will be no height. value.
Obviously there are some problems after setting the float in this way, such as:
The margin of the parent element is affected and cannot be centered up, down, left, and right.
If the height of the parent element is not set and the height of the parent element is not expanded after floating, the parent element will not be displayed on the display.
Example description (background color)
Without clearing floats:
After clearing floats:
3. How to clear floats
The following introduces several ways to clear floats in css (Achieving the effect of the above picture):
1. Use an empty element with a clear attribute
Use an empty tag to clear the float: inside the parent element that needs to clear the float Add an empty tag after all floating elements (theoretically it can be any tag, but commonly used
) to clear the floating elements, and define the CSS code clear:both for them.
Code example:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <style> .demo{ width: 500px; margin: 50px auto; background-color: #CCCCCC; } .left{ width: 100px; height: 100px; float: left; background-color: #21B4BB; } .right{ width: 100px; height: 50px; float: right; background-color: #21B4BB; } .clear{ clear:both; } </style> </head> <body> <div class="demo"> <div class="left">left</div> <div class="right">right</div> <div class="clear"></div> </div> </body> </html>
Advantages: simple, less code, good browser compatibility.
Disadvantages: A large number of unsemantic html elements need to be added, the code is not elegant enough, and it is not easy to maintain later.
2. Use the overflow property of CSS
Use overflow to clear floats: Just define the CSS code overflow:auto or overflow:hidden in the element where the float needs to be cleared. That is Can.
Code example:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <style> .demo{ width: 500px; margin: 50px auto; background-color: #CCCCCC; overflow:hidden } .left{ width: 100px; height: 100px; float: left; background-color: #21B4BB; } .right{ width: 100px; height: 50px; float: right; background-color: #21B4BB; } </style> </head> <body> <div class="demo"> <div class="left">left</div> <div class="right">right</div> </div> </body> </html>
Advantages: There are no structural and semantic problems, and the amount of code is very small
Disadvantages: When the content increases, it is easy to cause automatic line wrapping, causing the content to be Hide it and cannot display the elements that need to overflow
3. Use CSS:after pseudo-element
Use the :after pseudo-element for the parent element and set display:table
display:table causes the generated elements to be displayed in a block-level table, occupying the remaining space.
Generate content as the last element through content: " ". As for what is in the content, it is OK. The classic CSS is content: ".". Some versions may have empty content.
Code example:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <style> .demo{ width: 500px; margin: 50px auto; background-color: #CCCCCC; *zoom: 1; } .demo:after { content: " "; display: table; clear: both; } .left{ width: 100px; height: 100px; float: left; background-color: #21B4BB; } .right{ width: 100px; height: 50px; float: right; background-color: #21B4BB; } </style> </head> <body> <div class="demo"> <div class="left">left</div> <div class="right">right</div> </div> </body> </html>
Disadvantages: suitable for modern browsers, does not support IE6/7, *zoom: 1 is for compatibility with IE6/7
The above is the detailed content of What does css clear float do? Methods to clean up floats (introduction). For more information, please follow other related articles on the PHP Chinese website!