In an earlier tutorial we looked at how to use the <a> container to link to a different web page. Well this particular container had a second use - to provide an anchor point in a web page that can be linked to.

The way that you would code an anchor point is very simple as you just need to give the anchor an id so that you have a way to reference it.

<a id="myanchor"/>

Another thing to consider is whether you already have an appropriate tag at the correct spot in your page since it is the id portion that makes the anchor work rather than the <a> tag as such and so if you already have a tag there you can add the id to that rather than needing to add an anchor tag. The last browser to require the use of the <a> tag for anchors was Netscape 4, all more recent browsers will recognise the id regardless of the tag you attach it to.

Once we have an anchor point defined we can now place links elsewhere on that page or on other pages that link to that anchor point. The only difference between a link to an anchor point and an ordinary page link is what goes in the href attribute. Here is a link from elsewhere on the same page:

<a href="#myanchor">

To link from a different page you just need to add the anchor reference to the end of the href that refers to the page (eg. href="next.htm#myanchor").


This article written by Stephen Chapman, Felgall Pty Ltd.

go to top

FaceBook Follow
Twitter Follow