Without hyperlinks, the Web wouldn’t be “the Web”. This section explains how to create links to other websites (external links) and links within your own website (internal links).

The a Element

The anchor element, a, creates links. Here’s an example:

Example 2.12. External Links
   A link to <a href="">Google's home page</a>.

   A link to <a href="">a page within Google</a>.

The syntax is straightforward: the href attribute indicates where the link should go, and the text between the <a> and </a> tags becomes the highlighted link text.

Anchor Titles

The optional title attribute allows you to provide a little extra information about the link. Some older browsers ignore this attribute, but most modern browsers produce a little floating “tooltip” if you hover your mouse over the link.

Example 2.13. The title Attribute
Can physics be
<a href="" title="The most hilarious collection of physics
humor anywhere!">funny</a>?
Is that a rhetorical question?

You can use the title attribute to indicate where the link is going or provide some other useful information. It’s nifty, but it doesn’t hurt to leave it out, either.

Components of the URL

To construct links to other websites, we need to understand the structure of the URL. Let’s take a closer look at the URL ““:

  • The protocol (”http://“): Specifies that we would like to use the HTTP (HyperText Transport) protocol. This is the standard protocol for retrieving web pages, although there are others, such as FTP (File Transfer Protocol) and HTTPS (Secure HyperText Transport Protocol). Most of the time, though, you’ll be using “http://” to retrieve pages from external sites.

    You may specify the protocol in upper case, lower case, or anything in between, but most people use all lower case.

  • The website (”“): This specifies that we would like to access a page from More technically, it means:

    • Go to the top-level domain “com” (a collection of many millions of websites).
    • Within “com”, find the the domain name “google”.
    • Within “google”, find the the sub-domain named “www”.

    Finding the website is similar to finding a mailing address. The address “1600 Pennsylvania Avenue NW, Washington, DC 20500″ means: go to the zip code “20500″; within “20500″, find the city “Washington D.C.”; within “Washington, DC”, find the building “1600 Pennsylvania Avenue NW”.

    As with the protocol above, you may use any case you like for specifying the website, although most people use all lower case.

  • The file path (”/help/basics.php“): This specifies that we would like to access the file basics.php, which is inside the directory help/.

    Unlike the other components, the filepath can be case-sensitive. Depending on the website you’re visiting, the file Basics.php might be different from the file basics.php.

Wait, Which File Was That Again?

This is a little odd ― in our first example, “”, we didn’t specify a file or a directory. So what did we ask for, exactly?

First, it turns out the directory is specified. The single forward slash (”/”) represents the “root” or “top-level” directory of the website. We’re asking Google to give us a file from the top directory of its website.

Okay, but which file are we asking for? We didn’t specify the name of the file explicitly, we just asked for a directory. This means means we’re asking Google, “Give us the default file for that directory, if you have one.” As it turns out, Google is going to give us a file named “index.php”, but the exact name could be anything. It’s up to Google.

Internal links

Internal links are similar to external links except that you can leave off the protocol and website name. You only need to specify the file path.

File paths for internal links can be absolute or relative. Absolute links always begin with a forward slash. They specify a path that starts at the root directory of your web server. Relative links never begin with a forward slash. They specify a path that is relative to the page that the link is on.

Examples of absolute links:

  • href="/": Go to the web root directory and get the default file. (The home page of

  • href="/HTML/“: Go to the web root directory, then the HTML directory, and get the default file. (The first page of this tutorial.)

  • href="/HTML/internal_links.php": Go to the web root directory, then the HTML directory, and get the file internal_links.php (This page.)

  • href="/HTML/examples/linkexternal.txt": Go to the web root directory, then the HTML directory, then the examples directory, and get the file linkexternal.txt. (The source of Example 2.12, “External Links”.)

Examples of relative links:

  • href="index.php": Get the index.php file that is in the current directory. (The first page of this tutorial.)

  • href="../": Go up one directory from the current directory and get the default file. (The home page of

  • href="examples/linkexternal.txt": Go to the examples directory and get the file linkexternal.txt. (The source of Example 2.12, “External Links”.)

As the examples demonstrate, URLs can vary in complexity while still pointing to the same location.

For pages that are “near” the current page, relative links are often simpler. Relative links also work better if you are working with files on your desktop, as opposed to a real websever.

Because absolute links are the same no matter where you are on the website, they’re good for standard navigation links, header and footer links. However, absolute links don’t work as well if you are just messing around with files on your desktop. On a webserver, an absolute link such as /HTML/internal_links.php starts with the webserver’s root directory. But on your local desktop, there is no “webserver root directory”, and so your computer attempts to follow the filepath /HTML/internal_links.php from the root directory of your hard drive. In general, this will fail.

Linking Within Pages

We can link to web pages in external websites and internal websites. What about linking to a particular location in a page?

All elements provide this capability through the id attribute. Once you’ve assigned an id to the element, you can link directly to the location on the page where the element appears:

Example 2.14. Linking to an ID
<h2 id="fruitbat_migratory">
   Migratory Patterns of the Fruit Bat
<a href="#fruitbat_migratory">link to 'Migratory Patterns of the Fruit Bat'</a>.

The id’s value is “fruitbat_migratory“. Further down the page, we have a link to this ID, specified by href="#fruitbat_migratory".

You can combine named anchors with longer URLs: for example,

The number sign in the URL makes the link point to the named anchor. If we had accidentally written href=”parts��, that would have been a relative link to the directory named “parts”.


See Also