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
+8.4k Golang : Scramble and unscramble text message by randomly replacing words
+16.4k Golang : Multi threading or run two processes or more example
+13.2k Golang : syscall.Socket example
+3.9k Mac OSX : Get disk partitions' size, type and name
+23.6k Golang : Daemonizing a simple web server process example
+23.5k Golang : GORM read from database example
+9.7k Golang : Generate 403 Forbidden to protect a page or prevent indexing by search engine
+13.4k Golang : Recombine chunked files example
+5.3k PageSpeed : Clear or flush cache on web server
+5k Get website traffic ranking with Similar Web or Alexa
+4.3k Golang : Display packages names during compilation
+11.5k Golang : How to display image file or expose CSS, JS files from localhost?