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 4256742
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T05:23:47+00:00 2026-05-21T05:23:47+00:00

I have a weird bug that started showing up when I made the body

  • 0

I have a weird bug that started showing up when I made the body be a relative positioned element with a 39px margin (I’m making room for a toolbar at the top of a page).

Anyway – if you look at how most sites tell you to compute a page element position, you’ll see code like this:

function getPos(elt) {
    var pt = [0, 0];

    while (elt.offsetParent !== null) {
        pt[0] += elt.offsetLeft;
        pt[1] += elt.offsetTop;
        elt = elt.offsetParent;
    }
    return pt;
}

This works fine, even if your <body> tag has a a margin. BUT, if your body is also position:relative, then this returns a value with is too small – it does not include the margins of the body element.

How do I 1) detect if the body is relative positioned, and 2) find our what the margins are so I can add them back in?

Note that I need Page coordinates so I can compare them to MouseEvent PageX, PageY.

  • 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-21T05:23:47+00:00Added an answer on May 21, 2026 at 5:23 am

    OK, I just did more research on this. The short answer, use jQuery:

    $(elt).offset()
    

    This is an a complex area that jQuery has a lot of code to handle all the cases correctly (see https://github.com/jquery/jquery/blob/master/src/offset.js). I think most browsers support getClientBoundingRect – in which case jQuery will use that (but has to adjust for viewport scroll position). Otherwise, it adjusts for the computed style of the body (yes, $(elt).css(‘margin-left’) will return the computed style), as well as any borders on intervening elements.

    So, the long answer is – unless you want to re-invent the 100+ lines of code from jQuery or other framework – best to get this measurement using their code, rather than write your own.

    Here’s a fiddler show some of the other methods (expanded from Snake Faust’s – thanks!):

    http://jsfiddle.net/mckoss/9zLGt/

    BTW, Snake, the offset from parent containers is $(elt).position(); $(elt).offset() returns the offset relative to the page/document – which is what I am looking for here.

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

Sidebar

Related Questions

This has been driving me nuts all day. I have a weird bug that
This is a weird bug that I can't figure out because I have tested
I have a weird date rounding problem that hopefully someone can solve. My client
I have a weird issue that only seems to be affecting IE 7. The
I have some extremely weird behavior that seems to result in silent exceptions. How
Hey fellas, I have this weird bug, and I have no clue how to
I have a fairly weird 'bug' with a simple query, and I vaguely remember
I believe I have found a weird bug as follow: I want to delete
I got a weird bug. In my .css file I have the following rule:
I have a really weird bug and cant figure out why things are not

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.