Within-Page Links

When we jump to a different site, we provide its full pathname in the href. When we jump to a different page on our own site, we provide its relative pathname. In either case, the place we’re linking to has a name.

When we want to link from one part of a page to another (for example, to section two of this page), we have a problem. Other people’s sites have names. Our own files have names. The line at the beginning of section two in our file isn’t a file itself; it doesn’t have a name.

We will have to find some way to give that anonymous line a name so that we can link to it. We use the <a> element to establish an anchor on that line for us to link to. The easiest way to do a line of this form just before the line you want as your anchor point:

<a id="name_of_line"></a>

You can use any name you like, as long as it starts with a letter. We recommend that you use a short but meaningful name, and use only letters and digits and underscore characters. As an example, here’s the tag we used for section two of this very page:

<a id="section2"></a>

Notice that there’s no text between the opening and closing <a> tags. That’s OK in this case; we’re not activating the text, we’re anchoring it by giving it a name.

Section Two - Linking to a Named Anchor

Now that we’ve established an anchor, the line has a name, and we can link to it. We do it like this:

<a href="#section2">Go to Section 2</a>

Notice that this is an href, since we’re activating text for a link. You tell the browser that you’re referring to an anchor rather than a file by starting with a pound sign (which we’ve put in red for emphasis), followed by the name of the anchor you want to link to. The anchor is case sensitive, by the way, so if we had said href="#Section2" it wouldn’t have worked.

One other note: Nature abhors a vacuum, and so does a browser. Unless you pull some sneaky tricks, you will never see blank space at the end of a web page. When you link to an anchor, that anchor comes as close as it can to the top of the screen without leaving any blank space at the bottom.

In summary, jumping within a page is a two-stage process:

Step one: Establish an anchor with a name just before the line you want to link to. <a id="the_destination"></a>
Step two: put in the link that has a hypertext reference to that anchor. Don’t forget the # <a href="#the_destination">This text links to the destination</a>
 << Links to pages on your site Index The “Combo Link” >>