SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Thread: Unwanted top margin

  1. #1
    SitePoint Zealot Macchiato's Avatar
    Join Date
    Nov 2009
    Posts
    149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unwanted top margin

    I've set a top margin of 5em to a Div element that holds a background image. This Div element is within an other Div element. The top margin of 5em is being assigned to both Div elements, but I only want it to be assigned to the Div element that holds a background image. How can I fix this problem?

    Code CSS:
    body
    {
    padding:0px;
    margin:0px;
    }
     
    div#wrapper
    {
    margin:0 auto;
    width:70em;
    }
     
    div#main
    {
    background-color:SteelBlue;
    width:70em;
    height:70em;
    }
     
    div#image
    {
    width:201px;
    height:201px;
    background:url(../_images/image.png);
    margin:5em auto 0em auto;
    }


    HTML Code:
    <body>
    
    <div id="wrapper">
      <div id="main">
        <div id="image"></div>
      </div>
    </div>
    
    </body>
    Thanks in advance

  2. #2
    It's all Geek to me silver trophybronze trophy
    SitePoint Award Recipient ralph.m's Avatar
    Join Date
    Mar 2009
    Location
    Melbourne, Australia
    Posts
    20,311
    Mentioned
    225 Post(s)
    Tagged
    3 Thread(s)
    Hi again Macchiato,

    this is a case of "collapsing margins", where the margin of an inner element pokes out of its containing element.

    Two easy ways to get around this are either

    Code:
    div#main {
    background-color:SteelBlue;
    width:70em;
    height:70em;
    padding-top:1px;
    }
    or
    Code:
    div#main {
    background-color:SteelBlue;
    width:70em;
    height:70em;
    border-top:1px solid SteelBlue;
    }
    Take your pick!
    Facebook | Google+ | Twitter | Web Design Tips | Free Contact Form

    Try your hand at the new JavaScript Challenge!

    If you don't like getting your feet stuck in a bog, avoid Twitter BootsTrap.

  3. #3
    SitePoint Zealot Macchiato's Avatar
    Join Date
    Nov 2009
    Posts
    149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Ralph! That did the trick

Bookmarks

Posting Permissions

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