Home >Web Front-end >CSS Tutorial >rel (HTML attribute)
<code class="language-html"><!DOCTYPE html> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Understanding the HTML rel Attribute</title> <h1>The `rel` Attribute: Defining Relationships in HTML</h1> <p>The <code>rel</code> attribute specifies the relationship between the current document and the linked resource. It's primarily used with the <code><link></code> and <code><a></code> tags to provide context and meaning to links.</p> <h2>Common Uses of the `rel` Attribute</h2> <h3>Stylesheets</h3> <p>The most common use is linking stylesheets: <code><link rel="stylesheet" href="styles.css"></code>. This clearly indicates the linked file is a stylesheet.</p> <h3>Alternate Stylesheets</h3> <p>You can define multiple stylesheets using <code>rel="alternate stylesheet"</code>. However, browser support for easily switching between these is limited. A JavaScript-based solution is often necessary for user selection.</p> <pre class="brush:php;toolbar:false"><code> <link href="main.css" media="screen" rel="stylesheet" type="text/css"> <link rel="alternate stylesheet" title="Higher Contrast" href="contrast.css" type="text/css" media="screen"> <link rel="alternate stylesheet" title="Gratuitous CSS" href="hot.css" type="text/css" media="screen"> </code>
Note the use of the title
attribute. This text appears in the browser's style sheet selection menu (where available).
The rel="alternate"
attribute is also used to link to alternative content formats like RSS or Atom feeds:
<code> <link rel="alternate" type="application/rss+xml" href="/rss.xml" title="RSS 2.0"> <link rel="alternate" type="application/atom+xml" href="/atom.xml" title="Atom 1.0"> </code>
To specify a favicon (website icon), use rel="shortcut icon"
:
<code> <link href="/favicon.ico" rel="shortcut icon"> </code>
For sequential pages, rel="next"
and rel="prev"
define the next and previous pages in a series.
You can indicate the license for page content using rel="license"
and providing a link to the license details.
While predefined values are recommended, you can create custom relationships. However, browsers won't inherently understand these custom values; they might be useful for JavaScript manipulation or other non-browser-specific purposes.
This section will answer common questions about the `rel` attribute.
The rel
attribute specifies the relationship between the current HTML document and the linked resource.
rel="stylesheet"
indicates the linked resource is a CSS stylesheet.
Yes, values like rel="nofollow"
and rel="canonical"
influence search engine behavior.
rel="alternate"
specifies alternative versions of a document (e.g., different languages).
Similar to <link>
, it adds semantic meaning to hyperlinks (e.g., rel="nofollow"
).
Yes, separate multiple values with spaces (e.g., rel="nofollow noopener"
).
Specifies the author of a document (though its support is limited).
Specifies a favicon for a website.
Specifies resources to be loaded in advance to improve website performance.
They define the next and previous pages in a sequence of paginated content.
The above is the detailed content of rel (HTML attribute). For more information, please follow other related articles on the PHP Chinese website!