I know that we can have the absolute position of a div even if we don't set it (by using offset) : http://www.quirksmode.org/js/findpos.html
Can we get the width (or height) of a div even if we don't specify it ?
I know that we can have the absolute position of a div even if we don't set it (by using offset) : http://www.quirksmode.org/js/findpos.html
Can we get the width (or height) of a div even if we don't specify it ?
You can use offsetHeight
or offsetWidth
, like this:
var elem_height = document.getElementById('elem').offsetHeight;
var elem_width = document.getElementById('elem').offsetWidth;
This will return the dimensions including border and padding (but not margin).
You can use clientWidth and offsetWidth to get the width.
var foo = document.getElementById('foo').clientWidth;
var foo = document.getElementById('foo').offsetWidth;
clientWidth returns the viewable width of an element but does not include borders, margins, or scrollbars. offsetWidth returns the width of an element (including borders and padding), but not margins.
if you can use jQuery, then easily: $("#id").width() or .height(). This will also work on most browsers (even very old ones).