zoukankan      html  css  js  c++  java
  • margin-top塌陷问题

    margin-top塌陷是怎么回事呢?

    两个盒子嵌套的时候,内部盒子的margin-top会加到外面的盒子上面,导致盒子的margin-top塌陷。这是一个系统级别的bug.

    解决的办法有:

    1.给盒子加上一个边框,缺点是盒子会有一个边框影响美观

    2.外部盒子设置overflow:hidden;缺点是当盒子里面的元素溢出的时候,会裁剪掉盒子里面的内容。

    3.使用伪元素方法(最优的方法):这个方法的原理至今没弄懂,为啥加上一个伪元素和一个display:table就可以解决了呢???

    .clearfix:before{

      content:"";

      display:table;

    }

    代码如下:

    <!DOCTYPE html>
    <html>
    <head>
    <title>margin特性</title>
    <style type="text/css">
    .con{
    300px;
    height: 200px;
    background-color: gold;
    /*设置盒子居中的方法:padding或者margin
    100px;
    height: 100px;
    padding: 50px 100px;*/

    /*利用margin来居中会出现margin-top塌陷的问题;解决margin-top塌陷的方法一:加外边框;但是会加上一个边框
    298px;
    height: 198px;
    background-color: gold;
    border:1px solid red;*/

    /*解决margin-top塌陷第二种方法
    overflow: hidden;*/
    }

    .box{
    100px;
    height: 100px;
    background-color: green;
    margin:50px 100px;
    }

    .clearfix:before{
    content: "";
    display: table;
    }
    </style>
    </head>
    <body>
    <div class="con clearfix">
    <div class="box"></div>
    </div>
    </body>
    </html>

  • 相关阅读:
    CentOS 7下PXE+Kickstart无人值守安装操作系统
    利用pentestbox打造ms17-010移动"杀器"
    XSS测试代码
    sublime Text3基本配置记录+python
    CTF中那些脑洞大开的编码和加密
    信息安全相关资源
    RIP 实验
    python输出有色记录
    下载Chrome商店和Youtube资源
    mysql使用问题记录
  • 原文地址:https://www.cnblogs.com/pengwa1226/p/12286299.html
Copyright © 2011-2022 走看看