goer.org | HTML Tutorial : Basic : Links

Table of Contents

About
Introduction
Diving In
Structure
Tinkering
Tags
Attributes
Browser Tools
Basic
Paragraph Breaks
Headings
Font Styles
Colors
Links
Images
Lists
Intermediate
Style Sheets
Classes and IDs
Div and Span
Font Control
Borders
Margins and Padding
Align and Indent
Advanced
Tables
Miscellany
SHTML

Links

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> Tag

The <a> tag (anchor tag) allows you to create links. Here's a simple example:

External Links (source)

<p>
  A link to <a href="http://www.google.com/">Google's 
  home page</a>.
</p>
<p>
  A link to 
  <a href="http://www.google.com/help/basics.html">a 
  page within Google</a>.
</p>

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.

External Links (Results)

A link to Google's home page.

A link to a page within Google.

Parts of the URL

Let's take a closer look at the URL "http://www.google.com/help/basics.html":

  1. The protocol ("http://"): This specifies that we would like to use the HTTP (HyperText Transport) protocol. This is the standard protocol for retrieving web pages, although there are certainly others: FTP (File Transfer Protocol), HTTPS (Secure HyperText Transport Protocol), and so on. Most of the time, though, you'll be using "http://" to retrieve web 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.

  2. The website ("www.google.com"): This specifies that we would like to access a page from Google.com. (Technically, it means that we would like to access a page from "the computer named 'www' in the domain of 'google' with the top-level-domain of 'com'.") As with the protocol, you may use any case you like for specifying the website, although most people stick with all lower case.

  3. The file path ("/help/basics.html"): This specifies that we would like to access the file "basics.html", which is inside the directory "/help/". Unlike the other parts of the URL, the filepath is case-sensitive: the file "Basics.html" is different from the file "basics.html".

Which File Was That Again?

This is a little odd -- in our first example, "http://www.google.com/", 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? Well, in the immortal words of famed web designer David Spade, "Mmmm... don't-know-don't-care." We didn't specify the name of the file, which 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.html", but the exact name could be anything, really. It's up to Google.)

Internal Links

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

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

Examples of absolute links:

Examples of relative links:

As you can see, URLs can vary considerably in length and complexity while still pointing to the same location. Sometimes it's a good idea to use relative links: for pages that are "near" the current page, relative links are often simpler. Sometimes it's a good idea to use absolute links: an absolute link will be the same no matter where you are on the website, so they're excellent for standard navigation links, header and footer links, and so on.

One caveat about absolute links: if you are just messing around with files on your desktop (as opposed to placing files on a proper web server), your absolute links will fail. A URL such as "/HTML/basic/links/" starts with the "webserver root directory" -- but if your files are not loaded onto a webserver, then there is no "webserver root directory", and your computer will try to match the URL to your local file system. On Windows machines, this might look like, "C:\WINDOWS\Desktop", on a Mac, "/Users/evan/Desktop/", and on another UNIX machine, "/home/goer". In all three cases, the link will fail.

Linking Within Pages

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

The anchor tag provides this capability through the name attribute. Once you've named an anchor tag, you can link directly to the spot where the anchor tag appears:

Named Anchor (source)

<a name="parts"></a>
<h4>Parts of the URL</h4>
...
...
...
<p>
  <a href="#parts">link to 'Parts of the URL'</a>.
</p>

The named anchor is named, "parts", and it does not have an href attribute. Further down the page, we have a link to this named anchor, specified by, href="#parts". Note that it is the pound sign in the URL that 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".

Named Anchor (Results)

link to 'Parts of the URL'

If you select the link, you will jump to the 'Parts of the URL' heading, which is near the top of this page.

Named Anchor Usage

Anchor Titles

The optional title attribute allows you to provide a little extra information about the link. Some (older) browsers will ignore this attribute, but others will produce a little floating "tooltip" if you hover your mouse over the link: Hover over this link.

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.