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
+19.5k Golang : Count JSON objects and convert to slice/array
+5.3k PHP : Convert string to timestamp or datestamp before storing to database(MariaDB/MySQL)
+15.1k Golang : Delete certain files in a directory
+22.5k Golang : simulate tail -f or read last line from log file example
+12k Golang : Flush and close file created by os.Create and bufio.NewWriter example
+20.1k Nginx + FastCGI + Go Setup.
+8.6k Golang : Gaussian blur on image and camera video feed examples
+6.6k Get Facebook friends working in same company
+11.4k Golang : Surveillance with web camera and OpenCV
+11.7k Golang : Detect user location with HTML5 geo-location
+11.2k Golang : Handle API query by curl with Gorilla Queries example
+6.9k CloudFlare : Another way to get visitor's real IP address