Javascript : How to get width and height of a div?
Problem :
You have a <div>
and you want to get the dimension of the <div>
such as height and width. You want to ensure that the <div>
dimension is not being tempered by third-party browser's add-ons.
How to do that?
Solution :
Imagine you have a <div id="box">
with id = box. You can get the dimension with getElementById()
function to get the offsetWidth and offsetHeight properties.
For example :
the HTML :
<div id="box">
fill up something here
the more you fill... the higher
the height will be
<br>
<br>
</div>
and the Javascript :
var element = document.getElementById('box');
var height = element.offsetHeight;
alert(height);
var width = element.offsetWidth;
//alert(width)
See demo at http://codepen.io/anon/pen/KdVXLw
IF the height or width is below certain pixels, you can either prompt alert to your website visitors or redirect them to another page.
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+47.8k Golang : How to convert JSON string to map and slice
+7k Golang : Validate credit card example
+17.5k Golang : [json: cannot unmarshal object into Go value of type]
+18.9k Golang : When to use public and private identifier(variable) and how to make the identifier public or private?
+9.1k Golang : Generate random Chinese, Japanese, Korean and other runes
+39k Golang : How to read CSV file
+13.9k Golang : Google Drive API upload and rename example
+24.4k Golang : How to print rune, unicode, utf-8 and non-ASCII CJK(Chinese/Japanese/Korean) characters?
+9.1k Golang : Temperatures conversion example
+14.7k Golang : Basic authentication with .htpasswd file
+42.9k Golang : Convert []byte to image