float 与 flex

概述

float 与 flex 均能实现九宫格布局

HTML

<div class="boxFather">
    <div class="box1 box"></div>
    <div class="box2 box"></div>
    <div class="box3 box"></div>
    <div class="box4 box"></div>
    <div class="box5 box"></div>
    <div class="box6 box"></div>
</div>

flex

flex 需要在父视图中设置

.boxFather {
    background-color: pink;
    width: 500px;
    height: 500px;
    display: flex;
    flex-wrap: wrap;
}

float

float 需要在子视图中设置
并且使用 float 需要在后面主动清除浮动

.boxFather {
    background-color: pink;
    width: 500px;
    height: 500px;
}

.box {
    width: 110px;
    height: 60px;
    float: left;
    width: calc(calc(100%/3) - 10px);
    margin-left: 10px;
    margin-bottom: 10px; 
}

.boxFather::after {
    clear: both;
    content: "";
    display: block;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。