skip to Main Content

Can the <link> tag in HTML be outside the <head> element?
If not, why?
I want to know if the tag can be placed outside of the <head> element

2

Answers


  1. First of all, you should learn to do your own research afford. Such questions can be easily answered with own research effort by reading the documentation (API).

    Specifically, there are 2 institutions that specify everything within the web stack:

    • W3C (World Wide Web Consortium)
    • WHATWG (Web Hypertext Application Technology Working Group)

    The easiest way to read into the documentation as a user is to use the MDN Web Docs (Mozilla Developer Network Web Documentation).

    If you look at the MDN Web Docs for the link tag you will find a section for Specifications which in this case will lead to the WHATWG Specifications for the link tag.

    The WHATWG Specification explicit states:

    If the rel attribute is used, the element can only sometimes be used in the body of the page. When used with the itemprop attribute, the element can be used both in the head element and in the body of the page, subject to the constraints of the microdata model.

    That means that according to the official specifications the link tag can be used within the body in certain cases.

    Login or Signup to reply.
  2. It can, under some conditions.

    If it has a rel attribute that is body-ok, i.e. one of dns-prefetch, modulepreload, pingback, preconnect, prefetch, preload, prerender, and stylesheet, or if it has an itemprop attribute, then it is allowed in the body. If it doesn’t meet either of this conditions, it is not allowed in the body, but it won’t be a parse error either, and thus won’t be moved away in the DOM, but its action may not work.

    console.log(document.querySelector("#link").parentNode === document.body);
    // It's still in the body, but the favicon wouldn't be set by this tag
    <!-- we're in the <body> here -->
    <link rel="icon" href="" id="link">
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search