Skip to main content.

Web Based Programming Tutorials

Homepage | Forum - Join the forum to discuss anything related to programming! | Programming Resources

How to use HTML 3.2

Chapter 6 -- Using Hypertext Links

Chapter 6

Using Hypertext Links


CONTENTS



The single greatest feature of the World Wide Web is its diverse collection of documents, which number in the millions. All of these documents are brought together through the use of hypertext links. Users navigate the Web by clicking on the links that HTML authors provide. Hypertext links are a crucial part of HTML-which, after all, is short for Hypertext Markup Language.

In this chapter, we'll look at the simple process behind how hyperlinks work in your HTML documents. You'll also learn how to link to a specific point in a large document by using the ID attribute. Finally, we'll take a look at the difference between using absolute and relative path names in your hyperlink references.

Linking is one of the easiest and most important parts of using HTML. So warm up your Web browser and Notepad and get ready to explore.

How to Create a Hyperlink

Hyperlinks connect two different documents. You can link to one of your own documents or to any other document on the World Wide Web. You can even link to a different section in the same document. It is very easy to create links with HTML, and you only need to follow a few simple steps.

Tip Sheet

  1. Use your Web browser to locate the document you want to link. You can link to any other document on the World Wide Web.

    Figure 6.2 :

  2. Make a note of the Uniform Resource Locator (URL) of the document you want to link to. The URL is prominently displayed by your Web browser, usually near the top. Make sure to note the complete URL.

    Figure 6.3 :

  3. To make a link to another document, you need to use a special type of HTML tag known as an anchor tag, also commonly known as a link tag. Locate the place in your HTML document where you want to insert the hypertext link. Type <A HREF=", followed by the URL of the document you want to link to. Then close the tag by typing ">.

    Figure 6.4 :

  4. Type some descriptive text (also known as the link text) after the anchor tag to let readers know something about where this link will take them.

    Figure 6.5 :

  5. Finish the anchor tag by typing </A> on the same line.

    Figure 6.6 :

  6. Once you've created your link, check to make sure it works by clicking on it while using your Web browser. Note that by default, most Web browsers display hypertext links as underlined text in a different color than normal text. This lets your readers know that clicking on the text will take them to another document.

How to Use the ID Attribute

When you create a simple link to a Web page using the technique you learned on the previous page, the reader is always taken to the top of the new page. What if you want to link to a particular section of a document and take the reader immediately to that point?

Assigning an ID to an element in your HTML document allows hyperlinks to point directly to that element instead of to the very top of the page. The HTML 3.0 ID attribute did not make it into the HTML 3.2 standard, but it is rendered by some browsers. You can use the ID attribute for most HTML elements, such as paragraphs, headings, and lists.

Tip Sheet

  1. Locate the element you'd like to name with an ID. This can be almost any element in your document, but it is usually a paragraph or heading.

    Figure 6.8 :

  2. Inside the element's opening tag, just after the letter P, insert a space and type ID=.

    Figure 6.9 :

  3. Your element ID needs a name. The hyperlinks will use this name to take readers directly to this section of your document. In this example, we'll simply name the element "MyParagraph".
  4. Type the name of your ID, inside quotation marks.

    Figure 6.10 :

  5. To create a hyperlink directly to this element, add a pound sign and the ID name inside your hyperlink tag. For example, to link directly to "MyParagraph", a typical hyperlink might look like the one above.

    Figure 6.11 :

How to Use Relative Path Names

In the beginning of this chapter, you learned how to create a hyperlink by pointing to the full URL of another document. However, if you're linking to different documents on the same Web server (usually your own), you don't always need to use the full URL. You can use relative path names.

Web browsers, even when running on PC or Macintosh machines, always follow UNIX style path names. This means that directories (folders) are separated by forward slash marks (/), and higher-level directories are indicated by two periods (..).

Tip Sheet

  1. The simplest relative path name is no path name at all. If you're linking to another document that's in the same directory, all you have to do is type in the file name of the new document in place of the full URL. For example, to link to a document named newfile.html, type
    <AHREF="newfile.html">.

    Figure 6.12 :

  2. To link to documents or files in a subdirectory, all you need to specify is the path and file name relative to the current document. For example, to link to a document called budget.html in a subdirectory named budget96, you would type
    <A HREF="budget96/budget.html">.

    Figure 6.13 :

  3. You can also navigate up the directory tree of your server by using two periods (..) to move up one level. For example, to link from the budget.html file in the previous example back to the main document, you would type
    <A HREF="../main.html">.

    Figure 6.14 :

  4. If the new document was two levels above the current one, you would separate each level with a slash, and type
    <A HREF="../../main.html">.

    Figure 6.15 :

  5. The single greatest advantage to using relative path names is portability. If you do your HTML development on a local machine, and then upload your finished work to a Web server, you can save yourself the trouble of having to reset all of your hyperlinks to reflect the new location. Likewise, relative path names will save you the headache of changing your hyperlinks if you move your existing HTML files to an entirely new Web server.