Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7170655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:15:22+00:00 2026-05-28T15:15:22+00:00

So based on this question I asked, what’s the most reliable way of getting

  • 0

So based on this question I asked, what’s the most reliable way of getting position of objects that’s crossbrowser? Thx

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T15:15:23+00:00Added an answer on May 28, 2026 at 3:15 pm

    In general, assuming you have an element named elem, it’s actually quite easy to get the X and Y coordinates of the top-left corners of an element, assuming you want these in document coordinates. In all browsers this is returned by the elem.offsetLeft and elem.offsetTop properties.

    The only trick you have to be aware of is that if elem is absolutely positioned in another element, say a div with a left / top margin of 20px, these properties will return 0, as it only takes into account the current element and not the entire chain of elements. Luckily we can use a “chain-traversal” function to capture all of the margins of elements associated with a given element, tally them up to get the correct document coordinates.

    As Sime Vidas mentioned, there is also JQuery’s position() and offset() properties, in this case you would want the offset() properties.

    You can also use the getBoundingClientRect() method, however this returns the coordinates of an element relative to its offsetParent and thus is not entirely reliable. Look at the following examples:

    // getPosition function
    function getPosition(elem){
    
        var dims = {offsetLeft:0, offsetTop:0};
    
        do {
            dims.offsetLeft += elem.offsetLeft;
            dims.offsetTop += elem.offsetTop;
        }
    
        while (elem = elem.offsetParent);
    
        return dims;
    }
    
    cont1.style.position = "absolute";
    cont1.style.marginLeft = "10px";
    
    cont2.style.position = "absolute";
    cont2.style.marginLeft = "10px";
    
    box.style.position = "absolute";
    box.style.marginLeft = "10px";
    
    
    console.log(getPosition(box).offsetLeft); // returns "30"
    console.log(getPosition(box).offsetTop); // returns "0"
    
    // or in JQuery
    console.log($(box).offset().left) // also returns "30"
    console.log($(box).offset().top) // also returns "0"
    

    Also I suggest you read this.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Based on this SO question asked a few hours ago, I have decided to
I had asked this question in a much more long-winded way a few days
Based on this question I asked earlier on setting up cookies in Perl ,
Ok, I asked this question before, but deleted it as the way I went
I was asked this question recently in an interview : What is the most
Over a year ago someone asked this question: Execute .sql files that are used
So based on this question ( here ), of which I asked last week,
I asked this question on gis.stackexchange but it was suggested that this is more
I asked this question a while back to delete duplicate records based on a
I've seen this question asked before but it seems that the answer may be

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.