简体   繁体   中英

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 because 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'>&nbsp;
    <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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM