• div里面的margin-top失效


    div标签中的元素margin-top失效的解决方法

    元素上级标签是div,已经设置了width和height等的属性,可是,在对元素使用margin进行调整的时候,无法生效,下面有个不错的解决方法,感兴趣的朋友可以参考下
     
    如题。

    问题很奇葩。元素上级标签是div,已经设置了width和height等的属性,可是,在对元素使用margin进行调整的时候,无法生效。同学经验提示,对该元素的父元素属性设置成

    overflow:hidden; 
     
     
     
    嵌套div中margin-top转移问题的解决办法

    在这两个浏览器中,有两个嵌套关系的div,如果外层div的父元素padding值为0,那么内层div的margin-top或者margin-bottom的值会“转移”给外层div。

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>无标题文档</title>
    </head>

    <body>
    <div style="300px; height:100px">上部层</div>

    <div style=" 300px; height:300px;overflow:hidden "> <!--父层-->
         <div style="margin:50px; 200px; height:200px"">子层</div>
    </div>

    </body>
    </html>

    原因:盒子没有获得 haslayout  造成 margin-top无效

    解决办法:

    1、在父层div加上:overflow:hidden;

    2、把margin-top外边距改成padding-top内边距 ;

    3、父元素产生边距重叠的边有不为 0 的 padding 或宽度不为 0 且 style 不为 none 的 border。

        父层div加: padding-top: 1px;

    4、让父元素生成一个 block formating context,以下属性可以实现
        * float: left/right
        * position: absolute
        * display: inline-block/table-cell(或其他 table 类型)
        * overflow: hidden/auto

       父层div加:position: absolute;
  • 相关阅读:
    css相关
    文章管理列表
    高性能MySQL基础篇
    mysql
    node.js开发实战
    React Hooks
    client-*,scroll-*,offset-*的区别
    将create-react-app从javascript迁移到typescript
    为Github项目添加Travis持续集成服务
    cookie储存
  • 原文地址:https://www.cnblogs.com/hduhdc/p/5286370.html
Copyright © 2020-2023  润新知