zoukankan      html  css  js  c++  java
  • div嵌套导致子区域margin-top失效不起作用的解决方法

    有两个嵌套关系的div,如果外层div的父元素padding值为0,

    那么内层div的margin-top或者margin-bottom的值会“转移”给外层div,使父元素产生上外边距。

     1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
     2 <html xmlns="http://www.w3.org/1999/xhtml">
     3 <head>
     4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
     5 <title>无标题文档</title>
     6 </head>
     7 <body>
     8     <div style="background-color:#FF0000;300px; height:100px">上部层</div>
     9     <div style="background-color:#009900; 300px; height:300px;overflow:hidden "> <!--父层-->
    10     <div style="margin:50px; background-color:#000000;200px; height:200px"">子层</div>
    11 </div>
    12 </body>
    13 </html>

    原因:盒子没有获得 haslayout 造成 margin-top无效     (haslayout 是Windows Internet Explorer渲染引擎的一个内部组成部分。)

    解决办法:

    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;

     

  • 相关阅读:
    手机各种JS语法,随时更新
    ionic上拉加载-下拉刷新
    JS获取浏览器信息及屏幕分辨率
    jQuery元素的显示、隐藏及动画
    jQuery原型
    表单序列化为对象
    html里的ajax数据传输
    面试技巧-互联网行业通吃
    jquery表单验证validate
    Javascript缓动动画原理
  • 原文地址:https://www.cnblogs.com/hegaiying/p/5937762.html
Copyright © 2011-2022 走看看