How to make div's use 100% height witout stretching too much

Discussion in 'CSS' started by fischer, Apr 23, 2007.

  1. #1
    Hello all.

    I have a problem with this site: http://cumdealer.com/csstest/test.html

    My problem is that the "div-1" stretches too much. I only want it to stretch too fill the window height (no scrollbars!), but as you can see it stretches beyond window height.

    I don't know if I have described this probobly, but I hope you understand my problem!

    Thanks in advance,
    fischer.
     
    fischer, Apr 23, 2007 IP
  2. Mr Blonde

    Mr Blonde Guest

    Messages:
    142
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #2
    this is because you have 3 divs, and as 1 is set to 100% height, so the other divs are added onto that height. If you want it to fit the whole height of the page you'll need to make the accumulative total of all the divs make 100%.

    eg:
    #div-1 {
      height:80%;
    }
    #div-before {
      height:10%;
    }
    #div-after {
      height:10%;
    }
    Code (markup):
     
    Mr Blonde, Apr 23, 2007 IP
  3. fischer

    fischer Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    thanks for the answer, I see your point!
    - but can't I set a fixed width in pixels on "div-before" and "div-after"?
    I would like these to have a fixed width and "div-1" to stretch according to window size.
     
    fischer, Apr 23, 2007 IP
  4. Mr Blonde

    Mr Blonde Guest

    Messages:
    142
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #4
    if you want it to behave like a frameset with the top and bottom divs constantly positioned at the top and bottom of the page there's a bit of a trick to it.

    First, layout the html as such...
    <div id="div-1">
    <div id="div-before">text</div>
    --content for div 1 goes underneath div-before--
    </div>
    <div id="div-after">text</div
    Code (markup):
    Then add the following to your css...
    html,body{
        height:100%;
        margin:0;
        padding:0;
    }
    #div-1 {
    min-height:100%;
    margin-top:-25px;/* height of div-after*/
    }
    /* mac hide \*/
    * html #div-1{height:100%}
    /* end hide */
    #div-before {
    margin: 0;
    padding: 0;
    background-color: #000;
    height:25px;
    border-top:25px solid #000;/* height of div-after - soaks up space made by negative margin*/
    }
    #div-after {
    clear:both;
    height:25px;
    }
    Code (markup):
    all this is doing, is adding div-before to be at the top of your page, and making div-1's height 100% minus whatever the pixel height of div-after is.

    hope that's what you were after
     
    Mr Blonde, Apr 23, 2007 IP
  5. kk5st

    kk5st Prominent Member

    Messages:
    3,497
    Likes Received:
    376
    Best Answers:
    29
    Trophy Points:
    335
    #5
    You're thinking print design. A web page doesn't really have a height. It's as tall as it needs to be. Let your content decide how much space it needs.

    In web design, the content must come first. Web design is about what the visitor will see and how he'll get around the site. After you've determined what goes on the page, and how you want the visitor to 'tour' the page, and you've properly marked up the content with semantically correct html, then you can begin the presentation part. It is damned difficult to lay out a well crafted page without content.

    cheers,

    gary
     
    kk5st, Apr 23, 2007 IP
  6. Mr Blonde

    Mr Blonde Guest

    Messages:
    142
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #6
    yeah i agree... i just usually use this method for small pages like error messages etc to create a sticky footer
     
    Mr Blonde, Apr 23, 2007 IP
  7. kk5st

    kk5st Prominent Member

    Messages:
    3,497
    Likes Received:
    376
    Best Answers:
    29
    Trophy Points:
    335
    #7
    Just in case you thought otherwise, my remarks were directed toward the OP. Most newbies, and especially those with graphics training, have little understanding of how a web page works, as opposed to print. It is not easy to get them over the hump, either. ;)

    cheers,

    gary
     
    kk5st, Apr 23, 2007 IP
  8. Mr Blonde

    Mr Blonde Guest

    Messages:
    142
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #8
    yeah i have a mate from a print background getting into web design and know exactly what you mean... im constantly driving articles on semantics and web standards down his throat ;)
     
    Mr Blonde, Apr 23, 2007 IP
  9. fischer

    fischer Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    thank you very much for the answers guys!..
    it was very helpfull!..

    cheers!
     
    fischer, Apr 25, 2007 IP
  10. Rasczak

    Rasczak Peon

    Messages:
    131
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #10
    well, it might be a bit more complicated regarding cross-browser compatibility, so try this layout:

    link
     
    Rasczak, Apr 26, 2007 IP