If you find this helpful, please click the Google Button to the left, if it is white, to make it turn blue or red. Thank you! (It also helps find this page again more easily.) |
The <link/> Tag in HTML 5
The <link/> tag can be used to identify an external resource that is to be loaded with a web page or a hypertext link to another page. Which purpose the <link>
tag serves depends on the value of the rel
attribute.
Some uses of the <link> tag include:
- specify a favorites icon to be displayed at the beginning of the address bar
- specify default and alternate style sheets
- link to a feed for the page or web site
- link to profile(s) of the current web page author(s)
<link rel="shortcut icon" sizes="16x16" type="image/vnd.microsoft.icon" href="/favicon.ico"></link> <link rel="stylesheet" type="text/css" media="screen" href="/screen.css"/> <link rel="feed" href="http://www.HTML-5.com/feeds/html5-tutorial.xml"/>
For a good example of alternate style sheets, go to the differences section at the bottom of this article on the <link/> tag and then select the "Incorrect code in red" style from the View -> Page Style menu in Firefox or from the View -> Style menu in IE.
<a> tag for links to other HTML pages
To style text as hypertext links, highlighted with underlines, that that the user can click on to navigate to other web pages, use the HTML <a> tag.
<link/> Tag Syntax
<?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet ...?> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> ... metadata content ... <meta http-equiv="..." content="..."/> ... metadata content ... <title>My Title</title> ... metadata content ... <meta name="..." content="..."/> ... metadata content ... </head> <body> ... flow content ... <meta itemprop="..." content="..."/> ... flow content ... </body> </html>
Rules for coding the HTML link element
Make sure you understand the difference between a tag and element and are familiar with the definitions of namespace and other HTML terms.
<link/> tag as metadata content
- Code any <link> tags in the <head> section of the HTML document where metadata content is expected.
- Each link element consists of a standalone <link/> tag. The element name uses lower case letters and should be in the HTML namespace, which it will pick up automatically from the
xmlns
attribute on the <html> tag. - Include a
rel
attribute within the <link/> tag. The value of the attribute, which is enclosed in double quotes, is the type of relationship between the linked resource and the current document. - Include an
href
attribute within the <link/> tag with the URL of the external resource. - Include any other attributes of the <link/> tag as appropriate.
- Since the link element is a void element, it should always be coded as a self-closing tag terminated with the delimiter string
/>
.
<link/> tag for microdata properties
- Code the <link> tag in the <body> section of the HTML document where phrasing content is expected.
- The link element consists of a standalone <link/> tag. The element name uses lower case letters and should be in the HTML namespace, which it will pick up automatically from the
xmlns
attribute on the <html> tag. - Inside the <link> tag, code an
itemprop
attribute to indicate the link element represents a microdata property. The value of the attribute, which is enclosed in double quotes, is the name or URL of the microdata property. - Inside the <link/> tag, code an
href
attribute with the value of the microdata property in double quotes. - Include any other attributes of the <link/> tag as appropriate.
- Since the
<link/>
tag is a void element, it should always be coded as a self-closing tag terminated with the delimiter string/>
.
<link/> Content Model
Contents of the <link/> Tag
Content: Empty. All properties are coded using attributes.
Since the <link/>
tag is a void element, it is not allowed to have any content, even HTML comments and therefore should always be coded as a self-closing standalone tag, ending with the delimiters />
rather than just >
(<link .../>
).
<link/> Tag Attributes
Attributes of the <link/> tag
global attributes | In addition to the local attributes of the <link> tag below, any of the common HTML attributes can also be coded. The itemprop attribute is required when the <link/> tag is used where flow content or phrasing content is expected. |
href |
URL of the referenced document or any other URI reference (or IRI reference depending on the Use percent escape codes as explained in the URL Encoding Tutorial for any special characters in the URI reference. If the value of the
If the protocol scheme, username, host name/IP address and port number are omitted the default is the current host - the same server as the base of the current document. If the path starts with a slash |
hreflang |
indicates the language of the referenced document |
media |
The value of the media attribute is a media type or a comma-separated list of media types indicating an "or" relationship. Some browsers may also recognize expressions with media queries, such as Continuous media types
Hybrid (continuous or paged) media types
Paged media types
These media query values can also be used in the |
rel="acquaintance" rel="alternate" rel="alternate stylesheet" rel="apple-touch-icon" rel="apple-touch-startup-image" rel="archives" rel="author" rel="appendix" rel="bookmark" rel="chapter" rel="child" rel="co-resident" rel="co-worker" rel="colleague" rel="contact" rel="contents" (use rel="index" instead)rel="copyright" rel="crush" rel="date" rel="external" rel="feed" rel="feed alternate" rel="first" rel="friend" rel="glossary" rel="help" rel="icon" rel="index" rel="kin" rel="last" rel="license" rel="me" rel="met" rel="muse" rel="neighbor" rel="next" rel="nofollow" rel="noreferrer" rel="parent" rel="pingback" rel="prefetch" rel="prev" rel="search" rel="section" rel="shortcut icon" (use rel="icon" instead)rel="sibling" rel="sidebar" rel="spouse" rel="start" (use rel="first" instead)rel="stylesheet" rel="subsection" rel="sweetheart" rel="tag" rel="up" |
The
|
sizes |
Used only with rel="icon" for a shortcut/favorites icon, the value provides one or more pairs of width and height values, specified as positive integers and joined by an "x" (width "by" height) or the keyword "any" separated by spaces. |
title |
Although title is one of the common attributes, which can be coded on any HTML element, it performs somewhat differently when coded on the link tag. The title may appear as a tool tip when the mouse hovers over the link. For rel="alternate stylesheet" links, the values of the title attribute identify groups of alternate style sheets that may optionally be used to view the document. In addition, the value of the title attribute of the link tag is not inherited from its ancestors. Line breaks in the title attribute are preserved. Therefore, the title should not be split across multiple lines. |
type |
|
<link/> Tag Examples
Examples of the <link/> tag in HTML 5
Link to favorites icons of various sizes
<link rel="icon" sizes="128x128" type="image/vnd.microsoft.icon" href="/favicon_128x128.ico"/> <link rel="icon" sizes="64x64" type="image/vnd.microsoft.icon" href="/favicon_64x64.ico"/> <link rel="icon" sizes="48x48" type="image/vnd.microsoft.icon" href="/favicon_48x48.ico"/> <link rel="icon" sizes="32x32" type="image/vnd.microsoft.icon" href="/favicon_32x32.ico"/> <link rel="icon" sizes="16x16" type="image/vnd.microsoft.icon" href="/favicon_16x16.ico"/> <link rel="shortcut icon" sizes="16x16" type="image/vnd.microsoft.icon" href="/favicon.ico"></link>
The last link element, which points to the favicon.ico default location and is closed with an ending </link>
tag, is for Internet Explorer, which treats the "shortcut icon"
relationship in a non-standard way as a single value rather than a space-separated list. The slash at the beginning of the href
attributes indicates that the favorites icons are in the default location at the root of the web site's document tree.
Link to default and alternate style sheets
<link rel="alternate stylesheet" type="text/css" title="Printer-Friendly" href="/print.css"/> <link rel="stylesheet" type="text/css" media="print" href="/print.css"/> <link rel="stylesheet" type="text/css" media="screen" href="/screen.css"/> <link rel="stylesheet" type="text/css" media="handheld" href="/handheld.css"/>
The slash at the beginning of the href
attributes indicates that the style sheets are in the default location at the root of the web site's document tree.
The user can select an alternate stylesheet using options provided by some browsers, such as the Page Style menu in Firefox. To see this in action go to the differences section at the bottom of this article and select the "Incorrect code in red" style from the View -> Page Style menu in Firefox or the View -> Style menu in IE.
Link in a single blog post, pointing to the feed containing it
<link rel="feed" href="http://www.HTML-5.com/feeds/html5-tutorial.xml"/>
Link in index summary of blog posts, pointing to a feed with the same items
<link rel="feed alternate" href="http://www.HTML-5.com/feeds/html5-tutorial.xml"/>
The <link> tag in the feed <channel> would probably point back to the document containing this link.
Changes in HTML 5 - <link/> Tag
What's new in HTML 5
- Many new possible values of the
rel
attribute have been added. - Additional values of the
rel
attribute can be added if they go through a formal approval process. The XFN values have already gone through the process and have been accepted.
In addition, the sizes attribute, used with the rel="icon" relationship, has been added.
Differences between HTML 5 and earlier versions of HTML
The following values of the rel
attribute are no longer valid:
rel="appendix"
rel="chapter"
rel="contents"
(userel="index"
instead)rel="copyright"
rel="glossary"
rel="section"
rel="shortcut icon"
(userel="icon"
instead)rel="start"
(userel="first"
instead)rel="subsection"
In ployglot HTML documents the <link/>
tag is coded as a self-closing tag the same as in the 2000-2010 Recommendations from the W3C HTML Working Group. Those specifications changed the ending delimiter to />
from >
in older recommendations. They also defined the HTML namespace for the link element type name and the names of other HTML element types.