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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T00:25:58+00:00 2026-06-15T00:25:58+00:00

This isn’t a bug because the behaviour is consistent across FF, Chrome, IE9 and

  • 0

This isn’t a bug because the behaviour is consistent across FF, Chrome, IE9 and Safari on Win7.

The app i’m working on is 3rd party to the host page, so the CSS is immutable.The script attempts to align a new div with an existing element.

  • The body is position:relative
  • There is an H1 at the top of the page
  • The margin from the H1 seems to change where the body 0,0 is calculated from – even though the background on the body extends all the way to the edge, and it’s offsetTop property reports 0
  • Setting a border on the body solves the problem – seems strange behaviour but is consistent across browsers? (not a viable solution)
  • Removing the H1 margins solves the problem (not a viable solution)

Example here, the JS is commented for replicating each case:
http://codepen.io/anon/pen/EGvlb

I don’t think this is a bug with jQuery – it seems to be down to a legitimate relationship between the H1 margin and the body element?

$(function(){

  /* Setting body to position:relative breaks offset()
     because H1 margin moves body down */
  $(document.body).css({position: "relative"});

  /* Strange: putting a border on body fixes things? */
  //$(document.body).css({border: "1px solid #000"});

  /* Removing H1 margin removes problem */
  //$("h1").css({margin: 0});

  $("#overlay").css({
      left: $("#existing").offset().left,
      top: $("#existing").offset().top
  })
});
  • 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-06-15T00:26:00+00:00Added an answer on June 15, 2026 at 12:26 am

    I don’t think this is a bug with jQuery – it seems to be down to a legitimate relationship between the H1 margin and the body element?

    Yeah: the margins of the body and the h1 are collapsing, so not only does the h1 get shifted down by its default margin, but so does body. This is why you were able to make these observations:

    • Setting a border on the body solves the problem – seems strange behaviour but is consistent across browsers? (not a viable solution)
    • Removing the H1 margins solves the problem (not a viable solution)

    Anyway, by setting the body to position: relative, you’re telling #overlay to absolutely position itself with body’s top offset as the origin. Your script then moves it relative to the offset of #existing. The value of its offsetTop is relative to the viewport, which is calculated based on the position of this box… which itself is relative to h1 because it directly follows the h1 in normal flow.

    Because the margins of the h1 and body elements are collapsing, they have the same rendered margins. What happens then is twofold:

    1. #existing is pushed down by the h1 as its following sibling in normal flow. This increases its offset from the viewport (in your CodePen test case, it’s the top of the preview pane), resulting in its offsetTop being greater.

    2. When the body is set to position: relative, #overlay ends up getting positioned with its origin set to that of the body, which itself has also been pushed down due to collapsing with the h1’s margin. This increases the top offset of its origin, which would not happen if you had not positioned the body, because then #overlay uses the origin of the viewport instead, which never moves.

    The distance of #existing from the top of the viewport is added on to the offset of #overlay from its origin, that being the body, resulting in this effect.

    In summary: due to margin collapse, the browser ends up having to account for downward shifts in both h1 and body. The h1 influences #existing and in turn its offsetTop, while the body influences #overlay when you position it relatively. So the shifting effect appears twofold.

    As a quick workaround, you can subtract the h1 margins from the offsets of #existing for the purposes of offsetting #overlay:

      $("#overlay").css({
          left: $("#existing").offset().left - $("h1").offset().left,
          top: $("#existing").offset().top - $("h1").offset().top
      })
    

    Updated example

    Notice that using $(document.body) instead of $("h1") above won’t work. This is because margin collapse is purely a rendering effect, and does not actually influence the body’s offsets until you give it its own margin (in other words, the body margins are still computing to zero, so the DOM remains none the wiser).


    Not directly related to the issue at hand, but worth addressing anyway:

    • The margin from the H1 seems to change where the body 0,0 is calculated from – even though the background on the body extends all the way to the edge, and it’s offsetTop property reports 0

    The first and last parts of this point have been addressed above.

    As for the body background: while it appears to extend all the way to the edge, this extended background does not actually belong to body – it is propagated from body to, and thus belongs to, the root element (html) and the viewport. See this answer for an explanation.

    Oh and this:

    This isn’t a bug because the behaviour is consistent across FF, Chrome, IE9 and Safari on Win7.

    Made me smile. Because for once, somebody is right in saying it.

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

Sidebar

Related Questions

This isn't working in visual studio 2010 , it gives me the following error
This isn't strictly a programming question, but I'm asking it here because it's certainly
This isn't working the way I expect it to. Sample input will include an
This isn't a question on proper coding practice, I'm just working through the semantics.
This isn't working. I'm trying to replicate the animate to red and then remove
This isn't working in IE (although it does work in FFX). Why? Using HTML
This isn't a very simple question, but hopefully someone has run across it. I
This isn't working: $.ajax({url: PStoreZoomArea.php, type: get, data: {mapza: mapZoomArea, mapc: mapCenter, mapz: mapZoom}})
This isn't working in SQL Server 2008: ALTER TABLE Employee ALTER COLUMN CityBorn SET
This isn't my code, I'm just troubleshooting it. Some code that has been working

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.