Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Dec 2008
    Posts
    95
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Nested div spacing

    Hello everyone,

    I have a nested div that I want to space vertically from its containing div. However, if i add a margin-top to the nested div, it moves both the nested and containing div, and if I apply padding-top to the nested div it does nothing. If I apply padding to the container div, it works, execpt it moves my footer as well. Any suggestions?

    Here is an example of what I'm working with:

    Code:
    <div id="container">
    
      <div id="nested"></div>
    
    </div>

  • #2
    Master Coder Excavator's Avatar
    Join Date
    Dec 2006
    Location
    Alaska
    Posts
    9,668
    Thanks
    22
    Thanked 1,827 Times in 1,811 Posts
    Hello alexmel7,
    Could we see the entire document with? A link would be even better...
    Hard to say what your doing with your CSS, if have a DocType or if your code is even valid.

    This works -
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    <style type="text/css">
    html, body {
    	font: 14px "Comic Sans MS";
    	background: #FC6;
    }
    * {
    	margin: 0;
    	padding: 0;
    	border: none;
    }
    #container {
    	width: 800px;
    	margin: 30px auto;
    	background: #999;
    	overflow: auto;
    }
    #nested {
    width: 50px;
    height: 50px;
    	margin: 10px 0 0 0;
    	background: #fff;
    }
    </style>
    </head>
    <body>
        <div id="container">
      		<div id="nested"></div>
        <!--end container--></div>
    </body>
    </html>
    Last edited by Excavator; 10-26-2009 at 07:30 PM. Reason: added dimensions to #nested
    Validate often DURING development - Use it like a splelchecker | Debug during Development |Write it for FireFox, ignore IE
    Use the right DocType | Validate your markup | Validate your CSS | Why validating is good | Why tables are bad

  • #3
    New Coder
    Join Date
    Dec 2008
    Posts
    95
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Thanks for the quick reply excavator. By looking at your example I see that all I had to do was add 'overflow: auto' to the container div. Why does this work?

  • #4
    Master Coder Excavator's Avatar
    Join Date
    Dec 2006
    Location
    Alaska
    Posts
    9,668
    Thanks
    22
    Thanked 1,827 Times in 1,811 Posts
    That clears the float and allows #container to wrap #nested.

    Here's a good site that shows how overflow:auto; works http://www.quirksmode.org/css/clearing.html
    Validate often DURING development - Use it like a splelchecker | Debug during Development |Write it for FireFox, ignore IE
    Use the right DocType | Validate your markup | Validate your CSS | Why validating is good | Why tables are bad

  • Users who have thanked Excavator for this post:

    alexmel7 (10-26-2009)

  • #5
    New Coder
    Join Date
    Dec 2008
    Posts
    95
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Awesome. Thanks!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •