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
+18.6k Golang : Implement getters and setters
+27.3k Golang : Convert integer to binary, octal, hexadecimal and back to integer
+6.9k Golang : Find the shortest line of text example
+16.7k Golang : Set up source IP address before making HTTP request
+19.1k Golang : Calculate entire request body length during run time
+8k Golang : Randomize letters from a string example
+5.5k Golang : Shortening import identifier
+10.2k Golang : Simple Jawi(Yawi) to Rumi(Latin/Romanize) converter
+15.5k Chrome : ERR_INSECURE_RESPONSE and allow Chrome browser to load insecure content
+10.5k Golang : Resolve domain name to IP4 and IP6 addresses.
+29.7k Golang : Get and Set User-Agent examples
+11k Golang : Read until certain character to break for loop