HTML <link> Tag

tutorials

The HTML <link> tag is an important element used to link external resources, such as stylesheets, to an HTML document. It plays a crucial role in web development by allowing you to connect your webpage to external CSS files, favicons, and other resources. The <link> tag is placed inside the <head> section of the HTML document and helps in enhancing the design, layout, and functionality of a webpage without embedding styles or code directly within the HTML file.

Syntax of the <link> Tag

The <link> tag is self-closing and does not contain any content between the opening and closing tags. Here’s the basic syntax:

html
<link rel="stylesheet" href="style.css">
  • rel: Defines the relationship between the HTML document and the linked resource. For example, stylesheet is used to link CSS files.
  • href: Specifies the URL or path of the external resource being linked.

Attributes of the <link> Tag

The <link> tag supports several attributes that define the relationship and behavior of the linked resources. Below is a list of the most commonly used attributes:

AttributeDescription
relSpecifies the relationship between the current document and the linked resource. Common values include stylesheet,icon and preload.
hrefSpecifies the URL of the linked resource (e.g., external CSS file or favicon).
typeDefines the MIME type of the linked resource. For CSS files, it is text/css.
mediaSpecifies the media type for which the linked resource is designed (e.g., screen, print).
sizesUsed for <link> elements that define icons, to specify the icon's size.
crossoriginSpecifies how the resource should be fetched, allowing for cross-origin resource sharing (CORS).
asDefines the type of content for resources like preload or prefetch.

The most commonly used attributes for linking CSS files are rel and href.

Examples of HTML <link> Tag

Example 1: Linking an External Stylesheet

html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Linking CSS Example</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <h1>Welcome to the Styled Page</h1>
    <p>This page uses an external CSS stylesheet.</p>
</body>
</html>

In this example, the <link> tag is used to connect an external CSS file called styles.css to the HTML document. This ensures that the document will use the styles defined in that file.

Example 2: Linking a Favicon

html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Favicon Example</title>
    <link rel="icon" href="favicon.ico" type="image/x-icon">
</head>
<body>
    <h1>Website with a Favicon</h1>
    <p>This page includes a favicon in the browser tab.</p>
</body>
</html>

In this example, the <link> tag is used to add a favicon to the webpage, which will appear in the browser tab. The rel attribute is set to icon and the href attribute specifies the path to the favicon file.

FAQs About HTML <link> Tag

Q1: What is the purpose of the HTML <link> tag?
A: The <link> tag is used to link external resources, such as stylesheets or favicons, to an HTML document. It helps to separate content (HTML) from presentation (CSS) and can also be used to preload or prefetch resources.

Q2: Can the <link> tag be used to link JavaScript files?
A: No, the <link> tag is typically used for linking CSS files, favicons, or other external resources. To link JavaScript files, you should use the <script> tag.

Q3: What is the difference between the <link> tag and the <style> tag?
A: The <link> tag is used to link external CSS files, while the <style> tag is used to embed CSS directly within the HTML document. Using <link> is preferred when you want to keep your styles separate from your HTML.

Q4: Is the <link> tag required for SEO?
A: While the <link> tag itself doesn't directly impact SEO, proper use of external stylesheets and favicons linked through <link> can improve user experience, site speed, and mobile-friendliness, all of which indirectly affect SEO rankings.

Q5: Can I link multiple stylesheets using the <link> tag?
A: Yes, you can link multiple stylesheets to a single HTML document by using multiple <link> tags, each referencing a different CSS file.

tools

HTML

Related Articles