CSS clear floating method (super detailed)

1. Why clear the float ?

The principle of floating is to let the picture leave the document flow and float directly on the desktop . When we generally layout, we only set the width but not the height, and let the content automatically fill the height. However, after using float , the height of the original filling will disappear, and the height of the parent element will be 0. Subsequent addition of content layout will cause confusion and cause height collapse . At this time, clear float can be used to solve the problem of parent element height collapse .


   
   
    
    
  1. <style>
  2. *{
  3. margin: 0;
  4. padding: 0;
  5. }
  6. .box1{
  7. width: 100px;
  8. height: 100px;
  9. background-color: pink;
  10. }
  11. .box2{
  12. width: 80px;
  13. height: 80px;
  14. background-color: skyblue;
  15. }
  16. </style>
  17. <div class="box1">1 </div>
  18. <div class="box2">2 </div>

Taking the above code as an example, by default, its display effect is as follows:

Now add a float: left to box 1, and let's see what happens:

 We can see that the No. 2 box is missing on the desktop, so what is the reason? The reason is that the No. 1 box has floated up, and the space it originally occupied has been vacant, so the No. 2 box is below the No. 1 box, as shown in the figure shown,

 So here is our solution:

2. Solution one (clear)

Add a clear:left to the second box directly. It should be noted here that clear eliminates the influence of the previous box on itself, so we will clear whichever side the previous box floats to. The previous box here is float :left, so we added clear:left to the second box

 3. Solution 2 (overflow: hidden)

The overflow attribute needs to be added to the parent element, so we add a parent element to the two boxes. In order to see the height collapse, we add a bottom box, as follows:


   
   
    
    
  1. <style>
  2. *{
  3. margin: 0;
  4. padding: 0;
  5. }
  6. .box1{
  7. width: 100px;
  8. height: 100px;
  9. background-color: pink;
  10. }
  11. .box2{
  12. width: 80px;
  13. height: 80px;
  14. background-color: skyblue;
  15. }
  16. .father{
  17. width: 100%;
  18. border: 2px solid red;
  19. }
  20. .bottom{
  21. width : 100%;
  22. height: 200px;
  23. background-color: green;
  24. }
  25. </style>
  26. <div class="father">
  27. <div class="box1">1 </div>
  28. <div class="box2">2 </div>
  29. </div>
  30. <div class="bottom">3 </div>

This is the default state 

 Now add floats to boxes 1 and 2, let's see what happens


   
   
    
    
  1. <style>
  2. .box1{
  3. width: 100px;
  4. height: 100px;
  5. background-color: pink;
  6. float: left;
  7. }
  8. .box2{
  9. width: 80px;
  10. height: 80px;
  11. background-color: skyblue;
  12. float : left;
  13. }
  14. </style>

You can see that our bottom box is not in the original position. What is the reason? The reason is that we did not set the height for the parent element at this time. The height of the parent element is completely supported by the No. 1 and No. 2 boxes. Now give 1 The No. 2 box is added with floating, which does not occupy the original position, so the height of the parent element is zero at this time. If it does not occupy the position, the bottom box will go up, causing the height to collapse.

The solution is: add an overflow:hidden to the parent element


   
   
    
    
  1. <style>
  2. .father{
  3. width: 100%;
  4. border: 2px solid red;
  5. overflow: hidden;
  6. }
  7. </style>

 4. Solution three (add height to the parent element)

The third method is to add a height to the parent element. Note that the height of the parent element must be set greater than the height of the highest box in the child element. In this example, the height of the highest box in the child element is 100px, then the parent element The height must be greater than or equal to 100px


   
   
    
    
  1. <style>
  2. .father {
  3. width : 100%;
  4. height: 100px;
  5. border: 2px solid red;
  6. }
  7. </style>

 5. Solution four (additional label method)

Add an empty label after the label that needs to be cleared and add clear:both to it


   
   
    
    
  1. <div class="father">
  2. <div class="box1">1 </div>
  3. <div class="box2">2 </div>
  4. <div class="clear"> </div>
  5. </div>
  6. <div class="bottom">3 </div>

   
   
    
    
  1. <script>
  2. .clear{
  3. clear: both;
  4. }
  5. </script>

