Home >Web Front-end >CSS Tutorial >rel (HTML attribute)

rel (HTML attribute)

Lisa Kudrow
Lisa KudrowOriginal
2025-02-27 10:09:16821browse
<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).

rel (HTML attribute)
Example of a stylesheet title in a browser's menu.

RSS/Atom Feeds

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>

Favicons

To specify a favicon (website icon), use rel="shortcut icon":

<code>
<link href="/favicon.ico" rel="shortcut icon">
</code>

Page Relationships (Navigation)

For sequential pages, rel="next" and rel="prev" define the next and previous pages in a series.

Licenses

You can indicate the license for page content using rel="license" and providing a link to the license details.

Custom Relationships

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.

Frequently Asked Questions

This section will answer common questions about the `rel` attribute.

What is the purpose of the rel HTML attribute?

The rel attribute specifies the relationship between the current HTML document and the linked resource.

How does the rel attribute work with stylesheets?

rel="stylesheet" indicates the linked resource is a CSS stylesheet.

Can the rel attribute be used for SEO purposes?

Yes, values like rel="nofollow" and rel="canonical" influence search engine behavior.

What does the rel="alternate" attribute do?

rel="alternate" specifies alternative versions of a document (e.g., different languages).

How is the rel attribute used with the a tag?

Similar to <link>, it adds semantic meaning to hyperlinks (e.g., rel="nofollow").

Can I use multiple values in the rel attribute?

Yes, separate multiple values with spaces (e.g., rel="nofollow noopener").

What is the rel="author" attribute used for?

Specifies the author of a document (though its support is limited).

What does the rel="icon" attribute do?

Specifies a favicon for a website.

What is the rel="preload" attribute used for?

Specifies resources to be loaded in advance to improve website performance.

What is the difference between rel="next" and rel="prev"?

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn