HTML link Tag: Linking Stylesheets

html link tag

The link tag in HTML connects a web page to external resources like stylesheets or icons. It’s placed in the <head> and doesn’t display content.

The <link> tag defines a link between a document and an external resource. It does not create a visible link. It sits in the document head. It connects stylesheets and other external files.

Here is the tag

<link rel="stylesheet" href="style.css">

Here are its attributes:

  • rel Defines the relationship between the HTML and the external file. Common value is stylesheet.
  • href Gives the path or URL of the linked resource. It must point to a valid file.
  • type Describes the content type of the linked file. CSS files use text/css.
  • media Applies the link only for specific media types or conditions. Use it to load styles for print or screen.
  • sizes Applies to icons. Defines icon sizes like 16x16 or 32x32.

Place it inside the element. Browsers load it before the page appears.. All major browsers support the <link> tag. It works across desktop and mobile.

Use to connect a CSS file to your HTML page. This lets you style your content.

Here is the structure you need:

<head>
  <link rel="stylesheet" href="styles.css" type="text/css">
</head>

What this means is important. The browser reads the HTML. It finds the <link> tag. It then loads the CSS file. It applies styles to the page.

You must use the correct path. If the file sits in the same folder, use a relative path. If the file sits on another site, use an absolute URL.

This gives you separation between structure and style. This reduces code clutter. It lets you reuse styles across many pages.

You can also use to define icons and alternate stylesheets.

<link rel="icon" href="favicon.ico" type="image/x-icon">

This connects a small image as the browser tab icon.

Preload Resource:

<link rel="preload" href="font.woff2" as="font" type="font/woff2" crossorigin="anonymous">

This tells the browser to load the font early.

Alternate Stylesheet:

<link rel="alternate stylesheet" href="theme-dark.css" title="Dark Mode">

This sets up a secondary stylesheet users can switch to.

The <link> defines a resource relationship. It works behind the scenes. The <a> defines a hyperlink. It appears in the page. Users can click it.

Here is the use case:

  • Use to load CSS files and preloads
  • Use to link users to another page or section

Examples

Load a CSS File:

<head>
  <link rel="stylesheet" href="main.css">
</head>

This example loads a CSS file named main.css. The file sits in the same folder. The browser reads it and applies styles to the page.

Add a Favicon:

<head>
  <link rel="icon" href="/favicon.ico" type="image/x-icon">
</head>

This example adds a favicon to the browser tab. The file must exist at the given path.

Preload a Font:

<head>
  <link rel="preload" href="/fonts/myfont.woff2" as="font" type="font/woff2" crossorigin>
</head>

This example tells the browser to load a font early. This reduces layout shifts.

Alternate Stylesheet:

<head>
  <link rel="alternate stylesheet" href="night.css" title="Night Mode">
</head>

This example sets up a secondary theme. It allows a switch without a change to the main stylesheet.

Wrapping Up

In this article, you learned what the <link> tag does and how it works. You also saw how to link CSS and more.

Here is a quick recap:

  • Use to connect external files like CSS or icons
  • Place it in the
  • Use attributes like rel and href
  • Use for background tasks. Use for user-visible links

FAQs

What is the purpose of the <link> tag in HTML?

The <link> tag links external resources. These include stylesheets, icons, or preloaded files. It does not appear on the page.

Can I place the <link> tag in the body?

No. Always place the <link> tag in the <head>. The browser reads it before showing the page.

How do I link a CSS file to HTML?

Use <link rel="stylesheet" href="style.css"> inside the <head> tag. This tells the browser to load and apply the CSS.

What is the difference between <link> and <a> tags?

The <link> tag links to resources like CSS. It does not appear on the page. The <a> tag creates hyperlinks users can click.

Similar Reads

HTML audio Tag: How to Embed and Control Sound in Pages

The audio tag adds sound to web pages without plugins. It gives you control to play, pause, loop, or mute…

HTML Link Attributes Guide for Beginners

HTML link attributes tell the browser how a link works. They set where the link goes, how it opens, and…

The em Tag in HTML: How to Emphasize Text in HTML

The HTML em tag shows stress or emphasis in text. It changes meaning for readers and screen readers. What is…

HTML source Tag: How to Embed Media with Examples

The source tag in HTML helps load different media formats and screen sizes. You can use it inside many other…

Comments in HTML: How They Work with Examples

Comments in HTML help you explain your code. You can add notes without showing anything in the browser. Understand How…

HTML Breadcrumb Navigation Guide for Beginners

The breadcrumb in HTML shows a path through a site and marks the place of a page inside it. It…

HTML Small Tag: How to Make Text Small

The HTML small tag displays text in a smaller font size than nearby text. It shows less important or side…

HTML output Tag: How to Display Calculation Results

The <output> tag in HTML shows results inside a form. It helps display values from user actions or scripts. You…

HTML title Attribute: How it Works with Examples

The HTML title attribute adds extra info to any element. You can use it to show tooltips on hover. What…

HTML noscript Tag: How to Display Fallback When JS Is Disabled

The HTML noscript tag tells the browser what to show when JavaScript does not run. This tag targets users who…

Previous Article

HTML style Tag: Inline CSS

Next Article

HTML title Tag: How to Set Web Page Titles

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *


Subscribe to Get Updates

Get the latest updates on Coding, Database, and Algorithms straight to your inbox.
No spam. Unsubscribe anytime.