Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why browser is returning empty string on style.height ? How to get actual height of an element?

Simple one line of html:

<div onclick="alert(this.style.height)">sometext</div>

and alert gives:

but it should be like 10px or sth like that.

like image 411
rsk82 Avatar asked Jan 10 '11 11:01

rsk82


People also ask

How do I get the height of an element in CSS?

height() It returns the current height of the element. It does not include the padding, border or margin. It always returns a unit-less pixel value. Note: The height() will always return the content height, regardless of the value of CSS box-sizing property.

How do you find the height of an element?

In JavaScript, you can use the clientHeight property, which returns an element's height, including its vertical padding. Basically, it returns the actual space used by the displayed content. For example, the following code returns 120, which is equal to the original height plus the vertical padding.

How do I get the content height in HTML?

var height = document. getElementById('content'). clientHeight; That will not include any borders or scrollbars in the element, just padding.

How is Dom height calculated?

Use offsetWidth & offsetHeight properties of the DOM element to get its the width and height.


1 Answers

When you use this.style.height, the height must have been specified on the element first, like this:

<div style="height: 15px;" onclick="alert(this.style.height)">sometext</div>

Otherwise, you should probably use offsetHeight or clientHeight:

<div onclick="alert(this.offsetHeight)">sometext</div>
like image 145
DanneManne Avatar answered Sep 20 '22 08:09

DanneManne