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
+6.8k Javascript : Put image into Chrome browser's console
+3.7k Mac OSX : Get disk partitions' size, type and name
+15.6k Golang : Get input from keyboard
+18.3k Golang : Count JSON objects and convert to slice/array
+4.9k Golang : How to search a list of records or data structures
+14.2k Golang : Delete certain files in a directory
+4.9k Facebook : How to force facebook to scrape latest URL link data?
+4.9k Golang : Find change in a combination of coins example
+5.1k Golang : Dealing with backquote
+22.9k Golang : How to print rune, unicode, utf-8 and non-ASCII CJK(Chinese/Japanese/Korean) characters?
+10.4k Golang : Concurrency and goroutine example
+25.8k Golang : Convert CSV data to JSON format and save to file