HTML <link> Element

Publish in HTML Tutorial el 24/05/2025 16:36

The <link> HTML element specifies relationships between the current document and external resources. This element is most commonly used to link to stylesheets, but is also used to establish site icons (favicons) and other relationships between documents.


Key attributes of <link>:

  • href: Specifies the location of the linked resource
  • rel: Defines the relationship between documents
  • type: Indicates the MIME type of the linked resource
  • media: Specifies which media/device the resource is optimized for


Basic Syntax

Here's how a basic <link> element looks in HTML:


Examples of <link> Usage

1. Linking a CSS Stylesheet

The most common use of <link> is to connect an external CSS file to your HTML document.

Play Code


2. Adding a Favicon

The <link> element is used to specify favicons for your website.

Play Code


3. Preloading Resources

You can use <link> with rel="preload" to tell the browser to start loading critical resources early.

Play Code


4. JavaScript-Enabled Stylesheet Switching

This example shows how to use JavaScript with <link> to switch between stylesheets.

Play Code

How does it work?

  • Bootstrap 5.3+ includes support for dark mode using data-bs-theme="dark" or "light" in the <html>
  • The button calls switchTheme(), which simply toggles that attribute.
  • All components (buttons, cards, etc.) automatically change their appearance without reloading CSS.


5. Dynamic Resource Loading with JavaScript

This example demonstrates how to dynamically load a CSS file using JavaScript.

Play Code


Tips and Tricks

  • Performance Tip: Place <link> elements in the <head> to allow browsers to discover and load resources early.
  • Multiple Icons: You can provide multiple favicon sizes for different devices by using multiple <link> elements with different sizes.
  • DNS Prefetch: Use <link rel="dns-prefetch" href="//domain.com"> to perform DNS lookups for external domains in advance.
  • Media Queries: The media attribute allows you to load different stylesheets for different devices or screen sizes.
  • Crossorigin: When loading fonts from CDNs, use crossorigin="anonymous" to ensure proper CORS handling.


Browser Support

The <link> element is supported by all browsers, though some relationship types (like preload) may have varying support in older browsers.

También te puede interesar

HTML <style> Element
HTML <style> Element

The <style> HTML element contains style information for a document, or part of a document. ...

HTML <strong> Element
HTML <strong> Element

The <strong> element is used to indicate that its content has strong importance, seriousnes...

HTML <span> Element
HTML <span> Element

The <span> element is an inline container used to mark up a part of a text or document. Unl...

HTML <source> Element
HTML <source> Element

The <source> element is used to specify multiple media resources for media elements like &l...

HTML <small> Element
HTML <small> Element

The <small> element is used to represent side-comments and small print, typically for discl...

HTML <section> Element
HTML <section> Element

HTML <section> Element: The Complete Guide The <section> element is a semantic HTML ...