2

I have tried this and it just brings an empty space in console of my browser. How can can i get the height of my element.

var background = document.getElementById('bgimage');
console.log(background.style.height);
.background {
  width: 100%;
  height: 500vh;
}
<div class="section s1">
  <div class="background" id="bgimage">
    .......
  </div>
</div>
  • 2
    Does this answer your question? [How do you get the rendered height of an element?](https://stackoverflow.com/questions/526347/how-do-you-get-the-rendered-height-of-an-element) – BadPiggie Nov 30 '19 at 10:04
  • `element.style.height` refers to ***inline** style only*. – connexo Nov 30 '19 at 15:12

3 Answers3

2

An element's clientHeight is a measurement that includes the element CSS height and the element vertical padding and, if rendered, subtracting the height of the horizontal scrollbar.

try background.clientHeight

2

Use offsetHeight:

var background = document.getElementById('bgimage');
var h = Math.max(document.documentElement.clientHeight, window.innerHeight || 0);
console.log(background.offsetHeight*100/h);
.background {
width: 100%;
height: 500vh;
}
<div class="section s1">
<div class="background" id="bgimage">
   .......
</div>
</div>

Note: element.offsetHeight will return height of element in pixels (px).

Ritesh Khandekar
  • 3,885
  • 3
  • 15
  • 30
0

Just fix your javascript code

var background = document.getElementById('bgimage');
var bgimage = getComputedStyle(background);
console.log(bgimage.getPropertyValue('height'));
  • can i get this value by (vh); –  Nov 30 '19 at 11:35
  • This one might help you. https://stackoverflow.com/questions/28295072/how-can-i-convert-px-to-vw-in-javascript –  Nov 30 '19 at 13:36