Yash Jain
Yash Jain

Reputation: 822

How to get height from html div element?

I am using angular 5 where in a component I have one method

onFullScreen : function (event){ console.log(event[0]) }

Here, when I do console.log(event[0]), this will return this

enter image description here

This returns a HTMLDivElement, now I want to get the height property in my onFullScreen() method. How to get it?

Upvotes: 2

Views: 5329

Answers (2)

Oscar Paz
Oscar Paz

Reputation: 18292

The best way to get the height of an element (and lots of other layout-related properties) is to use getBoundingClientRect (https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect).

So you can do this:

const height = event[0].getBoundingClientRect().height;

Upvotes: 3

sree kumar
sree kumar

Reputation: 44

You can try this:

var height = document.getElementById('element').style.height;

Since the height is defined in inline styles for this element, this would work.

Warning: this does not work if height is not explicitly defined in css but calculated based on content or outer box.

Upvotes: 1

Related Questions