Instructional Module W23c

Links and Images in (X)HTML

Contents

About this Document

Audience and Objectives

Review

 


 

Links in (X)HTML

Links are what give the Web its power: the power to leap effortlessly from one part of the world to another with a single click of the mouse.

Or to put it more technically, a link is an instruction to a browser to seek a Web page (or other service) from a server.

So: how do we create (X)HTML links?

Building a Link

 


Here is a link in (X)HTML, and what it looks like in the browser:

(X)HTML
Browser
<a href="http://inp.wccnet.edu/">
Internet Professional Department
</a>
Internet Professional Department

From this example you can see that there's a lot more to a link than meets the eye! Let's look at the parts...

(X)HTML
What is it?
<a href="http://inp.wccnet.edu/">
Internet Professional Department
</a>
Opening Anchor tag
<a href="http://inp.wccnet.edu/">
Internet Professional Department
</a>
Parameter defining this Anchor as a hyper-reference.
<a href="http://inp.wccnet.edu/">
Internet Professional Department
</a>
Hyper-destination: URL, file name, or other hyper-reference designator. This is what the browser is asked to look for.
<a href="http://inp.wccnet.edu/">
Internet Professional Department
</a>
Linked object. This is text, but can also be an image or other object. It is something that the browser displays and the user can click on.
<a href="http://inp.wccnet.edu/">
Internet Professional Department
</a>
Closing Anchor tag

 

 

Targets


Ordinarily, a hyperlink opens a file in the same window as the originating link, so that the new file replaces the old one. There are times when that's not ideal, and it would be better if the new file were "shot" into a different window.

A target is the window in which a hyper-reference will appear.

The target is assumed to be the same window as the link, unless there is a Target parameter in the Anchor tag. Here's what one looks like (highlighted):

(X)HTML
Browser

<a href="http://inp.wccnet.edu/"
target="_blank"
>
Internet Professional Department
</a>

Internet Professional Department

 

What the browser shows is exactly the same with and without the Target parameter - a user wouldn't know that clicking this link will open the file in a different window. (Try it!)

There are several possibilities for what you can name as the "target". Notice that several of them begin with an _underscore_ character:

target="_blank" Opens a new (blank) window and tries to put the new file in it.
target="_parent" If the link is in a frame, the file is opened in the parent fame.
Otherwise, it is opened in the same window as the link.
target="_top" If the link is in a frame, the file is opened in the top-level frame.
Otherwise, it is opened in the same window as the link.
target="_self" This specifies that the new file opens in the same window as the link - it's the default action, so need not be specified.

 

Window Names


The target names shown above are standard names, and need to be spelled as you see them, with _underscore_ characters in front of them.

But you can put in any name you want! This makes it possible for you to give names to windows. Here's how it works...

You put in a link with a target name. For example:

(X)HTML
Browser

<a href="w23ca.htm" target="Piglet">
Click here to open Piglet window.
</a>

Click here to open Piglet window.

The first time the user clicks the link (try it!) the browser opens a new window and remembers it as Piglet. The name Piglet is not part of the (X)HTML code in the linked file - it's just something the browser keeps track of for itself.

Now you create another link. The target is the Piglet window, but the link is to a different file. (Try this one, too.)

(X)HTML
Browser

<a href="w23cb.htm" target="Piglet">
Click here to see Christopher Robin in the Piglet window.
</a>

Click here to see Christopher Robin in the Piglet window.

 

 

 

Types of Links

At the start, we said a link is an instruction to a browser to seek a Web page (or other service). One of the reasons the Web has become so popular is the variety of services it can give us access to. It's done fairly simply by specifying the protocol to be used in talking to the server. This lets the browser know what kind of service is needed.

Web Pages


Web pages are what we naturally expect browsers to get for us - and what they expect to get, too. The page's file may be located on the same computer as the browser (a local file) or on a server.

(X)HTML
Notes
<a href="file:///C|/My Documents/letter.htm">
This is my first Web page!
</a>

If you want to specify a file on the same computer but in a different directory, you need to use the notation "file:///". Also, the file's name should be represented in the URL notation used by browsers.

Hint: The best way to get a file name into this notation is to open the file in a browser, copy the URL using Edit > Copy, and paste it into your (X)HTML file.

<a href="http://inp.wccnet.edu/">
Internet Professional Department
</a>

The notation "http://" indicates that the browser needs to use the HyperText Transfer Protocol. This is a "language" for browsers to use over the Internet to request files from servers.

Tip: When you create a hyperlink in (X)HTML, you must always put in the "http://", even though you don't need to when typing the same URL in the browser's Goto window.

<a href="mypage.htm">
This is my first Web page!
</a>

When a browser finds a name with no special markers, it assumes it is a file:

  1. On the same computer as the file containing the link (the "linking file")
  2. In the same directory as the file containing the link

The browser checks to see where the linking file came from, and fills in the complete path (directory listing) automatically. Then:

  • If the linking file came from the same machine the browser is on, the browser asks the local OS to get the file for it
  • If the linking file came from a server via the Internet, the browser uses HTTP to get the file.

 

 

Email

 


