div嵌套引起的margin-top不起作用(子div设定margin-top,导致父div与其他元素有间隙)
2013-05-20 17:03
441 查看
问题:嵌套div中margin-top转移问题的解决办法(子div设定margin-top,导致父div与其他元素有间隙)
如图:
在这浏览器中,有两个嵌套关系的div,如果外层div的父元素padding值为0,那么内层div的margin-top或者margin-bottom的值会“转移”给外层div。
原因:盒子没有获得 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;
原文地址:http://blog.sina.com.cn/s/blog_6bec36f9010110w9.html
如图:
在这浏览器中,有两个嵌套关系的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="background-color:#FF0000;width:300px; height:100px">上部层</div> <div style="background-color:#009900; width:300px; height:300px;overflow:hidden "> <!--父层--> <div style="margin:50px; background-color:#000000;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;
原文地址:http://blog.sina.com.cn/s/blog_6bec36f9010110w9.html
相关文章推荐
- Div嵌套引起子元素的margin-top不起作用
- div嵌套引起的margin-top不起作用
- div嵌套引起的margin-top不起作用
- div嵌套引起的margin-top不起作用
- 小菜学习日记—div嵌套导致子区域margin-top失效问题(外边距合并问题)
- div嵌套引起的margin-top不起作用
- div标签中的元素margin-top失效的解决方法
- 子元素用margin-top 为什么反而作用在父元素上?对使用margin-top 的元素本身不起作用?
- div标签中的元素margin-top失效
- 如何解决子div设置margin-top时影响父div的margin-top
- css-子div设置margin-top后,父div与子div一起下移
- 关于DIV多层嵌套的margin-top的BUG
- div margin-top不起作用
- 关于margin-top/bottom在non-Replaced inline元素上(如span)不起作用的解释
- FF浏览器嵌套div中margin-top转移问题的解决办法
- 关于内层DIV设置margin-top不起作用的解决方案
- div嵌套导致的margin-top不起作用
- 为您解惑:关于内层DIV设置margin-top不起作用的解决方案。。。
- 子div设置margin-top后会影响父元素
- FF浏览器嵌套div中margin-top转移问题的解决办法