Put 400px wide div horizontally center aligned inside a full width div with top margin
Please see this code
<!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>
I see a gap between body and footer beca开发者_如何学编程use I have put 30px top margin for footer-contents div. Why can I put a div inside another with some top margin, Due to this margin the whole footer div is added margin of 30 px when you see it in browser, I can solve this by adding padding instead of margin, but I want to know what if I want to put a 700px wide div horizontally centered inside a full page 100% wide div, and also I want to add some margin the inner div.
Add some content other than the 'footer-contents' div to the 'footer' div. For example:
<div class='footer'>
<div class='footer-contents'>
Some contents goes here
</div>
</div>
In this case I get the margin applied correctly to the inner div.
http://jsfiddle.net/Fq64H/1/
Hope this helps.
Bob
精彩评论