Front end clear float

First way: additional label clarity

<div style="clear: both;"></div> 

Features: if the float is cleared, the father will automatically check the height of the child, whichever is the highest

Advantages: easy to understand

Disadvantage: add additional tags to break the structure

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Extra label clear float</title>
    <style>
    .father {

    }
    .big {
        width: 200px;
        height: 200px;
        background-color: blue;
        float: left;
    }
    .small {
        width: 100px;
        height: 100px;
        background-color: yellow;
        float: left;
    }
    .footer {
        width: 400px;
        height: 100px;
        background-color: pink;
    }
    /* Clear floating */
    /* .clear {
        clear: both;
        If the float is cleared, the father will automatically check the height of the child, whichever is the highest
    } */

    </style>
</head>
<body>
    <div class="father">
        <div class="big"></div>
        <div class="small"></div>
    </div>
    <!-- Extra label clear float -->
    <!-- <div class="clear"></div> -->
    <div style="clear: both;"></div> <!-- Advantage:Easy to understand:Added a lot of extra useless tags to break the structure -->
    <!-- Extra label clear float -->
    <div class="footer"></div>
</body>
</html>

The second way: overflow cleanup

Add overflow:hidden in the parent tab box

Advantages: simple code

Disadvantages: when the content is increased, it is easy to avoid automatic line wrapping, resulting in the content being hidden

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>overflow Clear floating</title>
    <style>
    .father {
        /* Add overflow: hidden; to the parent element to clear the float
        Not all floats need to be cleared, and the influence will be cleared
        Advantages: simple code;
        Disadvantages: when the content is increased, it is easy to avoid automatic line wrapping, resulting in the content being hidden
        Cannot display elements that need to overflow; */
        overflow: hidden;

    }
    .big {
        width: 200px;
        height: 200px;
        background-color: blue;
        float: left;
    }
    .small {
        width: 100px;
        height: 100px;
        background-color: yellow;
        float: left;
    }
    .footer {
        width: 400px;
        height: 100px;
        background-color: pink;
    }

    </style>
</head>
<body>
    <div class="father">
        <div class="big"></div>
        <div class="small"></div>
    </div>
    <div class="footer"></div>
</body>
</html>

The third way: after pseudo element removal

. clearfix:after {/ * normal browser clear float*/
        content: "";
        display: block;
        height: 0;
        clear: both;
        visibility: hidden;
    }
    .clearfix{ 
zoom: 1; /* zoom 1 is the method for ie6 to clear floating * it is recognized by versions below ie7*/
    }
The fourth way: Double pseudo element marking

.clearfix:before, .clearfix:after {
        content: "";
        display: table;
    }
    .clearfix:after {
        clear: both;
    }
    .clearfix {
        *zoom: 1;
    }
 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>overflow Clear floating</title>
    <style>
    .clearfix:before, .clearfix:after {
        content: "";
        display: table;
    }
    .clearfix:after {
        clear: both;
    }
    .clearfix {
        *zoom: 1;
    }

    .father {

    }
    .big {
        width: 200px;
        height: 200px;
        background-color: blue;
        float: left;
    }
    .small {
        width: 100px;
        height: 100px;
        background-color: yellow;
        float: left;
    }
    .footer {
        width: 400px;
        height: 100px;
        background-color: pink;
    }

    </style>
</head>
<body>
    <div class="father clearfix">
        <div class="big"></div>
        <div class="small"></div>
    </div>
    <div class="footer"></div>
</body>
</html>

 

Posted by marf on Fri, 22 Nov 2019 12:40:32 -0800