While browsing the internet these days, I see a lot of this:
<body>
...
<a name="top"></a>
...
<a href="#top">- Back to Top -</a>
...
</body>
There’s an easier, better and prettier way. CSS Signatures are all the rage these days. If you’re not familiar with a CSS Signature, it’s basically nothing more than an ID on your body tag, like this:
<body id="www-domain-tld">
The fundamental purpose of the CSS Signature is to allow a user to specify style adjustments to your site in their own user style sheets. Whether or not users are actually capitalizing on this is a discussion for another day, but doing this has other benefits like having an extra id to use when dealing with CSS specificity.
Additionally, we can use this to capitalize on a little known fact about HTML and anchors: you can use anchors to jump to any element on your page with an ID attribute.
Let me repeat that: You can jump to any element with an ID attribute on any page.
Here’s an example, if you don’t believe me: Nowhere on CNN.com’s homepage will you find a link to their footer, and you’ll find no mention of <a name="cnnFooter"></a> in the code but if you follow this link to CNN you will in fact find yourself staring at the footer on the CNN homepage. This is because their footer div has “cnnFooter” “cnn_ftrcntnt” as the ID which allows me to jump directly to that element.
Oh sure, I know what you’re thinking: “Great, so how do I get this to work in IE?” Surprise: it works just fine in IE, at least as far back as IE 5.5 and maybe further. So, here we have a nice, tidy little tidbit with no cross-browser issues.
So, we can do away with the empty anchor element at the top of our pages and instead do this:
<body id="www-domain-tld">
...
<p><a href="#www-domain-tld">- Back to Top -</a></p>
...
</body>
Trying to be all kinds of accessible with the “Jump to Content” link? Try this:
<p>
<a href="#content">- Jump to Content -</a>
</p>
...
<div id="content">
<p>Neat, huh?</p>
</div>
Also works well on comments:
<p>
<strong>Update:</strong>
Based on <a href="#comment-12345">so and so's comment</a>...
</p>
...
<blockquote id="comment-12345">
<h3>On Day X, so and so said:</h3>
<p>Blah blah blah...</p>
</blockquote>
…and comment forms:
<p>
<strong>2 Responses</strong>
<a href="#comment-form">- Jump to Comment Form -</a>
</p>
...
<form id="comment-form" method="post" action="">
...
</form>
Using this approach will streamline a lot of your code, allowing you to get rid of a ton of those empty anchor elements. It also allows for greater flexibility when linking to content on other’s sites. Not to mention that using it means you can sit at the cool kid’s table.
About the Author
Over the course of 33 years, Bill Brown has lived and worked in West Africa as a Field Station Coordinator on Bioko Island in Equatorial Guinea, represented the Memorex line of consumer electronics on QVC, owned a squirrel, taught improvisation and stage combat and launched two successful improv comedy troupes. Oh, and along the way, he’s developed some websites and applications.
Self-taught with a penchant for entertaining others, Bill’s clients have included internationally renowned photographer Steve McCurry, The National Maritime Law Enforcement Academy (whose site was developed entirely in the rain forests of Bioko Island), Virtual Giving, Workforce Strategy Center and the WSC web application Pathways to Competitiveness. Bill was also a presenter at the 2008 National Association of Government Webmasters conference in Chicago, IL.
Bill is the creator of WebDevelopedia.com and is an active member of several discussion lists, including the CSS Discussion List (404 link removed 2012/09/05). When not online, Bill can be found enjoying the company of his girlfriend, Jessica and their dog, Leica (she doesn’t have a web site).
38 Responses
Jeff Starr – September 16, 2009 •
@Helen: Thanks for the information and insight. This will give me something to refer to the next time I am dealing with unruly anchor behavior. Note that the
nameattribute is deprecated in XHTML in favor ofid. Usingnamefor scrolltop scripts with HTML is a nice workaround whenidis reserved for styling. Thanks :)@Max Weaver: I don’t think anyone is surprised at the idea of anchors, but replacing them with
ids for intra-page navigation may not have been realized by the wider design community. And yes, as Helen points out, anchors work fine in Safari, as doids used as anchors.@Chris McCorkle: Absolutely. As mentioned in the article, any element that accepts the
idattribute can be referenced and used for intra-page navigation. So comments, quotes, forms, divs, and just about anything else that is needed.Mark – September 17, 2009 •
Anchors have another use in our web-oh-two/RIA/ajaxy times. The problem is seen when you change the content of a page dynamically, eg. by use of AJax, and then the user wants to either
i) Book mark the page
and/or
ii) Refresh the page.
In both of these scenarios, on reload, the original, unchanged webpage will be seen.
One solution is to use and manipulate anchors, specifically the URL string to ‘remember’ the dynamically generated content.
The key is the little known or used javascript attribute ‘document.location.hash’ which is read/write access to the URL after the ‘#’ (That’s a HASH, not a Pound – I’m English dammit!).
Here’s a good enough example of it’s use – enjoy!
http://yensdesign.com/2008/11/creating-ajax-websites-based-on-anchor-navigation/
Mark – September 17, 2009 •
I didn’t know about CSS signatures – Cool idea I just implemented it on scamdex.com:
(Not that I will ever see anyone using it :’( )
Eric Meyer is my Hero!
Helen – September 20, 2009 •
I’ve found a wonderful piece of jQuery that I’ve been using for a while now. Don’t remember the author’s name, sorry! Scrolling smoothly, using “
name=top“, so you can set .Finally, it rewrites the pathname, so the visitor won’t end up with “
pagename.html#top#down#left#right“.$(function(){$('a[href*=#]').click(function() {if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')&& location.hostname == this.hostname) {var $target = $(this.hash);$target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');if ($target.length) {var targetOffset = $target.offset().top;$('html,body').animate({scrollTop: targetOffset}, 3000);return false;}}});});PS: Replace the
3000(milliseconds) by another value to chance velocity.Jeff Starr – September 20, 2009 •
@Mark: Thanks for chiming in on this — I am currently dealing with that exact issue on a new site I am working on. The article you posted may be just what the doctor ordered. Looking forward to checking it out. Also, the portion of a URL that proceeds the hash sign is often (although debatable) referred to as a “fragment identifier” — something that I didn’t realize until I began researching the topic.
@Helen: Awesome snippet. I have been using something similar with jQuery but it doesn’t work with the
nameattribute. Your code looks like it will do the job nicely. Thanks for sharing :)Garrett W. – September 25, 2009 •
Looks like in the last snippet, you forgot to change the
<form id="comment-12345"toid="comment-form";)Jeff Starr – September 26, 2009 •
Fixed :)
Helen – September 27, 2009 •
What I forgot to mention: The anchor-topic now has become relevant to SEO now: Anchor-IDs are used by Google to section your site in areas which will be display in the search results.
http://googlewebmastercentral.blogspot.com/2009/09/using-named-anchors-to-identify.html
If you have a Google-Webmaster-account: Log in. There is information about whether your site can be sectioned by Google.
Jeff Starr – October 6, 2009 •
Awesome tips, Helen — thanks again for contributing to the information presented for this topic. Much appreciated! :)
Alex – October 15, 2009 •
Excellent info! Already implemented this on my site to help people jump to the top of pages.
Very useful. Thanks.
Alex
Sunny Singh – December 7, 2009 •
After I realized that this works, I stopped using
<a></a>I also use the name of the section of the site, instead of the domain.
So
<body id="www-myunv-com">would be<body id="main">or “home” or something.smk – January 5, 2010 •
@Sunny
The whole point of using your domain name as body id is to create consistent “CSS Signature” for your whole site, so people may override your site style with their browser
user.css(or Greasemonkey, or whatever).E.x.
#www-myunv-com p {font-size: 46px; /* because I'm shortsighted */}:-)