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
+3.4k Golang : Handle Palindrome string with case sensitivity and unicode
+2.8k Golang : Handling image beyond OpenCV video capture boundary
+2.5k Golang : Populate slice with sequential integers example
+3.1k Get Facebook friends working in same company
+17.1k Golang : Regular Expression for alphanumeric and underscore
+3k Android Studio : AlertDialog to get user attention example
+2.8k Golang : Shuffle array of list
+16.2k Golang : Read directory content with filepath.Walk()
904 Golang : Customize scanner.Scanner to treat dash as part of identifier
+6.6k Golang : Read file and convert content to string
+4.2k Golang : Inject/embed Javascript before sending out to browser example
+2.3k Linux/Unix/PHP : Restart PHP-FPM