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 »
10,640 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 »
10,640 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 »
10,640 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 »
10,640 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 »
10,640 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



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
InVision App - Prototyping Made Beautiful With Prototyping Tools Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
Feb 12, 2012 at 3:37 AM
Learning ColdFusion 8: CFImage Part III - Watermarks And Transparency
Hi Ben, Just to ask currently it is placed bottom right corner, if i need to replace the same rendered image on the bottom left side or in the bottom center, how that can be calculated. bottom ce ... read »
Feb 11, 2012 at 9:29 PM
Use jQuery's SlideDown() With Fixed-Width Elements To Prevent Jumping
I can't say how glad I am that I found your post. Thank you very much. ... read »
Feb 10, 2012 at 7:21 PM
jQuery AJAX Strips Script Tags And Inserts Them After Parent-Most Elements
Update! Instead of $(eval(options.insertAfter)).after(data['insertData']); I now use: var ajaxNode = document.createElement('span'); var parent = $(eval(options.insertAfter))[0].parentNode; ... read »
Feb 10, 2012 at 6:18 PM
jQuery AJAX Strips Script Tags And Inserts Them After Parent-Most Elements
encountered this same, what I consider, jQuery bug last week. I'm building a site in which I load some content via AJAX. This content contains Linkedin share button placeholders which Linkedin API ne ... read »
Feb 10, 2012 at 11:30 AM
Cross-Origin Resource Sharing (CORS) AJAX Requests Between jQuery And Node.js
After you understand the concepts here, this is an awesome cheatsheet for enabling CORS in just about anything http://enable-cors.org/ ... read »
JM
Feb 10, 2012 at 9:10 AM
My Safari Browser SQLite Database Hello World Example
@Amy, Here is a very good tutorial on how to use JOIN: http://www.sqltutorial.org/sqljoin-innerjoin.aspx ... read »
Feb 10, 2012 at 4:42 AM
Building A Twitter-Inspired RESTful API Architecture In ColdFusion
This is great, very useful Ben. I spotted a small typo in the api.cgm listing: <cfthrow type="Unauthroized" /> Cheers Stefan ... read »
Feb 9, 2012 at 10:35 PM
CFDirectory Filtering Uses Pipe Character For Multiple Filters (Thanks Steve Withington)
I was wondering if there would be a filter you could apply so that you got everything but what you included in the filter. As in show me all docs that are not a .pdf. ... read »