Creating a Hyperlink or Anchor (<a>)
The <a> element creates a hyperlink (use the href attribute) or anchor (use the id or the deprecated name attribute for browsers that need to use name, such as Netscape). This element is supported in XHTML 1.0 Strict, XHTML 1.0 Transitional, XHTML 1.0 Frameset, and XHTML 1.1. Here are this element's attributes:
Attribute | Description |
---|
accesskey | Assigns a keyboard access key to the hyperlink. (XHTML 1.0 Strict, XHTML 1.0 Transitional.) |
charset | Indicates the character encoding of the hyperlink's target. Set this to an RFC (Request for Comments) 2045 language character set string. The default value is ISO-8859-1. |
class | Gives the style class of the element. |
coords | Sets the coordinate values (in pixels) appropriate to the corresponding shape attribute to define a region of an image for image maps. |
dir | Sets the direction of text that doesn't have an inherent direction in which you should read it, called directionally neutral text. You can set this attribute to ltr, for left-to-right text, or rtl, for right-to-left text. |
href | Holds the target URI of the hyperlink. You must assign a value to either this attribute or the id attribute. |
hreflang | Gives the base language of the target indicated in the href attribute. Set this to RFC 1766 values. |
id | Use the ID to refer to the element; set this attribute to a unique identifier. |
lang | Specifies the base language used in the element. Applies only when the document is interpreted as HTML. |
name | Is available in the three XHTML 1.0 DTDs, but is deprecated and not available in the XHTML 1.1 DTD (use id instead). Gives the anchor a name, which may be used as the target of a hyperlink. (XHTML 1.0 Strict, XHTML 1.0 Transitional, XHTML 1.0 Frameset.) |
rel | Specifies the relationship described by the hyperlink. |
rev | Is the same as the rel attribute, but the syntax works in the reverse direction. For example, a link from A to B with rel="X" signifies the same relationship as a link from B to A with rev="X". |
shape | Specifies the type of region for mapping in an <area> element. Used with the coords attribute. Possible values are rect (the default), circ, circle, POLY, and polygon. |
style | Is an inline style indicating how to render the element. |
tabindex | Specifies the tab sequence of hyperlinks in the page for keyboard navigation. (XHTML 1.0 Strict, XHTML 1.0 Transitional, XHTML 1.0 Frameset.) |
target | Indicates the named frame that serves as the target of the hyperlink. (XHTML 1.0 Transitional, XHTML 1.0 Frameset.) |
title | Contains the title of the element (which might be displayed in ToolTips). |
type | Specifies the Multipurpose Internet Mail Extensions (MIME) type of the target given in the href attribute. |
xml:lang | Specifies the base language for the element when the document is interpreted as an XML document. |
This element supports these XHTML events: onclick, ondblclick, onfocus, onblur, onmousedown, onmouseup, onmouseover, onmousemove, onmouseout, onkeypress, onkeydown, and onkeyup.
The <a> element is a big part of what makes the Web work. You use this element to create hyperlinks and anchors (an anchor can serve as the target of a hyperlink). In this element, you must set either the href attribute to set the target URI of a hyperlink, or the id attribute to create an anchor. Here's an example using href, specifying the W3C site as the target of a hyperlink:
<?xml version="1.0"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>
Using the <a> Element
</title>
</head>
<body>
<center>
<h1>
Using the <a> Element
</h1>
Want to learn more about XHTML? Go to:
<a href="http://w3c.org">W3C</a>
</center>
</body>
</html>
You can see the results of this XHTML in Figure 17.2; as is standard in HTML browsers, the hyperlink appears as underlined text (largely because the browser thinks that this is standard HTML). You can also use graphical hyperlinks if you enclose an <img> element in the <a> element.