tags:

views:

546

answers:

5

On an HTML page, a link like this:

<a href="#pagelocation">Location on Page</a>

...should navigate to this spot on the page:

<a name="pagelocation">

But in my experience, it sometimes misses - especially when linking from another page (like <a href="somepage.html#pagelocation">). By "misses," I mean it scrolls to the wrong spot on the page - maybe close, maybe not.

Normally, the target location ends up at the top of the screen. I know this can fail if there's not enough room below the anchor to scroll it to the top of the screen.

Why else would it fail? Does it depend on layout at all? How can I fix it?

(I'm keeping this general because I'd like a catch-all reference answer.)

Update 1

Thanks for the pointers so far about non-explicit image sizes. But what about on a page where all the elements have explicit size? (I'm dealing with one now.)

+4  A: 

I believe the behavior you are seeing is the result of the browser locating to that spot on the page before all images have finished loading. Once the images finish loading, then the layout of the page has changed (the page is likely longer vertically, for example), causing the location of where the anchor should be to have changed - but the browser still thinks it has already navigated to that anchor.

matt b
+14  A: 

Quite often the scrolling can occur before the page has finished loading. If you have images without widths and heights, the page will jump, then load the image and re-layout itself, making the place you previously jumped to seem wrong.

Edit: Anything else that can change page layout should also be considered with suspicion... this include javascript and CSS that's not loaded in the <head> (never mind that all CSS should be loaded in the head; it isn't always).

If the page is bounced through a redirect, I believe IE will scroll the end page but Firefox won't.

Greg
I think there could also be problems if the target is in a floated container, or the container is positioned using any other method than 'static' since this distrupts the natural flow of the document...
HorusKol
+1  A: 

Note that you can add id="pagelocation" to just about any HTML element, for the same result, which saves you adding the additional anchors for link destinations.

Peter Hilton
How would you structure the link?
Nathan Long
The same - <a href="#pagelocation">Location on Page</a>
Peter Hilton
A: 

As mentioned above, this is probably due to images being rendered late and 'adjusting' the layout as they load.

If you can specify the size of the images then that much room can be allocated before they render, which should prevent the problem.

As a side note I've had this problem before in the form of using forward/back between enough pages that the images needed reloading, causing me to end up in the wrong place after they had rendered.

DrHazzard
A: 

I have also seen this happen when JavaScript creates a drop-down menu at the top of a page. Then, once the menu has been finished, it is hidden, scrolling up the content below.

In the meantime, the browser has already set the target location at the top of the window. Hiding the menu a the very top of the page moves the target location up off the top of the window.

JSchaefer