Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 4 of 4
  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    1
    Member #
    37438

    Why does my negative margin affect both divs?

    Hi all,

    I am very new to CSS and HTML and I have coded the following:

    Code:
    <!DOCTYPE html>
    
    <html>
    <head>
    <link rel="stylesheet" type="text/css" href="css/style1.css">
    
    
    </head>
    <body>
    <div id="titleblock">
    <h1>RSVP FORM</h1>
    <p>Aide memoire invite</p>
    </div>
    <div id="nav1">
    <ul>
    	<li>
    	Home
    	</li>
    	Events
    	<li>
    	Contact
    	</li>
    	<li>
    	</li>
    </ul>
    </div>
    </body>
    </html>
    Code:
    @charset "UTF-8";
    /* CSS Document */
    
    
    /* reset */
    
    
    * {
    margin: 0;
    padding: 0;
    
    /* style screen */
    
    }
    
    body{
    background-color: #aaa;
    margin: 20px;
    font-family: Arial, Georgia;
    }
    
    h1 {
    font-family: Arial, Georgia;
    }
    
    p {
    font-family: Arial, Georgia;
    }
    
    #titleblock{
    width: 200px;
    height: 120px;
    background-color: #fff;
    padding: 20px;
    margin: -20px 0 0 0;
    border-radius: 0px 0px 10px 10px;
    float: left;
    }
    
    #nav1 {
    background-color: #000;
    color: #FFF;
    padding: 10px 20px 10px 20px;
    margin: -20px 0 0 0;
    }
    
    
    ul {
    list-style: none;
    }
    li {
    display: inline;
    padding: 5px 20px 5px 20px;
    }
    My question is both my "titleblock" id and my "nav1" have a -20px top margin but it seems my "nav1" margin affects the "titleblock causing it to have a -40px top margin?

    Does anyone understand why this is happening?

  2.  

  3. #2
    Senior Member Ronald Roe's Avatar
    Join Date
    Mar 2011
    Location
    Oklahoma City
    Posts
    3,141
    Member #
    27197
    Liked
    959 times
    So, here's something: it isn't doubling your margin, it's just adding a top offset of equal height. Now why it's doing that, I'm still trying to figure out...
    Last edited by Ronald Roe; Oct 16th, 2013 at 06:08 AM.
    Ron Roe
    Web Developer
    "If every app were designed using the same design template, oh wait...Bootstrap."

  4. #3
    Senior Member Ronald Roe's Avatar
    Join Date
    Mar 2011
    Location
    Oklahoma City
    Posts
    3,141
    Member #
    27197
    Liked
    959 times
    Did a little research, came across this article: The Definitive Guide to Using Negative Margins | Smashing Coding
    A little ways into the article, find the heading about negative margins on a floated element. Apparently, negative margins on floated elements cause the element to collapse by that amount, which I'm guessing manifests as a negative offset.
    Ron Roe
    Web Developer
    "If every app were designed using the same design template, oh wait...Bootstrap."

  5. #4
    Junior Member
    Join Date
    Oct 2012
    Posts
    6
    Member #
    33551
    I wonder what will happen if you change the negative top-margin for negative top.


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 01:43 AM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com