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
+4.8k Golang : Struct field tags and what is their purpose?
+18.5k Golang : Append content to a file
+4.8k Golang : Use NLP to get sentences for each paragraph example
+10.6k Golang : convert(cast) float to string
+8.4k Golang : Launch Mac OS X Preview (or other OS) application from your program example
+8k Golang : Inject/embed Javascript before sending out to browser example
+5.9k Golang : Takes a plural word and makes it singular
+5.2k Golang : Join lines with certain suffix symbol example
+29.6k error: trying to remove "yum", which is protected
+5.5k Golang : Find the longest line of text example
+31.2k Golang : Regular Expression for alphanumeric and underscore
+7.4k Golang : Add text to image and get OpenCV's X, Y co-ordinates example