Section 12: Links
Introduction to links and anchors
-
This section introduces the link (or hyperlink, or Web link), the basic hypertext construct. A link is a connection from one Web resource to another.
-
A link has two ends (also called anchors) and a direction. The link starts at the "source" anchor and points to the "destination" anchor, which may be any Web resource (e.g., an image, a video clip, a sound bite, a program, an HTML document, an element within an HTML document, etc.).
12.1.1 Visiting a linked resource
- The default behavior associated with a link is the retrieval of another Web resource.
- This behavior is commonly and implicitly obtained by selecting the link (e.g., by clicking, through keyboard input, etc.).
- The
href
attribute in each source anchor specifies the address of the destination anchor with a URI.
- The destination anchor of a link may be an element within an HTML document.
- two ways to achieve it:
- by the
A
element (naming it with the name
attribute)
- by any other element (naming with the
id
attribute).
12.1.2 Other link relationships
- Authors may insert links in their documents that express other relationships between resources. Links that express other types of relationships have one or more link types specified in their source anchor.
- The roles of a link defined by
A
or LINK
are specified via the rel
and rev
attributes.
12.1.3 Specifying anchors and links
-
The LINK
element may only appear in the head of a document. The A element may only appear in the body.
-
When the A
element's href
attribute is set, the element defines a source anchor for a link that may be activated by the user to retrieve a Web resource.
-
The source anchor is the location of the A
instance and the destination anchor is the Web resource.
-
The retrieved resource may be handled by the user agent in several ways:
- by opening a new HTML document in the same user agent window
- opening a new HTML document in a different window
- starting a new program to handle the resource, etc.
12.1.4 Link titles
- The
title
attribute may be set for both A
and LINK
to add information about the nature of a link. This information may be spoken by a user agent, rendered as a tool tip, cause a change in cursor image, etc.
12.1.5 Internationalization and links (adding)
- the A and LINK elements support the charset attribute. This attribute allows authors to advise user agents about the encoding of data at the other end of the link.
12.2 The A element
- Start tag: required, End tag: required
- Attribute definitions
- name = cdata [CS]
- This attribute names the current anchor so that it may be the destination of another link. The value of this attribute must be a unique anchor name. The scope of this name is the current document.
- href = uri [CT]
- specify the location of a Web resource, thus defining a link between the current element (the source anchor) and the destination anchor
- hreflang = langcode [CI]
- specify the base language of the resource designated by href and may only be used when href is specified.
- type = content-type [CI]
- gives an advisory hint as to the content type of the content available at the link target address. It allows user agents to opt to use a fallback mechanism rather than fetch the content if they are advised that they will get content in a content type they do not support.
- rel = link-types [CI]
- describe the relationship from the current document to the anchor specified by the href attribute. The value of this attribute is a space-separated list of link types.
- rev = link-types [CI]
- describe a reverse link from the anchor specified by the href attribute to the current document. The value of this attribute is a space-separated list of link types.
- charset = charset [CI]
- specify the character encoding of the resource designated by the link.
- Attributes defined elsewhere
- id, class (document-wide identifiers)
- lang (language information), dir (text direction)
- title (element title)
- target (target frame information)
- tabindex (tabbing navigation)
- …
- Examples:
- the
A
element defines a link. The source anchor is the text "W3C Web site" and the destination anchor is "http://www.w3.org/"
- To tell user agents what the character encoding of the destination page is, set the
charset
attribute:
- Link a defined anchor from the same or another document
- first, define a anchor named "anchor-one" in the file "one.html".
- then, a link defined in the file "two.html" in the same directory as "one.html" would refer to the anchor
- How to?
- URIs that designate anchors contain a "#" character followed by the anchor name (the fragment identifier). Here are some examples of such URIs:
12.2.1 Syntax of anchor names
- An anchor name is the value of either the
name
or id
attribute. Anchor names must observe the following rules:
- Uniqueness: Anchor names must be unique within a document. Anchor names that differ only in case may not appear in the same document.
- String matching: Comparisons between fragment identifiers and anchor names must be done by exact (case-sensitive) match.
- Legal example:
12.2.2 Nested links are illegal
-
Links and anchors defined by the A
element must not be nested; an A
element must not contain any other A elements.
-
Since the DTD defines the LINK
element to be empty, LINK
elements may not be nested either.
12.2.3 Anchors with the id
attribute
- The
id
attribute may be used to create an anchor at the start tag of any element (including the A element).
- create an anchor in an H2 element. And it is linked to via the A element.
NOTE
- The
id
and name
attributes share the same name space. This means that they cannot both define an anchor with the same name in the same document.
Like this:
- It is permissible to use both attributes to specify an element's unique identifier for the following elements:
A
, APPLET
, FORM
, FRAME
, IFRAME
,IMG
, and MAP
. When both attributes are used on a single element, their values must be identical.
- Because of its specification in the HTML DTD, the
name
attribute may contain character references. Thus, the value Dürst is a valid name attribute value, as is Dürst . The id
attribute, on the other hand, may not contain character references.
Use id or name?
12.2.4 Unavailable and unidentifiable resources
- A reference to an unavailable or unidentifiable resource is an error.
- If a user agent cannot locate a linked resource, it should alert the user.
- If a user agent cannot identify the type of a linked resource, it should still attempt to process it. It should alert the user and may allow the user to intervene and identify the document type.
12.3 Document relationships: the LINK element
- Start tag: required, End tag: forbidden
- This element defines a link. Unlike
A
, it may only appear in the HEAD
section of a document, although it may appear any number of times. Although LINK
has no content, it conveys relationship information that may be rendered by user agents in a variety of ways (e.g., a tool-bar with a drop-down menu of links).
12.3.1 Forward and reverse links
-
the rel
attribute specifies a forward link and the rev
attribute specifies a reverse link.
-
Consider two documents A and B.
Document A: <LINK href="docB" rel="foo">
Has exactly the same meaning as:
Document B: <LINK href="docA" rev="foo">
Both attributes may be specified simultaneously.
12.3.2 Links and external style sheets
- When the LINK element links an external style sheet to a document, the
type
attribute specifies the style sheet language and the media
attribute specifies the intended rendering medium or media. User agents may save time by retrieving from the network only those style sheets that apply to the current device.
12.3.3 Links and search engines
- Authors may use the
LINK
element to provide a variety of information to search engines, including:
- Links to alternate versions of a document, written in another human language.
- Links to alternate versions of a document, designed for different media, for instance a version especially suited for printing.
- Links to the starting page of a collection of documents.
Examples
- The examples below illustrate how language information, media types, and link types may be combined to improve document handling by search engines.
- the
hreflang
attribute to tell search engines where to find Dutch, Portuguese, and Arabic versions of a document.
- the
charset
attribute for the Arabic manual.
- the
lang
attribute to indicate that the value of the title
attribute for the LINK
element designating the French manual is in French.
- In the following example, we tell search engines where to find the printed version of a manual.
- we tell search engines where to find the front page of a collection of documents.
-
Start tag: required, End tag: forbidden
-
In HTML, links and references to external images are always specified by a URI. Relative URIs are resolved according to a base URI, which may come from a variety of sources. The BASE
element allows authors to specify a document's base URI explicitly.
-
When present, the BASE
element must appear in the HEAD
section of an HTML document, before any element that refers to an external source. There can be only one <base> element in a document.
-
For example, given the following BASE
declaration and A
declaration:
12.4.1 Resolving relative URIs
- User agents must calculate the base URI according to the following precedences (highest priority to lowest):
- The base URI is set by the BASE element.
- The base URI is given by meta data discovered during a protocol interaction, such as an HTTP header (see [RFC2616]).
- By default, the base URI is that of the current document. Not all HTML documents have a base URI (e.g., a valid HTML document may appear in an email and may not be designated by a URI). Such HTML documents are considered erroneous if they contain relative URIs and rely on a default base URI.