0

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 ?

user1365010
  • 3,185
  • 8
  • 24
  • 43
  • You can get the [computed style](http://stackoverflow.com/questions/6134471/have-problem-when-use-elements-that-added-to-an-array-with-document-getelementb/6134501#6134501) – Ibu May 15 '12 at 17:08

3 Answers3

1

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).

Ben Lee
  • 52,489
  • 13
  • 125
  • 145
1

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.

j08691
  • 204,283
  • 31
  • 260
  • 272
0

if you can use jQuery, then easily: $("#id").width() or .height(). This will also work on most browsers (even very old ones).

Gavriel
  • 18,880
  • 12
  • 68
  • 105