Using BODY ID As A Back To Top Page Anchor

Posted July 11, 2007 at 10:35 AM by Ben Nadel

Tags: HTML / CSS

Traditionally, I think a lot of people use a named Anchor tag to create a "Back To Top" page anchor:

  • <a name="top"></a>

I know that I've done that. But, it always felt a bit ganky to me. I mean, the first element in your page is a useless link? That just feels wrong, doesn't it?

Just recently, I found out that you can refer to element IDs rather than named anchors to create page anchors links. This is a really cool piece of information that can easily be applied to the "Back to Top" page anchor links. Now, instead of having these silly, useless links, we can put an ID in the XHTML BODY tag and then use that as our back to top target:

  • <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  • <html>
  • <head>
  • <title>Body ID For Top Anchor Demo</title>
  • </head>
  •  
  • <!-- NOTICE: ID in Body Tag. -->
  • <body id="top">
  •  
  • <h1>
  • This Is My Demo
  • </h1>
  •  
  • <p style="margin-bottom: 3000px ;">
  • This paragraph has a huge ass bottom margin
  • so that the page will definitely scoll and
  • put the following link below the page fold.
  • </p>
  •  
  • <p>
  • <!--
  • This link will jump back up to the ID:top in
  • the document. Since that is the ID of the body
  • tag, this link will jump to the top of the page.
  • -->
  • <a href="#top">Back To Top</a>
  • </p>
  •  
  • </body>
  • </html>

As you can see here, our XHTML BODY tag as the ID "top." Then, our link at the bottom of the document uses a hash that points to that ID. Clicking on it will act in the same way as a named anchor tag would have, scrolling the user back up to the top of the page. The only minor note here is that the scrolling takes body margins into account. Meaning, if your body has a 10 pixel top margin, the window will jump to a 10 pixel offset since technically, the body is not at the zero offset.

Anyway, this just occurred to me last night and I think it results in much cleaner, more syntactically informative mark-up.



Reader Comments

Jul 11, 2007 at 11:57 AM // reply »
1 Comments

Just move your 'id="top"' to the <html> tag and you won't have to worry about the margins either


Jul 11, 2007 at 12:00 PM // reply »
11,243 Comments

Oh snap! I didn't even think of that. HTML doesn't occur to me as a rendered document element, so I didn't think it would be possible to jump to it. Thanks for the tip.


Jul 11, 2007 at 12:43 PM // reply »
43 Comments

Great tip. I didn't know you could do that, but it's so much more elegant.


Jul 11, 2007 at 1:08 PM // reply »
3 Comments

You may also use just a hash sign in href attribute for "back to top" link and you don't need to bother with ID's. I.e. <a href="#">back to top</a>


Jul 11, 2007 at 1:14 PM // reply »
11,243 Comments

@Jura,

While that works, I am not sure if that is a feature of the language? Or a general standard that browsers seem to employ.


Jul 11, 2007 at 1:18 PM // reply »
43 Comments

More to the point, is it adjusting the scroll value or is it reloading the entire page and you end up at the top of the page by default. I thought it was doing a complete reload (albeit from cache in many cases).


Jul 11, 2007 at 1:33 PM // reply »
11,243 Comments

Using the Hash should NOT reload the page. Just jumping around.


Jul 13, 2007 at 4:38 AM // reply »
3 Comments

2 Ben:

It's not the feature per se but default behaviour for most browsers I know - if the anchor name could not be found browser just scrolling page up to the top


Cal
Jul 20, 2007 at 12:40 PM // reply »
1 Comments

Unfortunately this doesn' seem to work in IE7. :(


Jul 20, 2007 at 12:52 PM // reply »
11,243 Comments

Typical. It seems to be a HTML 4 standard:

http://www.w3.org/TR/html401/struct/links.html#h-12.2.3

It also appears to be part of the XHTML standard:

http://www.w3.org/TR/2003/WD-xhtml2-20030506/mod-attribute-collections.html#adef_attribute-collections_id

... so who knows :(


Sam
Oct 9, 2008 at 1:17 AM // reply »
2 Comments

I have just spent AGES trying to fix this problem - you're a lift saver!!!

:):):):):):):):):):):):):):)


Sam
Oct 9, 2008 at 1:18 AM // reply »
2 Comments

and you save lives too :)


Dec 3, 2008 at 6:18 PM // reply »
1 Comments

