SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Member
    Join Date
    Mar 2010
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Nested Div Overflows

    Ok, here it goes. I have a project that is certainly more important than showing colorful boxes, but I can't seem to figure out why divs overflow the way that they do when they are nested. I have layed out a simple example and hopefully someone can explain to me why things happen this way, because if I grasp this I believe I can solve my more important problems.

    Here is the code:
    Code:
    <div style="background: green; width: 70px; height: 50px;">
          <div style="width: 60px; height 50px; background: yellow;">
            <div style="width: 50px; height 50px; background: orange;">
              test 1 <br/>
              test 1 <br/>
              test 1 <br/>
              test 1 <br/>
              test 1 <br/>
              test 1 <br/>
              test 1 <br/>
            </div>
          </div>
        </div>
        <div style="width: 50px; height 50px; background: red;">
          test 2
        </div>
    Here is what it looks like in Chrome, Firefox, etc:


    Here is what it looks like in IE:



    I have a few questions that Google refuses to deliver to my doorstep.

    1. I understand why the Chrome/Firefox yellow box overflows past the height of the green box. This is because of the default overflow: visible, right?

    2. Why does the green box expand in IE? This is the crux of the problem because I want my orange box to "run into" my red box (just like the
    Chrome/Firefox example). I think it has to do with the green box expanding.

    3. Why doesn't the orange box overflow outside of a 50px high yellow box in either browser? This is probably the second most important question. I can't find any explanation for this.

    4. Is there a work-around for the sticky IE mess?

  2. #2
    Ripe Tomatos silver trophybronze trophy Rayzur's Avatar
    Join Date
    Jun 2007
    Location
    Texas
    Posts
    4,174
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Hi,
    For starters you have some errors in the code you posted.
    You are missing the colon after height on two of your divs.

    height 50px;

    It should be -
    height: 50px;

    1. I understand why the Chrome/Firefox yellow box overflows past the height of the green box. This is because of the default overflow: visible, right?
    That is correct, but once you fix your errors it will not render as your screenshot shows it.

    The only difference I see in IE when the code is corrected is that it stacks the last div (with red BG) above the innermost nested div.

    That can be made to render like FF by setting position:relative on that innermost div for IE.

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html lang="en">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Test</title>
    
    <style type="text/css">
    
    #wrap {
        width:70px;
        height:50px;
        background:green; 
    }
    #a {
        width:60px; 
        height:50px; 
        background:yellow;
    }
    #b {
        width:50px; 
        height:50px; 
        background:orange;
        position:relative;
    }
    #c {
        width:50px; 
        height:50px; 
        background:red;
    }
    </style>
    
    </head>
    <body>
    
    <div id="wrap">
        <div id="a">
            <div id="b">
                test 1 <br>
                test 1 <br>
                test 1 <br>
                test 1 <br>
                test 1 <br>
                test 1 <br>
                test 1 <br>
            </div>
        </div>
    </div>
    <div id="c">
        test 2
    </div>
    
    </body>
    </html>

  3. #3
    Ripe Tomatos silver trophybronze trophy Rayzur's Avatar
    Join Date
    Jun 2007
    Location
    Texas
    Posts
    4,174
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    2. Why does the green box expand in IE?
    4. Is there a work-around for the sticky IE mess?
    Are you sure that screenshot was not taken in IE6?

    The green box had the height coded properly and I did not see that happen in IE7, even with the other height errors.

    IE6 treats height as min-height and that is why it would expand the green box. The fix for that would be overflow:hidden;

  4. #4
    SitePoint Member
    Join Date
    Mar 2010
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm so embarrassed that I had those errors. I blame copy and paste and I'm truly sorry for wasting your time on that.

    Fixing the syntax errors actually made things perform in firefox like I assumed they would, so for that I am satisfied that my intuition was correct.

    However for IE7, the code that you provided shows like this, with or without relative positioning:


    I am positive this was in IE7, and I'm pretty sure it happens in IE8 also. The overflow hidden is not what I'm looking for. I'm ultimately looking for the orange box to cover up the red box, without resizing the the green or yellow boxes. Can that be done in any way?

  5. #5
    Ripe Tomatos silver trophybronze trophy Rayzur's Avatar
    Join Date
    Jun 2007
    Location
    Texas
    Posts
    4,174
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by vinumber View Post
    I am positive this was in IE7, and I'm pretty sure it happens in IE8 also.
    Are you using a native copy of IE7? Because when I run IE7 via IEtester or IE8/Developer tools it does not render as your screenshot shows. IE7 and IE8 both render it just like FF for me.

    I'm ultimately looking for the orange box to cover up the red box, without resizing the the green or yellow boxes. Can that be done in any way?
    Well if you can't use overflow:hidden then you will need to remove the orange div from the page flow by setting it as position:absolute;

    You will need to remove the height as well if you want the orange BG to cover the red div.

    I set a 5px left margin on the red div to let it peek through from beneath the orange div.

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html lang="en">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Test</title>
    
    <style type="text/css">
    
    #wrap {
        width:70px;
        height:50px;
        background:green; 
    }
    #a {
        width:60px; 
        height:50px; 
        background:yellow;
        position:relative; /*set containing block for #b div*/
    }
    #b {
        width:50px; 
        /*height:50px;*/
        position:absolute;
        top:0;left:0; 
        background:orange;
    }
    #c {
        width:50px; 
        height:50px; 
        background:red;
        margin-left:5px;
    }
    </style>
    
    </head>
    <body>
    
    <div id="wrap">
        <div id="a">
            <div id="b">
                test 1 <br>
                test 1 <br>
                test 1 <br>
                test 1 <br>
                test 1 <br>
                test 1 <br>
                test 1 <br>
            </div>
        </div>
    </div>
    <div id="c">
        test 2
    </div>
    
    </body>
    </html>

  6. #6
    Ripe Tomatos silver trophybronze trophy Rayzur's Avatar
    Join Date
    Jun 2007
    Location
    Texas
    Posts
    4,174
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    To keep IE6 from stretcing the div heights with the nested divs you had I would be inclined to go about it a different way.

    All the divs you want behind the orange div with fixed widths/heights I would absolute position them and I would not nest other divs in them. Stack them just like you want them to appear on the page.

    I would wrap them all in a parent div without a height and leave the last orange div as a static block.

    Like this -

    Code HTML4Strict:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html lang="en">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Test</title>
     
    <style type="text/css">
    #wrap {
        width:70px;
        background:#CDF;
        position:relative;/*establish containing block for AP children*/
    }
    #a {
        width:70px;
        height:50px;
        position:absolute;
        top:0;
        left:0;  
        background:green;
    }
    #b {
        width:60px; 
        height:50px;
        position:absolute;
        top:0;
        left:0;  
        background:yellow;
    }
    #c {
        width:50px; 
        height:50px;
        position:absolute;
        top:50px;
        left:5px;  
        background:red;
    }
    #d {
        width:50px; 
        background:orange;
        position:relative;/*stack above AP divs*/
    }
    </style>
     
    </head>
    <body>
     
    <div id="wrap">
        <div id="a">green div</div>        
        <div id="b">yellow div</div>
        <div id="c">red div</div>
        <div id="d">
            test 1 <br>
            test 1 <br>
            test 1 <br>
            test 1 <br>
            test 1 <br>
            test 1 <br>
            test 1 <br>
        </div>
    </div>
    </body>
    </html>

  7. #7
    SitePoint Member
    Join Date
    Mar 2010
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Are you using a native copy of IE7? Because when I run IE7 via IEtester or IE8/Developer tools it does not render as your screenshot shows. IE7 and IE8 both render it just like FF for me.
    Yes, I am using a native copy. I just checked with someone using a native copy of IE8 and it still appears like my screen shot.

    I figured it out.... My problem was my doc type declaration wasn't at the very top. My bad.


    Can you explain why putting the position to relative causes the div to appear to be z-indexed on top? In other words, why does setting to relative somehow make it cover everything else?

  8. #8
    billycundiff{float:left;} silver trophybronze trophy RyanReese's Avatar
    Join Date
    Oct 2008
    Location
    Whiteford, Maryland, United States
    Posts
    13,724
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    Because position:relative basically makes an element sit on top of other non positioned elements

    http://www.w3.org/TR/CSS21/visuren.h...ve-positioning
    Always looking for web design/development work.
    http://www.CodeFundamentals.com

  9. #9
    Ripe Tomatos silver trophybronze trophy Rayzur's Avatar
    Join Date
    Jun 2007
    Location
    Texas
    Posts
    4,174
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by vinumber View Post
    I figured it out.... My problem was my doc type ....

    Can you explain why putting the position to relative causes the div to appear to be z-indexed on top? In other words, why does setting to relative somehow make it cover everything else?
    Ah yes, you were in quirksmode. No wonder that was happening.

    Positioned elements auto stack according to their order in the html. With the last one in the source coming out on top of the stack.

    I had all the other fixed dimension divs as AP so I just added RP to that last orange div to keep the stacking order going. To sum it up, I had every div positioned but that last one was not removed from the page flow.
    That allows it to expand the parent div (#wrap).

  10. #10
    SitePoint Member
    Join Date
    Mar 2010
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks everyone. Learned a lot!


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
  •