Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do page anchors sometimes miss?

Tags:

html

anchor

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.)

like image 468
Nathan Long Avatar asked Jan 06 '09 14:01

Nathan Long


People also ask

How do page anchors work?

An anchor tag, or anchor link, is a web page element that links to another location on the same page. They are typically used for long or text-heavy pages so that visitors can jump to a specific part of the page without having to scroll as much.

How do I link to an anchor on the same page?

To link to your newly-created named anchor, highlight the text, then click the Linkit button. In the "Link URL" field, add a "#" symbol and your anchor name: Click "Insert link" to add the link to your named anchor (on the same page).

Can you have anchor link to another page?

If the anchor you are linking to is on a different page as the link: In the pop-up box, click the Link to dropdown menu and select URL. Enter the full URL of the page followed by the # hashtag symbol, followed by the name of the anchor. Click Insert.


1 Answers

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.

like image 180
Greg Avatar answered Oct 21 '22 09:10

Greg