6. Solution five ( use the after pseudo-element to clear the float

This method also has a disadvantage, that is, it does not support IE6, IE7


   
   
    
    
  1. <style>
  2. .father :after{
  3.     content: "";
  4.     display: block;
  5.     height: 0;
  6.     clear:both;
  7.     visibility: hidden;
  8. }
  9. .father{
  10.     zoom: 1;
  11. /*The way IE6 clears the float is only implemented by IE6-IE7, and other browsers do not execute it*/
  12. }
  13. </style>

1. Why clear the float ?

The principle of floating is to let the picture leave the document flow and float directly on the desktop . When we generally layout, we only set the width but not the height, and let the content automatically fill the height. However, after using float , the height of the original filling will disappear, and the height of the parent element will be 0. Subsequent addition of content layout will cause confusion and cause height collapse . At this time, clear float can be used to solve the problem of parent element height collapse .


   
   
  
  
  1. <style>
  2. *{
  3. margin: 0;
  4. padding: 0;
  5. }
  6. .box1{
  7. width: 100px;
  8. height: 100px;
  9. background-color: pink;
  10. }
  11. .box2{
  12. width: 80px;
  13. height: 80px;
  14. background-color: skyblue;
  15. }
  16. </style>
  17. <div class="box1">1 </div>
  18. <div class="box2">2 </div>

Taking the above code as an example, by default, its display effect is as follows:

Now add a float: left to box 1, and let's see what happens:

 We can see that the No. 2 box is missing on the desktop, so what is the reason? The reason is that the No. 1 box has floated up, and the space it originally occupied has been vacant, so the No. 2 box is below the No. 1 box, as shown in the figure shown,

 So here is our solution:

2. Solution one (clear)

Add a clear:left to the second box directly. It should be noted here that clear eliminates the influence of the previous box on itself, so we will clear whichever side the previous box floats to. The previous box here is float :left, so we added clear:left to the second box

 3. Solution 2 (overflow: hidden)

The overflow attribute needs to be added to the parent element, so we add a parent element to the two boxes. In order to see the height collapse, we add a bottom box, as follows:


   
   
  
  
  1. <style>
  2. *{
  3. margin: 0;
  4. padding: 0;
  5. }
  6. .box1{
  7. width: 100px;
  8. height: 100px;
  9. background-color: pink;
  10. }
  11. .box2{
  12. width: 80px;
  13. height: 80px;
  14. background-color: skyblue;
  15. }
  16. .father{
  17. width: 100%;
  18. border: 2px solid red;
  19. }
  20. .bottom{
  21. width : 100%;
  22. height: 200px;
  23. background-color: green;
  24. }
  25. </style>
  26. <div class="father">
  27. <div class="box1">1 </div>
  28. <div class="box2">2 </div>
  29. </div>
  30. <div class="bottom">3 </div>

This is the default state 

 Now add floats to boxes 1 and 2, let's see what happens


   
   
  
  
  1. <style>
  2. .box1{
  3. width: 100px;
  4. height: 100px;
  5. background-color: pink;
  6. float: left;
  7. }
  8. .box2{
  9. width: 80px;
  10. height: 80px;
  11. background-color: skyblue;
  12. float : left;
  13. }
  14. </style>

You can see that our bottom box is not in the original position. What is the reason? The reason is that we did not set the height for the parent element at this time. The height of the parent element is completely supported by the No. 1 and No. 2 boxes. Now give 1 The No. 2 box is added with floating, which does not occupy the original position, so the height of the parent element is zero at this time. If it does not occupy the position, the bottom box will go up, causing the height to collapse.

The solution is: add an overflow:hidden to the parent element


   
   
  
  
  1. <style>
  2. .father{
  3. width: 100%;
  4. border: 2px solid red;
  5. overflow: hidden;
  6. }
  7. </style>

 4. Solution three (add height to the parent element)

The third method is to add a height to the parent element. Note that the height of the parent element must be set greater than the height of the highest box in the child element. In this example, the height of the highest box in the child element is 100px, then the parent element The height must be greater than or equal to 100px


   
   
  
  
  1. <style>
  2. .father {
  3. width : 100%;
  4. height: 100px;
  5. border: 2px solid red;
  6. }
  7. </style>

 5. Solution four (additional label method)

Add an empty label after the label that needs to be cleared and add clear:both to it


   
   
  
  
  1. <div class="father">
  2. <div class="box1">1 </div>
  3. <div class="box2">2 </div>
  4. <div class="clear"> </div>
  5. </div>
  6. <div class="bottom">3 </div>

   
   
  
  
  1. <script>
  2. .clear{
  3. clear: both;
  4. }
  5. </script>

6. Solution five ( use the after pseudo-element to clear the float

This method also has a disadvantage, that is, it does not support IE6, IE7


   
   
  
  
  1. <style>
  2. .father :after{
  3.     content: "";
  4.     display: block;
  5.     height: 0;
  6.     clear:both;
  7.     visibility: hidden;
  8. }
  9. .father{
  10.     zoom: 1;
  11. /*The way IE6 clears the float is only implemented by IE6-IE7, and other browsers do not execute it*/
  12. }
  13. </style>

Guess you like

Origin blog.csdn.net/m0_71349739/article/details/128860511