【发布时间】:2011-07-18 03:34:16
【问题描述】:
请看这段代码
<!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=iso-8859-1" />
<title>Untitled Document</title>
<style type="text/css">
<!--
.header {
height: 200px;
background-color: #CCCCCC;
text-align: center;
}
.contents {
background-color: #0099FF;
text-align: center;
height: 400px;
}
.footer {
background-color: #993300;
height: 100px;
}
.footer .footer-contents {
margin-top: 30px;
margin-right: auto;
margin-bottom: 30px;
margin-left: auto;
width: 700px;
}
-->
</style>
</head>
<body>
<div class="header">Header</div>
<div class="contents"> Body </div>
<div class="footer">
<div class="footer-contents">
Some contents goes here
</div>
</div>
</body>
</html>
我看到正文和页脚之间存在间隙,因为我为页脚内容 div 设置了 30 像素的上边距。为什么我可以将一个 div 放在另一个有一些上边距的地方,由于这个边距,当你在浏览器中看到它时,整个页脚 div 添加了 30 px 的边距,我可以通过添加填充而不是边距来解决这个问题,但我想知道如果我想将一个 700px 宽的 div 水平居中放置在一个 100% 宽的整页 div 中,并且我想在内部 div 中添加一些边距。
【问题讨论】: