Is <link> (not rel=“stylesheet”) allowed to be used in <body>?

后端 未结 7 483
情书的邮戳
情书的邮戳 2020-12-07 20:24

The new schema.org by Google, Yahoo and MS recommends usage of the attribute to display the status of products in an online shop:

&         


        
相关标签:
7条回答
  • 2020-12-07 20:59

    To make sure the code is cross-browser compatible, rather include your stylesheet like this:

    <style>
       @import url(style.css);
    </style>
    

    Putting a stylesheet in the body is usually considered bad practice, however it can be helpful in some cases:

    1. You want the style to be loaded prior to other elements
    2. If you include certain frequently occuring website's elements (a contact box, a menu header, etc) by php, you can put the link to the corresponding stylesheet into the php template. Once you include the template, the stylesheet will be automatically loaded without having to add anything to the head section.
    0 讨论(0)
  • 2020-12-07 21:00

    I'd like to add to the answers above, in short

    <body>
        <link rel="stylesheet" property="stylesheet" href="pathto.css">
    </body>
    

    is making the valdation error go away. Even just adding property="" (RDFa syntax or itemprop="" (Microformat syntax) attribute is sufficient. As @Jukka K. Korpela and @sideshowbarker explain in their answers, the reason lies in the HTML5+RDFa 1.1 spec.

    The solution above is basically a workaround to let validator ignore inline stylesheets as needed in critical path implementations. In future versions of validators it hopefully gets obsolete.

    By the way, in HTML5 you neither need a type attribute nor self-closing tag syntax.

    0 讨论(0)
  • 2020-12-07 21:02

    Link is allowed in BODY. I had same problem validating link tag in HTML5 and I solved it with this

    <link rel="stylesheet" property="stylesheet" href="css/homepage.css">
    

    Need to have both property and rel tag

    UPDATE 2016 (thanks to yuyokk below): There was a change to HTML5 spec recently that allows having links in the body

    0 讨论(0)
  • 2020-12-07 21:05

    There was a change to HTML5 spec recently that allows having links in the body

    0 讨论(0)
  • 2020-12-07 21:07

    The WHATWG HTML specification mentions, that the LINK-element can either have a rel-attribute:

    <link rel="…" />
    

    or an itemprop-attribute

    <link itemprop="…" />
    

    but not both.

    The rel-version is restricted to the HEAD-element, whereas the itemprop-version may appear in both, the HEAD and BODY-elements.

    http://www.whatwg.org/specs/web-apps/current-work/multipage/semantics.html#the-link-element

    What is this WHATWG specification:

    whatwg.org/specs/web-apps/current-work/multipage/introduction.html#is-this-html5?

    0 讨论(0)
  • 2020-12-07 21:08

    The specification as of March 2020:

    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.

    which is the opposite to what the accepted answer says as far as the rel attribute is concerned. I have personally never experienced issues by adding rel in the head though.

    Microdata is a WHATWG HTML specification used to nest metadata within existing content on web pages. Browsers, web crawlers and in particular search engines can extract and process Microdata from a web page and use it to provide a richer browsing experience for users.

    0 讨论(0)
提交回复
热议问题