css layout- nested div margins effect the containing div

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • crazeway
    New Member
    • Feb 2008
    • 6

    css layout- nested div margins effect the containing div

    The top margin of a nested div (#main) is pulling down its containing div (#middle) along with the 2 floated divs flanking it. This problem corrects itself and gives me the desired effect if I add a border to #middle. However I'd like to understand the problem and do away with the border. Here is a test page I made to illustrate my problem

    [CODE=html]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

    <html>
    <head>
    <style>
    #wrap {width: 900px;}

    #head {
    background: green;
    height: 60px;}

    #middle {background: blue;
    height: 800px;
    }

    #left {
    float: left;
    width: 100px;
    background: purple;
    height: 600px;
    }

    #right {
    float: right;
    width: 100px;
    background: red;
    height: 600px;
    }

    #main {
    margin: 60px 110px 0 110px;
    background: orange;
    height: 700px;
    }

    #foot {background: green;
    height: 60px;
    }
    </style>
    </head>
    <body>
    <div id="wrap">
    <div id="head"></div>
    <div id="middle">
    <div id="left"></div>
    <div id="right"></div>
    <div id="main"></div>
    </div>
    <div id="foot"></div>
    </div>
    </body>
    </html>[/CODE]
    Last edited by eWish; Feb 14 '08, 01:29 AM. Reason: Please use [CODE][/CODE] tags.
  • drhowarddrfine
    Recognized Expert Expert
    • Sep 2006
    • 7434

    #2
    I'm trying to go to bed.

    Always remember that parent elements are never to expand to contain their floated elements. IE does this but it's a bug. In any case, adding 'overflow:auto' to #middle causes it to expand to contain these elements that "overflow" it.

    I'd explain more as to why it's happening there but I'm literally woozy. Hope you see it so I don't have to go through it again in the morning. :)

    Comment

    • crazeway
      New Member
      • Feb 2008
      • 6

      #3
      I dont see how #main, #left or #right are overflowing #middle. #left and #right have defined widths of 100px, are floated and so they do not overflow #middle. With #main having left and right margins of 110px, it winds up between the two floats, its width then being determined by the width of #wrap and also does not overflow #middle. Even heightwise, #main with its additional 60px top margin is still shorter than #middle.

      I also don't see how adding a border would correct the problem if it was due to overflow.

      Comment

      • Death Slaught
        Top Contributor
        • Aug 2007
        • 1137

        #4
        It's your 'main' division. Its margin-top is pushing your header division up. Using overflow:auto; like drhowarddrfine said will fix your problem because your main division top margin is overflowing your middle division. Your over option would be to remove the top margin in your 'main' division. So here are your two options:

        Remove space and keep margin.

        [CODE=css]#middle {
        background: blue;
        height: 800px;
        overflow:auto;
        }[/CODE]

        Remove space and margin.

        [CODE=css]#main {
        margin: 0px 110px 0 110px;
        background: orange;
        height: 700px;
        }[/CODE]

        Thanks,
        {\_/}
        (' . ')
        (")[DEATH](")
        (")(")

        Comment

        • crazeway
          New Member
          • Feb 2008
          • 6

          #5
          Thanks for the fix. I do want the 60px margin, making #main lower than the floats. Even though it works, I still don't understand why. Can someone explain why overflow is necessary here. I would expect it to render the way it does without overflow if I had given a 60px margin-top to #middle, not #main. I'm not understanding why #main's 60px margin-top does not begin at the top edge of #middle, since #main is nested in #middle

          curiously enough, I just looked at this page in ie6 and it renders the way I want without overflow

          Comment

          • drhowarddrfine
            Recognized Expert Expert
            • Sep 2006
            • 7434

            #6
            It renders the way you want but you are witnessing an IE bug. Parent elements are never to expand to contain floated elements. So overflow is required by modern browsers so the box expands to contain an element "overflowin g" out of itself.

            Comment

            • drhowarddrfine
              Recognized Expert Expert
              • Sep 2006
              • 7434

              #7
              For example, insert an image into the markup below and view in Firefox and IE. This is typical of what might be found on the web. It doesn't work in IE. (Find an image to insert instead of "1.png")
              [HTML]<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
              "http://www.w3.org/TR/html4/strict.dtd">
              <html>
              <head>
              <title></title>
              <style type="text/css">
              img{float:left}
              </style>
              </head>

              <body>

              <p><img src="1.png">Lor em ipsum dolor sit amet, consectetuer adipiscing elit.
              </p>

              <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean auctor consectetuer sem. Proin quis nisl venenatis neque consectetuer tincidunt. Fusce quis nibh non diam imperdiet facilisis. Pellentesque vitae mauris. Phasellus scelerisque arcu sed purus. Ut at enim. Mauris ornare, sem vel lacinia condimentum, augue lorem molestie orci, vel rhoncus lorem sem ut risus. Cras venenatis adipiscing ipsum. Proin eros. Proin id est. Sed eu arcu quis arcu faucibus congue. Cras congue sapien vel purus. Duis mi quam, tristique non, imperdiet non, rhoncus et, nunc. Sed nisl.
              </p>

              </body>
              </html>[/HTML]

              Comment

              • crazeway
                New Member
                • Feb 2008
                • 6

                #8
                I understand how floats and their container's expansion works and why overflow is necessary in that situation. I think I am describing a different question, but maybe I'm not seeing the relation. I'm not trying to give props to ie or anything like that. The problem occurs in Opera, Safari, Firefox, and I haven't yet tried ie7.

                Based on my understandingsi nce #main isn't floated, #middle should expand to accomodate it (although this is not even an issue since I gave #middle a height bigger than #main.)

                My Main Problem
                What perplexes me is why the margin-top of #main does not start at the top edge of #middle, since #main is nested in #middle (and not floated)

                or, put another way

                why does adding a border to #middle suddenly make #main's margin-top start at the top edge of #middle, where without it, it seems to transfer its margin-top property to #middle

                Comment

                • drhowarddrfine
                  Recognized Expert Expert
                  • Sep 2006
                  • 7434

                  #9
                  Some things are just best left unsaid. You don't have a right to know these things. Is that your momma callin'? I believe it is. You better git home now. But I think it's best to not breath a word of this. Ya never know who's listenin'. And ya' don't wanna get hurt...ya' know?

                  But seriously follks......

                  The margin is there but it's poking out the top. Why it's doing that I haven't discovered yet. For some reason, it's acting like there are floats or abs pos elements. I thought it might be connected to the order of placement of the floats but moving them around didn't change anything.

                  I'm not stumped. I just need to take some more time to look into this.

                  Did you say this happens in all browsers but IE?

                  Comment

                  • drhowarddrfine
                    Recognized Expert Expert
                    • Sep 2006
                    • 7434

                    #10
                    Ok, problem solved. I was focused too much on the floats. I can't remember the rule that guides this but the margin for #main is sticking out of the containing box. Maybe that's what you know and are saying. I just need to look at the rule regarding that. I'm sure it's related to collapsing margins.

                    Comment

                    • drhowarddrfine
                      Recognized Expert Expert
                      • Sep 2006
                      • 7434

                      #11
                      Yep. This gets me every time if I'm not thinking about it. Collapsing margins. . The relation between the parent/child elements where the child can pass through the parent element.

                      Comment

                      Working...