u can either use "name" and there wont be any troubles with margins, but transitional doctype doesnt validate that : [


Aug 31, 2009 at 2:14 PM // reply »
1 Comments

Thanks so much for posting this great information. To solve the IE issue, I just hid my named anchor DIV from it, which is fine because I highly encourage my viewers to switch to a standards compliant browser anyway.

Thank you, thank you, thank you!


Dec 16, 2009 at 1:20 AM // reply »
1 Comments

Your body id trick just solved my problem with IE8 and Firefox reading differently my back-to-top solution.

Thank you.


Jan 4, 2010 at 5:43 PM // reply »
1 Comments

For a better solution (but with no IE6 support) try this:

http://agyuku.net/2009/05/back-to-top-link-using-jquery/


Jan 4, 2010 at 6:44 PM // reply »
11,243 Comments

@Giovanni,

Yeah, jQuery can do some nice stuff.


May 19, 2010 at 6:07 AM // reply »
1 Comments

Guys,

Thanks so much for this solution! I'm greatly indebted to you!


CC
Sep 6, 2010 at 5:38 AM // reply »
1 Comments

This is a really great tip. i tried sever ones suggested by other websites before which turned to be failures.This one works greatly!!!


Oct 18, 2010 at 5:20 PM // reply »
1 Comments

Thanks for that. Works great:) I'm gonna use this in my 2nd ever web project. Hope my teacher will approve. Will let u know.


Dec 14, 2010 at 11:25 AM // reply »
1 Comments

This is a fantastic way of fixing pages not going all the way back to the top - thanks


Mar 3, 2011 at 12:10 PM // reply »
1 Comments

Thanks for your post! I too felt awkward having a floating anchor tag. Took the advice of one of your commenters and added it to the html tag instead. Ahhhh so much better!


Jun 22, 2011 at 9:49 AM // reply »
1 Comments

Thank you so much! So simple, and it gets rid of annoying floating anchors. Yay! =D


Dev
Apr 12, 2012 at 3:32 AM // reply »
1 Comments

Thank you!


Jan 22, 2013 at 11:48 PM // reply »
1 Comments

Thank you so much for the helpful information on "back to top" code. Feel free to visit my page and view the footer example on the bottom with navigation, etc. Feel free to share and view its features. Thanks all. http://gmartiro.blogspot.com


Feb 27, 2013 at 6:36 PM // reply »
2 Comments

Hi. I'd like to get the link to reach the top of the comments section. I've tried some things but it keeps going to the top of the page. Any ideas?


Feb 27, 2013 at 6:41 PM // reply »
2 Comments

whoop - got it. changed it from the single.php to comments.php. Thank you for teaching me how to link : )



Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
May 23, 2013 at 5:19 AM
Ask Ben: Print Part Of A Web Page With jQuery
How to print also the background color of table cells and table lines ... read »
May 23, 2013 at 3:55 AM
Javascript Array Methods: Unshift(), Shift(), Push(), And Pop()
very interesting and helpful too. ... read »
May 22, 2013 at 5:35 PM
Script Tags, jQuery, And Html(), Text() And Contents()
This is still an issue 2 years later. jQuery is supposed to remediate these cross browser issues, no? I have been unable to find any statement from the jQuery team calling this behavior "by de ... read »
May 22, 2013 at 12:44 PM
Ask Ben: Query Loop Inside CFScript Tags
In cf10, if you call a function that has: local.result = {}; local.result.msg = ""; local.svc = new query(); local.svc.setSQL("SELECT * FROM..."); local.obj = local.svc.exe ... read »
May 22, 2013 at 12:29 PM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@Ben: What version of Java are you using? Also, did you test users.id to see what Java reports as the data type? I wonder if it's not a Java primitive data type, but getting returned as something ... read »
May 22, 2013 at 11:47 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@Dana, Awesome - so it looks like this bug was fixed in ColdFusion 10. Thanks so much for double-checking that. ... read »
May 22, 2013 at 11:37 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
When I c&p and run on cf10, I get: Selected User IDs: 1,4 User 1 selected: YES - YES User 2 selected: NO - NO User 3 selected: NO - NO User 4 selected: YES - YES User 5 selected: NO - ... read »
May 22, 2013 at 11:27 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@Tom, Good thought, but no dice. Both of these still exhibit the same behavior: users.id[ users.currentRow ] users[ "id" ][ users.currentRow ] It's just something whacky happening with ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools