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
+5.9k Golang : Normalize email to prevent multiple signups example
+12.1k Golang : Read from buffered reader until specific number of bytes
+13.3k Golang : How to filter a map's elements for faster lookup
+5.2k Golang : Experimental emojis or emoticons icons programming language
+12.9k Golang : concatenate(combine) strings
+22.4k Golang : Use regular expression to validate domain name
+10.6k Golang : Convert decimal number(integer) to IPv4 address
+7.7k Golang : Gorilla web tool kit schema example
+16.8k Golang : Check if a directory exist or not
+12.3k Golang : Get dimension(width and height) of image file
+50.1k Golang : Check if item is in slice/array
+4.6k How to check with curl if my website or the asset is gzipped ?