1

I would like to create a div which fits the full height and width of the browser viewport. I just thought about simple css like:

#wrapper { width: 100%;
           height: 100%;
           background-color: red;
}

but it does not work for the height. The #wrapper does not have any height. Why?

Thanks in advance.

supersize
  • 13,764
  • 18
  • 74
  • 133
  • 2
    You'll need to add `height: 100%;` to the `` and `` elements as well – Adrift Sep 02 '13 at 19:51
  • The question has already been answered here [http://stackoverflow.com/questions/6654958/make-body-have-100-of-the-browser-height][1] [1]: http://stackoverflow.com/questions/6654958/make-body-have-100-of-the-browser-height –  Sep 02 '13 at 19:57
  • you need to set position: absolute; – Ronny K Sep 02 '13 at 19:59

2 Answers2

7
#wrapper {
    position: absolute;
    width: 100%;
    height: 100%;
    background-color: red;
}

http://fiddle.jshell.net/zDuWh/

or

html, body { height: 100% }

#wrapper {
    width: 100%;
    height: 100%;
    background-color: red;
}

http://fiddle.jshell.net/VJv6h/

yckart
  • 32,460
  • 9
  • 122
  • 129
  • exactly what I needed. But could you explain why I have to declare the height in the body and htmls css and the width not? – supersize Sep 02 '13 at 20:02
  • @supersize Sure! A `div` per default a block-element (`display:block`) if your set `display` to `inline` for example this will not work anymore. You can read more about it here: https://developer.mozilla.org/en-US/docs/Web/CSS/display and here: http://dev.w3.org/csswg/css-box/#display – yckart Sep 02 '13 at 20:04
1

How about this:-

#wrapper {
    position: absolute;
    width: 100%;
    height: 100%;
    background-color: red;
}

or

html { height: 100% }
body { height: 100% }
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331