The <link> element in HTML defines relationships between the current document and an external source. The <link> element is mostly used to link stylesheets or site icons. The <link> element is an empty element that only contains attributes.
All major browsers support the <link> element when used for linking stylesheets.
The <link> element includes the following attributes:
crossorigin: Defines how cross-origin requests are to be handled.href: The absolute or relative hreflang: The language of the text in the linked resource; this attribute should only be used if href is present.media: The type of device the linked document will be displayed on.referrerpolicy: Defines which referrer should be used when fetching the resource.rel: A required attribute that specifies the relationship between the current document and the linked resource.sizes: The dimensions of the linked resource; this is only applicable when an icon is linked.title: A preferred or alternate stylesheet.type: The media type of the linked resource.Note: You can view an exhaustive list of attributes here.
Generally, the <link> tag is nested under the <head> tag. The code below shows how to use the <link> tag:
<head>
<link href="main.css" rel="stylesheet">
</head>
The above code will import all the style rules defined in the linked stylesheet. These rules can be overridden by using the <style> element directly in the current document.
You can also use the <link> element to provide alternate stylesheets, as shown below:
<link href="first.css" rel="stylesheet" title="First Stylesheet">
<link href="second.css" rel="alternate stylesheet" title="Second Stylesheet">
<link href="third.css" rel="alternate stylesheet" title="Third Stylesheet">
The first <link> tag is the default stylesheet used in the example shown above. The other <link> tags have their rel attribute set to alternate stylesheet, which specifies to the document that these stylesheets are alternatives to the default. Thus, viewers of a page can choose which particular stylesheet they wish to see.
Note: You can find further examples of the
<link>element’s usage here.
The code below shows how the <link> element works:
The <link> element in line 3 of the HTML code links the CSS file to the current document. As a result, the styling rules are imported, and the output text appears to be blue in color.
Free Resources