美文网首页
解决高度塌陷、外边距重叠

解决高度塌陷、外边距重叠

作者: xiaohan_zhang | 来源:发表于2019-11-26 12:40 被阅读0次
    • 高度塌陷
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>解决高度塌陷</title>
        <style>
            .box1{
                border: 10px red solid;
            }
            .box2{
                width: 100px;
                height: 100px;
                background-color: cadetblue;
                float: left;
            }
            .box1::after{
                content: '';
                display: block;
                clear: both;
            }
            /*box3没有脱离文档流,box3会把box1高度撑起来*/
            /*.box3{*/
            /*    clear: both;*/
            /*}*/
        </style>
    </head>
    <body>
        <div class="box1">
            <div class="box2"></div>
    <!--        <div class="box3"></div>-->
        </div>
    </body>
    </html>
    
    • 同时解决高度塌陷和外边距重叠
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>解决外边距重叠</title>
        <style>
            .box1{
                width: 200px;
                height: 200px;
                background-color: aqua;
            }
            .box2{
                width: 100px;
                height: 100px;
                background-color: fuchsia;
                margin-top: 100px;
            }
            /*可以同时解决高度塌陷和外边距重叠问题*/
            .clearfix::before,.clearfix::after{
                content: '';
                display: table;
                clear: both;
            }
        </style>
    </head>
    <body>
        <div class="box1 clearfix">
            <div class="box2"></div>
        </div>
    </body>
    </html>
    

    相关文章

      网友评论

          本文标题:解决高度塌陷、外边距重叠

          本文链接:https://www.haomeiwen.com/subject/ekuewctx.html