An email link is useful for encouraging visitors to a Website to send feedback to the designer - or to anyone else! Email links are simple to create. Here are some examples:

(X)HTML
Browser
<a href="mailto: krieg@wccnet.edu">
Send me email!
</a>
Send me email!
<a href="mailto:service@anywhere.com">
Click here to report a problem.
</a>
Click here to report a problem

 

Other Types


In addition to asking for Web pages and sending email, hyperlinks can be used for several other types of Internet exchanges. Here are a few of them:

(X)HTML
Notes
<a href="ftp://www.wccnet.edu/computer/mod/d23ca_f1.gif">
Click here to download the picture of Piglet.
</a>

FTP is the File Transfer Protocol, used for sending files from one computer to another.

<a href="gopher://gopher.ic.ac.uk">
Visit the Gopher Server at Imperial College, London
</a>

Gopher is an information linking system using the Internet. It was popular in the early 1990s, but lacked the appeal of the World Wide Web, and is practically unavailable now.

<a href="telnet:cidermill.wccnet.edu">
Internet Professional Department
</a>

Telnet is the Internet protocol used for logging in to a remote server. You generally need a user code and password to do this. When a browser sees a link like this, it opens a Telnet client in a separate window, tries to establish contact with the server, and let the user log in.

 

 

 

Creating Anchors

Anchors are like bookmarks inside an (X)HTML file. They aren't displayed by the browser, but you can use them to jump to any spot you like within a page.

How to Set Up an Anchor


Include this code in your file, just before the point you want your user to hop to:

<a name="Sec1" / >

Anchor name rules:

  • Names are case-sensitive, so be consistent in using them;
  • no spaces are allowed in anchor names (shorter is simpler, anyway!)

 

How to Link to an Anchor


Use the # ("pound sign") in front of the anchor's name to link to it:

<a href="#Sec1">Click here to go to Section 1</a>

You can also link to an anchor within another file, or on a different Website:

<a href="myfile.htm#Sec1">Link to my file's Section 1</a>

<a href="http://www.company.com/products/#Electronics">Click here to see the Company's electronic products</a>

 

Links with Images

When you see an image in a Web page, it is there because the browser made a special request for it. The image really is not part of the Web page: it is a separate file which is brought in by the browser and displayed in the proper place on the screen.

Given that, it's possible to use images as links, just like text. We'll show you how...

Simple Reference

 


Use the Img tag to call for an image to be displayed:

<img src="logo.gif" / >

The Img tag has several possible parameters that you can put in, but it always has a source src= parameter.

There is no closing tag for the Img tag, so in XHTML you'll need to put in the "self-closing" / at the end, as shown in the example. (This was not necessary in HTML 4.01 or earlier.)

Specifying the Size and Alignment

 


In addition to specifying the source, you may specify a number of other things:

  • Height of the image in pixels: height="285"
  • Width of the image in pixels: width="385"
  • Placement of the image: align="right"

<img src="w23c_f05.gif" width="385" height="285" align="right" />

Alternate Text


If the image is not displayed, or if the user runs the mouse pointer over an image, browsers can display text. This is helpful in many situations:

  • When visually impaired users have a text-to-speech program running, the program will read the alternate text aloud
  • If the image takes a long time to download, the text is displayed while you wait
  • If the image file can't be found, the text is displayed when the mouse runs over it

    <img src="w23c_f05.gif" alt="Figure 5: Over-link" />

Using an Image as a Link


Images are often used as links to other Web locations. Many buttons, for example, are images which are also links. This is done by putting the image tag in side a link tag.

(X)HTML
Browser
<a href="http://www.wccnet.edu/">
<img src="wcctile.gif" />
</a>
<a href="http://www.altavista.com/">
<img src="altavist.gif" />
</a>

 

 

About this document...

Review:

Link to review questions and answers. Link to review questions and answers.

Audience:

This is for people who have created a minimal (X)HTML page (see module W22h) and understand the general principles of (X)HTML (see module W22c).

Objectives:

When you successfully complete this lesson, you will be able to identify and explain the...

  1. Purpose of an (X)HTML link
  2. Purpose of the parts of an (X)HTML link
  3. Target parameter and its major options
  4. Various types of links that can be made
  5. Purpose of an (X)HTML anchor
  6. Syntax of an (X)HTML anchor
  7. (X)HTML Image tag and its parts
  8. Parameters to the Image tag setting height, width, and alternate message
  9. Parameter for assigning a background image to the Body tag.

Module W23c:

This document is part of a modular instruction series in Computer Information Systems. For more information, see the overview or the list of modules in this series, W: World Wide Web. This document has been used in the following classes: 

Author:

Laurence J. Krieg

Institution:

Internet Professional Department, Washtenaw Community College
Washtenaw Community College logo
History: Original: 09 November 2000; XHTML update 03 October 2003
Last modification:  Tuesday, 17-Feb-2004 12:39:28 EST
Copyright: Copyright © 2003, Laurence J. Krieg.
Instructors: You may point to this file in your Web-based materials.
Students: you may make a copy for your personal use.
All other uses: contact the author, Laurence J. Krieg for permission. Email krieg@ieee.org