zoukankan      html  css  js  c++  java
  • HTML-完美解决父子元素的外边距重叠和高度塌陷问题

    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="UTF-8">
            <title></title>
            <style type="text/css">
                *{margin:0px; padding:0px;}
                .box1{
                     300px;
                    height: 300px;
                    background-color: red
                }
                .box2{
                     200px;
                    height: 200px;
                    background-color: yellow;
                    margin-top: 100px;
                }
        
            </style>
        </head>
        <body>
            
            
            <div class="box1 ">
                
                <div class="box2"></div>
            </div>
            
        </body>
    </html>

    当把子元素margin-top:100 时,其父元素会跟着一样改变

    解决方法

    用css解决

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

    形成以下完全代码

    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="UTF-8">
            <title></title>
            <style type="text/css">
                *{margin:0px; padding:0px;}
                .box1{
                     300px;
                    height: 300px;
                    background-color: red
                }
                .box2{
                     200px;
                    height: 200px;
                    background-color: yellow;
                    margin-top: 100px;
                }
                .clearfix:before,
                .clearfix:after{
                    content: "";
                    display: table;
                    clear: both;
                }
                
                .clearfix{
                    zoom: 1;
                }
        
            </style>
        </head>
        <body>
            
            
            <div class="box1  clearfix">
                
                <div class="box2"></div>
            </div>
            
        </body>
    </html>

    运行后变成

  • 相关阅读:
    51nod 1127 最短的包含字符串
    hdu 2197 本原串
    hdu 2160 母猪的故事
    hdu 2594 Simpsons’ Hidden Talents
    自旋锁原理及java自旋锁
    Java中CAS详解
    dump相关
    多线程设置线程超时思路
    kafka遗忘点
    Java 和 HTTP 的那些事(四) HTTPS 和 证书(转)
  • 原文地址:https://www.cnblogs.com/hack-ing/p/11738316.html
Copyright © 2011-2022 走看看