[thelist] Finding the position of a relative DIV

Hassan Schroeder hassan at webtuitive.com
Mon Jun 30 23:09:29 CDT 2003

Chris Evans wrote:

> I have an image that lives inside of a table that I am trying to 
> dynamically position.  I am trying to find its current position, but am 
> having no luck.  It appears that you cannot set the top and left 
> position unless it is absolutely positioned, but I was hoping to be able 
> to at least read it.
> Is it possible to find the position of an image if it is not absolutely 
> positioned?

Setting position: absolute without specifying {top,right,bottom,left}
lets the browser put it where it normally would. Then you can get the
coordinates with something like this:

<style type="text/css">
#ball {
   position: absolute;
<script type="text/javascript">
function init()
   var ball = document.getElementById("ball");
   var thisTop = (document.all)? ball.offsetTop :
     document.defaultView.getComputedStyle(ball, "").getPropertyValue("top");
   /* tested Moz 1.4, IE 5.5 */

<img id="ball" src="/images/ball.png" alt=""/>

Caveat: the usual box model foolishness applies... :-)

Hassan Schroeder ----------------------------- hassan at webtuitive.com
Webtuitive Design ===  (+1) 408-938-0567   === http://webtuitive.com

                           dream.  code.

More information about the thelist mailing list