Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML & CSS - put <link> tag outside of the <head> [duplicate]

People also ask

What is HTML used for?

HTML is the language for describing the structure of Web pages. HTML gives authors the means to: Publish online documents with headings, text, tables, lists, photos, etc. Retrieve online information via hypertext links, at the click of a button.

Is HTML easy to learn?

HTML is perhaps one of the easiest front-end programming languages to master. So if you want to learn HTML, then go for it! With patience and practice, you'll learn to make the most of this popular language.

Is HTML a coding?

What you're looking at right now is HTML code, read and interpreted by your browser. But this doesn't make HTML a programming language. HTML is a type of markup language. It encapsulates, or “marks up” data within HTML tags, which define the data and describe its purpose on the webpage.


Style sheets are linked in the <head> so that the browser can style the HTML and render it as it goes. If you put the style information at the bottom of the document the browser will have to restyle and render the whole document from the top again.

This firstly, takes longer, and secondly, looks really ugly.

This differs from included scripts as scripts will block loading until they are done, so you load them as late as possible in the process.


According to the W3 specs, <link> tags are only supposed to go in the <head> section:

References

For HTML 4.01: http://www.w3.org/TR/html401/struct/links.html#edef-LINK

For HTML5: http://www.w3.org/TR/html5/document-metadata.html#the-link-element

Validation Issues: Updated October 27, 2017

Back in 2013, if you put a link tag within the body of the HTML document, it would not validate using validate.w3.org with rules based on HTML 4.01.

(You can try out HTML 4.01 versus HTML 5.0 validation at https://validator.nu)

On a first reading, the HTML 5.0 specification document seems to imply that link's should appear only in the head element of the document. However, if you validate using a HTML 5.0 validator, then the documents appears okay even if you have a link in the flow content.

The best explanation for this discrepancy may be as follows.

If you read the MDN documentation for the link entry (MDN Link entry), you see that if the link element has an itemprop attribute, then the link can appear in flow and phrasing content, thus, in the body.

This may be the reason why HTML 5.0 validators do not issue a warning even if the itemprop attribute is not present.

The itemprop is part of the microdata specification and is relatively new (read about HTML Microdata) and it is worth reading.

For the moment, one could add a link to a stylesheet within the body, but it is not clear what the advantages are.


This is an old discussion, but I think it's worth noting in here that Google Pagespeed Insights actually now (2017) recommends deferring the loading of large CSS files until below the fold to ensure they don't block loading of html.


Yes, it's okay with HTML5 specifications to put a link element inside the body element. If it's a bad or good idea depends on what your linking. If it's not crucial to render the first view of your site then I'd consider it a good idea to load it as late as possible.