-1

In html I assigned div height as 100% in inline css,when loading height of div is changed as 0. Why its happen? refer this http://jsfiddle.net/CLjH3/3/

This is my code sample:

display();

function display() {
  var elementHeight = $("#container").height();

  alert("ElementHeight :" + elementHeight);

  var elementWidth = $("#container").width();

  alert("ElementWidth :" + elementWidth);
}
<head>
  <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
</head>

<body>
  <div id="container" style="height:100%;width:100%;border:solid 1px red;">
  </div>
</body>
Andrzej Ziółek
  • 2,241
  • 1
  • 13
  • 21
mani
  • 11
  • 2

2 Answers2

0

Set body and html height:

body, html {
    height: 100%;
}
A. Wolff
  • 74,033
  • 9
  • 94
  • 155
0

Demo

In order for a percentage value to work for height, the parent's height must be determined. The only exception is the root element <html>, which can be a percentage height. .

So, you've given all of your elements height, except for the <html>, so what you should do is add this:

html, body {
    height: 100%;
}

From the Docs -

The is calculated with respect to the height of the containing block. If the height of the containing block is not specified explicitly, the value computes to auto. A percentage height on the root element (e.g. ) is relative to the initial containing block (whose dimensions are equal to the dimensions of the viewport).

Check more on this at - MDN Percent Doc

sources one two

Community
  • 1
  • 1
4dgaurav
  • 11,360
  • 4
  • 32
  • 59