Question regarding DIVs and margin

Discussion in 'CSS' started by NosZHi, Sep 23, 2010.

  1. #1
    Hi

    Here's a simple example of html & css code:

    <div id="box1">
    <div id="box2">
    text
    </div>
    </div>

    #box1 { height: 500px; background: red; }
    #box2 { margin-top: 50px; }

    First of all, this is not a request for workarounds or other ways of coding (padding, position, margin on different elements and other things will do the trick).

    My question is... why does the box2 margin pushes down the box1 div creating a white 50px background instead of leaving it red? Isn't that margin suppose to expand inside the box1 div, acting just like a padding for box1 div?
     
    NosZHi, Sep 23, 2010 IP
  2. drhowarddrfine

    drhowarddrfine Peon

    Messages:
    5,428
    Likes Received:
    95
    Best Answers:
    7
    Trophy Points:
    0
    #2
    This is related to "collapsing margins" which are put in place in order to give proper spacing between elements as you view them down the page. 'box1's margins are collapsed but this doesn't affect box2. To see what I mean, put 'outline: 1px solid' for box1 and see what happens. (I'm typing this too fast cause I'm leaving. HOpe it makes sense.)
     
    drhowarddrfine, Sep 23, 2010 IP
  3. pmek

    pmek Guest

    Messages:
    101
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #3
    If you wanted to resolve this issue, you need to clear the inner div. Try this CSS:

    #box1 { height: 500px; background: red; overflow: hidden }
    #box2 { margin-top: 50px; float: left }
     
    pmek, Sep 24, 2010 IP
  4. drhowarddrfine

    drhowarddrfine Peon

    Messages:
    5,428
    Likes Received:
    95
    Best Answers:
    7
    Trophy Points:
    0
    #4
    Clearing is for floated elements. He's not floating anything so your solution makes no sense. It's not the issue anyway.
     
    drhowarddrfine, Sep 24, 2010 IP
  5. pmek

    pmek Guest

    Messages:
    101
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Actually, my solution makes perfect sense. You'll notice in the CSS I offered I floated the div...
     
    pmek, Sep 24, 2010 IP
  6. drhowarddrfine

    drhowarddrfine Peon

    Messages:
    5,428
    Likes Received:
    95
    Best Answers:
    7
    Trophy Points:
    0
    #6
    You are stating that the solution to the problem is to clear the div but the problem occurs without a float so introducing a float then saying clearing the float is the solution doesn't make sense. Besides a collapsing margin is the issue, not floats.
     
    drhowarddrfine, Sep 24, 2010 IP
  7. pmek

    pmek Guest

    Messages:
    101
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I realise that. However, my solution will solve his problem.
     
    pmek, Sep 24, 2010 IP
  8. NosZHi

    NosZHi Peon

    Messages:
    27
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Thank you for your replies.
    As I said in my first post, I don't want solutions for that "problem". I just want to understand why that margin affects div1.
    I read more about collapsing margin and it makes (some) sense.
     
    NosZHi, Sep 24, 2010 IP
  9. david.rew1164

    david.rew1164 Peon

    Messages:
    55
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    i think if you could just use float to ur code it would work out fine. Just try it out.

    Thanks
     
    david.rew1164, Sep 30, 2010 IP
  10. anupviews

    anupviews Member

    Messages:
    795
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    35
    #10
    margin is the external invisible boundary while padding is internal boundary.

    Take an example of your body.

    The clothe you are wearing is your margin.
    And the negligible gap that you have between your skin and cloth is the padding.
     
    anupviews, Oct 7, 2010 IP
  11. david.rew1164

    david.rew1164 Peon

    Messages:
    55
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Good explanation
     
    david.rew1164, Oct 7, 2010 IP
  12. CSM

    CSM Active Member

    Messages:
    1,047
    Likes Received:
    25
    Best Answers:
    0
    Trophy Points:
    55
    #12
    He did not ask for solutions @pmek

    He wants to understand the whole thing. You did not provide any explanation, just a solution to a problem that was no problem.
    Adding a float/clear to his code was not the question.

    drhowarddrfine is correct about collapsing margin.
     
    CSM, Oct 7, 2010 IP