嵌套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="width:300px; height:100px">上部层</div>

<div style=" width:300px; height:300px;overflow:hidden "> <!--父层-->
     <div style="margin:50px; width: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;

最新文章

  1. ubuntu文本模式/终端中文乱码解决
  2. Mina、Netty、Twisted一起学(十):线程模型
  3. this和super
  4. Ubuntu安装SSH服务
  5. web.config
  6. delphi 常用属性+方法+事件+代码+函数
  7. String和bytes的编码转换
  8. xml--通过DOM解析XML
  9. CSS学习笔记总结和技巧
  10. iOS第三方库
  11. ob缓存
  12. loadrunner 手工参数拼接与l oadrunner的url编码
  13. python close()是假象,真正关闭Socket的方法
  14. SharePoint Framework 基于团队的开发(四)
  15. iOS ---------- 获取设备的各种信息
  16. Spirng boot maven多模块打包不踩坑
  17. pytonn04day
  18. 项目管理心经——正确的使用Microsoft Project
  19. SimpleDateFormat使用详解 &lt;转&gt;
  20. 通过 lsyncd + rsync 同步文件

热门文章

  1. Python学习 之 对内存的使用(浅拷贝和深拷贝)
  2. oracle 基本操作
  3. javascript类,对象,实例
  4. spring源码分析之spring-jms模块详解
  5. centos、linux改变ll命令显示颜色
  6. Maven学习小结(七 生命周期[转])
  7. 通过java实现对数据库的增删改查
  8. cocos2d-x3.x使用rapidjson
  9. [改善Java代码]不要在finally块中处理返回值
  10. MapReduce的方式进行HBase向HDFS导入和导出