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
+15.5k Golang : Gzip file example
+14.3k Golang : Intercept Ctrl-C interrupt or kill signal and determine the signal type
+7.9k Golang : Handle sub domain with Gin
+5.4k PHP : Proper way to get UTF-8 character or string length
+4.5k Javascript : How to loop over and parse JSON data?
+4.6k Golang : fmt.Println prints out empty data from struct
+10k Android Studio : Create custom icons for your application example
+5.1k Golang : Experimenting with the Rejang script
+18.4k Golang : Convert(cast) bytes.Buffer or bytes.NewBuffer type to io.Reader
+10.7k Golang : How to detect a server/machine network interface capabilities?
+29.8k Golang : Get local IP and MAC address
+4.9k Golang : Denco multiplexer example