Float, table, flex solve the problems and solutions of equal layout

Solve the problem

Solve the problem of float

Problem 1: If you add spacing to the sub-blocks, it will cause a row to be insufficiently divided and some blocks will be squeezed

盒子模型:box-sizing:
        content-box:盒子模型 width宽度 = width + padding-left/right + border-left/right
        border-box:怪异盒模型 width宽度 =width(padding+border已经计算进去了)
        margin 不影响盒子模型的大小 => 间距 / 位置移动
  • The solution to problem one. Method one to solve the border line: add box-sizing: border-box

Problem 2: After the margin is made with the box model, there is a gap in the left margin of the first module.

  • Wrong approach: Although you can deceive yourself visually, the leftmost module is actually bigger than the others
 .col1:first-child{
    
    
        border: 0px;
    } 

Solution to problem two:
Solution one: move the whole to the left-hide the spacing

#parent{
    
    
    height: 300px;
    /*对问题二的解决:整体往左边移动 - 间距隐藏起来*/
    margin-left: -10px;
}

Solution 2: The padding has been calculated into 25%, and the padding is part of the content.
The code block after the solution:

.col1,
.col2,
.col3,
.col4 {
    
    

height: 300px; 
/*等分 = float 100%/4*/
width: 25%;
float: left;
/*解决方法二:padding已经计算进了25%,padding是内容的一部分*/
padding-left: 10px;
box-sizing: border-box;

}

Resolved code block:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>5.1等分布局利用float+盒子模型用padding解决(解决方法二)</title>
    <style>
        *{
     
     margin: 0;padding: 0;}

        /* clearfix进行清浮动的封装 => 公用代码*/
        .clearfix::after{
     
     
            content: '';
            display: block;
            clear: both;
        }
        .parent-fix {
     
     
            overflow: hidden;
        }

        #parent{
     
     
            height: 300px;
            margin-left: -10px;

        }

        .col1,
        .col2,
        .col3,
        .col4 {
     
     

        height: 300px; 
        /*等分 = float 100%/4*/
        width: 25%;
        float: left;
        /*解决方法二:padding已经计算进了25%,padding是内容的一部分*/
        padding-left: 10px;
        box-sizing: border-box;

        }


        .col1 .inner{
     
     height: 300px; background-color:hotpink}
        .col2 .inner{
     
     height: 300px; background-color:rgb(105, 220, 255)}
        .col3 .inner{
     
     height: 300px; background-color:rgb(21, 255, 33)}
        .col4 .inner{
     
     height: 300px; background-color:rgb(203, 255, 13)}

    </style>
</head>
<body>
    <div class="parent-fix">
        <!-- 作为父级容器 -->
        <div id="parent" class="clearfix">
            <div class="col1">
                <div class="inner"></div>
            </div>
            <div class="col2">
                <div class="inner"></div>
            </div>
            <div class="col3">
                <div class="inner"></div>
            </div>
            <div class="col4">
                <div class="inner"></div>
            </div>
        </div>
    </div>
</body>
</html>

Solve the problem of table

Problem margin-left: -10px; causes the right side to be misaligned

Problem: If the padding-left: 10px; is removed from the first module, the first module will disappear directly

solution:

/*解决右边又不对齐*/
        .parent-wrap {
    
    
            margin-left:-10px;
        }
解决的代码:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>5.1等分布局利用float+盒子模型用padding解决(解决方法二)</title>
    <style>
        *{
     
     margin: 0;padding: 0;}


        /*解决右边又不对齐*/
        .parent-wrap {
     
     
            margin-left:-10px;
        }

        #parent{
     
     
            width: 100%;
            /*问题 margin-left: -10px;导致右边不对齐了 */
            /*table*/
            display:table;

        }


        .col1,
        .col2,
        .col3,
        .col4 {
     
     

        /*td*/
        display:table-cell;

        height: 300px; 


        padding-left: 10px;
        /*内部计算 padding-left*/
        
        }
        /*问题:第一个模块如果去掉padding-left: 10px;会导致第一个模块直接消失了*/
        /* .col1{
            padding-left: 0px;
        } */

        .col1 .inner{
     
     height: 300px; background-color:hotpink}
        .col2 .inner{
     
     height: 300px; background-color:rgb(105, 220, 255)}
        .col3 .inner{
     
     height: 300px; background-color:rgb(21, 255, 33)}
        .col4 .inner{
     
     height: 300px; background-color:rgb(203, 255, 13)}

    </style>
</head>
<body>
    <div class="parent-wrap">
        
            <!-- 作为父级容器 -->
            <div id="parent">
                <div class="col1">
                    <div class="inner"></div>
                </div>
                <div class="col2">
                    <div class="inner"></div>
                </div>
                <div class="col3">
                    <div class="inner"></div>
                </div>
                <div class="col4">
                    <div class="inner"></div>
                </div>
            </div>
        
    </div>
</body>
</html>

Solve the problem of flex

Code:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>5.1等分布局利用float+盒子模型用padding解决(解决方法二)</title>
    <style>
        *{
     
     margin: 0;padding: 0;}


        /*解决右边又不对齐*/
        .parent-wrap {
     
     
            margin-left:-10px;
        }

        #parent{
     
     
            width: 100%;
            /*问题 margin-left: -10px;导致右边不对齐了 */
            /*table*/
            display:table;

        }


        .col1,
        .col2,
        .col3,
        .col4 {
     
     

        /*td*/
        display:table-cell;

        height: 300px; 


        padding-left: 10px;
        /*内部计算 padding-left*/
        
        }
        /*问题:第一个模块如果去掉padding-left: 10px;会导致第一个模块直接消失了*/
        /* .col1{
            padding-left: 0px;
        } */

        .col1 .inner{
     
     height: 300px; background-color:hotpink}
        .col2 .inner{
     
     height: 300px; background-color:rgb(105, 220, 255)}
        .col3 .inner{
     
     height: 300px; background-color:rgb(21, 255, 33)}
        .col4 .inner{
     
     height: 300px; background-color:rgb(203, 255, 13)}

    </style>
</head>
<body>
    <div class="parent-wrap">
        
            <!-- 作为父级容器 -->
            <div id="parent">
                <div class="col1">
                    <div class="inner"></div>
                </div>
                <div class="col2">
                    <div class="inner"></div>
                </div>
                <div class="col3">
                    <div class="inner"></div>
                </div>
                <div class="col4">
                    <div class="inner"></div>
                </div>
            </div>
        
    </div>
</body>
</html>

Guess you like

Origin blog.csdn.net/qq_43218276/article/details/114631941
Recommended