幻想编程

div+css网页布局设计新开端(5)

时间:14-03-09 20:51:26点击:425

下面说说内外边距合并的问题

<html>

<head>

<style type="text/css"> #a{

width:100px;

height:50px;

background:red;

margin-bottom:20px;

}

#b{

width:100px;

height:50px;

background:green

}

</style> <head>

<body> <div id="a"></div>

<div id="b"></div> </body>

</html>

a层的下外边距是20px

下面看图


下面把绿色的层上外边距改为10px

<html>

<head>

<style type="text/css"> #a{

width:100px;

height:50px;

background:red;

margin-bottom:20px;

}

#b{

width:100px;

height:50px;

background:green;

margin-top:10px;

}

</style> <head>

<body> <div id="a"></div>

<div id="b"></div> </body>

</html>


没任何区别

简单地说,外边距合并指的是,当两个垂直外边距相遇时,它们将形成一个外边距。合并后的外边距的高度等于两个发生合并的外边距的高度中的较大者。

在看图


如果两者一样的px怎么办?假如都是20px

<html>

<head>

<style type="text/css"> #a{

width:100px;

height:50px;

background:red;

margin-bottom:20px;

}

#b{

width:100px;

height:50px;

background:green;

margin-top:20px;

}

</style> <head>

<body> <div id="a"></div>

<div id="b"></div> </body>

</html>

效果还是一样哦



下面测试一下

<html>

<head>

<style type="text/css"> #a{

width:100px;

height:100px;

background:red;

margin-top:20px;

}

#b{

width:50px;

height:50px;

background:green;

margin-top:10px;

}

</style> <head>

<body> <div id="a"><div id="b"></div></div>

</body>

</html>

ie6的显示


火狐的显示


可以看出ie6是不合并的,火狐的合并的

可能有人会好奇

这个为什么红色没有贴紧上面的浏览框

其实没有浏览器都有默认的内外边距的


只需要

<html>

<head>

<style type="text/css">

*{

margin:0;

padding:0;

} #a{

width:100px;

height:50px;

background:red;

margin-bottom:20px;

}

#b{

width:100px;

height:50px;

background:green;

margin-top:10px;

}

</style> <head>

<body> <div id="a"></div>

<div id="b"></div> </body>

</html>



有些浏览器可能要用

body{

margin:0;

padding:0;

}