• margin重叠现象


    1.上下/左右相邻的普通元素margin,不是两者相加之和,而是取最大值,这个现象叫做margin重叠。 
    2. 普通元素才会发生margin重叠,如果是float元素,就不会发生。margin是两者相加之和。

    例如:1普通元素

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>练习(margin重叠现象)</title>
        <style>
                      #test1{
                          height: 100px;
                          background: blue;
                          margin-bottom: 50px;
                      }
                      #test2{
                          height: 100px;
                          background: green;
                          margin-top:50px; 
                      }
        </style>
    </head>
    <body>
        <div id="test1"></div>
        
        <div id="test2"></div>
    </body>
    </html>

    2,浮动元素:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>练习(margin重叠现象)</title>
        <style>
                      #test1{
                          height: 100px;
                        width: 200px;
                          background: blue;
                        margin-bottom: 50px;
                        float: left;
                      }
                      #test2{
                          height: 100px;
                        width: 200px;
                          background: green;
                           float: left;
                         clear: left;
                          margin-top:50px; 
                      }
        </style>
    </head>
    <body>
        <div id="test1"></div>
            <div id="test2"></div>
        
    </body>
    </html>

    3,父子(普通元素)发生margin重叠现象

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>练习(margin重叠现象)</title>
        <style>
                      #test1{
                          height: 150px;
                        width: 200px;
                          background: blue;
    
                       
                      }
                      #test2{
                          height: 50px;
                        width: 50px;
                          background: green;
                          
                          margin-bottom:50px; 
                      }
                      #test3{
                        height: 50px;
                        width: 50px;
                        background: green;
                        margin-top: 50px;
                      }
        </style>
    </head>
    <body>
        <div id="test1">
            <div id="test2"></div>
            <div id="test3"></div>
        </div>
    </body>
    </html>

    4,父子(子为浮动元素)不发生margin重叠

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>练习(margin重叠现象)</title>
        <style>
                      #test1{
                          height: 150px;
                        width: 200px;
                          background: blue;
    
                       
                      }
                      #test2{
                          height: 50px;
                        width: 50px;
                          background: green;
                          float: left;
                          margin-bottom:50px; 
                      }
                      #test3{
                        height: 50px;
                        width: 50px;
                        background: green;
                        float: left;
                        clear: left;
                        margin-top: 50px;
                      }
        </style>
    </head>
    <body>
        <div id="test1">
            <div id="test2"></div>
            <div id="test3"></div>
        </div>
    </body>
    </html>

  • 相关阅读:
    sqlserver2005存储汉字成问号解决办法:
    .net c# 日期格式和常用处理
    文件夹无法访问拒绝访问,无法删除文件的,快速有效解决方法
    打印出所有的 info.plist 中的 keys、values
    利用时间戳来准确计算某个时间点具现在的时间差
    项目中的技巧经验汇总
    "Base SDK Missing"问题的解决
    UIPopoverController的使用
    给ios自带控件添加圆角边的方法
    实现程序内截屏功能的代码
  • 原文地址:https://www.cnblogs.com/lq13035130506/p/10459008.html
Copyright © 2020-2023  润新知