0

I have tried to set 100% height on the body, it works if there is no content, but with content the body does not fill the page.

<html>
 <body>
 test body
     <div class="item"> asdasdasda sda sda sdas das d</div>
     <div class="item"> asdasdasda sda sda sdas das d</div>
     <div class="item"> asdasdasda sda sda sdas das d</div>
     <div class="item"> asdasdasda sda sda sdas das d</div>
     <div class="item"> asdasdasda sda sda sdas das d</div>
     <div class="item"> asdasdasda sda sda sdas das d</div>
     <div class="item"> asdasdasda sda sda sdas das d</div>
</body>

html {
    background: red;
    margin: 0px;
    padding: 0px;
    height: 100%;
}

body {
    background: white;
    max-width: 320px;
    height: 100%;
    margin: auto;
}

.item {
    height: 150px;
}

I am getting MAD!

JS Fiddle: http://jsfiddle.net/02dknpeo/2/

MeV
  • 3,761
  • 11
  • 45
  • 78

4 Answers4

5

The problem is that the body stays the height of the page, regardless if the content is overflowing or not.
Use min-height so it will be the pages height if the content isn't taller then the window, but will grow to fit the content if it is:

body {
    background: white;
    max-width: 320px;
    min-height: 100%;
    margin: auto;
}

http://jsfiddle.net/bv1aaub6/

Jacob G
  • 13,762
  • 3
  • 47
  • 67
  • Thank you for the solution! it was not working with my code so I have find out that was caused by floating divs. This solution would not work if there are floating divs in the body so I had to replace the float with flex.... unbelievable. – MeV Nov 17 '15 at 15:34
  • @MaRco85 trick with float, you can just set their parents overflow to hidden, in your case `body{overflow:hidden;}`, and the parent will grow to fit them – Jacob G Nov 17 '15 at 15:43
2

Change the height of 100% on the body element to a min-height instead:

Updated Example

In doing so, the body element will always have at least a height of 100% rather than always having a height of 100% of the window (which is what your problem was).

body {
    background: white;
    max-width: 320px;
    min-height: 100%;
    margin: auto;
}
Josh Crozier
  • 233,099
  • 56
  • 391
  • 304
  • Thank you for the solution! it was not working with my code so I have find out that was caused by floating divs. This solution would not work if there are floating divs in the body so I had to replace the float with flex.... unbelievable. – MeV Nov 17 '15 at 15:34
1

You're restricting the height of the body element to the height of the viewport. Don't.

body {
    /* height: 100% */
}

If you need it to be at least that high, use min-height.

Demo

isherwood
  • 58,414
  • 16
  • 114
  • 157
  • Thank you for the solution! it was not working with my code so I have find out that was caused by floating divs. This solution would not work if there are floating divs in the body so I had to replace the float with flex.... unbelievable. – MeV Nov 17 '15 at 15:33
1

Just remove the height:100% from the body rule.

jsFiddle example

j08691
  • 204,283
  • 31
  • 260
  • 272
  • Thank you for the solution! it was not working with my code so I have find out that was caused by floating divs. This solution would not work if there are floating divs in the body so I had to replace the float with flex.... unbelievable. – MeV Nov 17 '15 at 15:34