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
+22.3k Golang : Match strings by wildcard patterns with filepath.Match() function
+9.4k Golang : How to get ECDSA curve and parameters data?
+11.2k Golang : Roll the dice example
+6.6k Golang : Convert an executable file into []byte example
+15.7k Golang : Validate hostname
+17.2k Golang : Get input from keyboard
+5.3k Golang : Issue HTTP commands to server and port example
+25.5k Golang : Convert long hexadecimal with strconv.ParseUint example
+8.5k Your page has meta tags in the body instead of the head
+12.1k Golang : Determine if time variables have same calendar day
+12k Golang : How to parse plain email text and process email header?
+14.7k Android Studio : Use image as AlertDialog title with custom layout example