DIV+CSS中DIV嵌套margin_top不起作用的解决方法
2012-11-05 01:22
453 查看
网站排版中DIV+CSS中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="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;
在这两个浏览器中,有两个嵌套关系的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;
相关文章推荐
- div嵌套margin-top失效的解决方法
- DIV+CSS兼容性解决IE6/IE7/FF浏览器的通用方法
- 浏览器兼容:Web标准化建站(DIV+CSS)的常见问题解决方法
- div+css布局遇到的一些问题和解决方法
- div+css浏览器兼容问题解决方法
- css div设置positon:relative之后,再设置top,div的原位置有空白,解决办法
- 网站常见问题及解决方法(div/css)
- Equal Height Columns --DIV+CSS布局中自适应高度的解决方法
- css中height:100%不起作用的解决方法
- DIV+CSS网页错位原因解决方法
- FireFox下设置DIV的top和left无效解决方法
- div+css总结—FF下div不设置高度背景颜色或外边框不能显示的解决方法
- div+css浏览器兼容问题解决方法
- 网站常见问题及解决方法(div/css)
- 在CSS中span标签设置padding-top/bottom以及margin-top/bottom无效的解决方法
- css定义div的宽度高度要减去多少?解决方法
- css 关于"浮动边距加倍"及其解决方法-------解决方案是在这个div里面加上display:inline;
- div+css总结—FF下div不设置高度背景颜色或外边框不能显示的解决方法 (新浪博客 )
- 最新整理45个div+css兼容性问题与解决方法
- CSS---关于内层DIV设置margin-top不起作用的解决方案