So the point of putting the CSS in the head is to eliminate the latency in the network request, ie there is no network request for the CSS — so it the CSSOM building starts immediately. By doing this, the point in which the render tree is created happens sooner rather than later in the process.
The <body> encapsulates the contents of the document, while the <head> part contains meta elements, i.e., information about the contents. This is (typically) title, encoding, author, styling etc.
The style element should apply to the whole document, wherever it is placed, however it is common practice to always put it in the head element.
(i) The HEAD tag defines the HTML document header. (ii) It contains the information such as page title. (i) The BODY tag defines the body of the document. (ii) It contains the entire contents that will appear in the web browser window.
Just to add on to what jdelStrother has mentioned about w3 specs and ARTstudio about browser rendering.
It is recommended because when you have the CSS declared before <body>
starts, your styles has actually loaded already. So very quickly users see something appear on their screen (e.g. background colors). If not, users see blank screen for some time before the CSS reaches the user.
Also, if you leave the styles somewhere in the <body>
, the browser has to re-render the page (new and old when loading) when the styles declared has been parsed.
The most recent versions of the HTML spec now permits the <style>
tag within body elements. https://www.w3.org/TR/html5/dom.html#flow-content
Also the scoped
attribute which used to be prerequisite to have a style
tag in the body
is now obsolete.
This means, that you can use the style
tag everywhere you want, the only implications are decreased page performance due to possible reflows/repaints once the browser hits styles further down in the page tree.
Obsolete answer:
The <style>
tag isn't allowed within <body>
according to the w3 specs. (You can, of course, apply inline styles via <div style="color:red">
if necessary, but it's generally considered poor separation of style & content)
Putting CSS in body means it is loaded later. It is a technique some use to let the browser start drawing the interface faster (i.e., it removes a blocking step). This is important for user experience on SmartPhones.
I do my best to keep one small css on the <head>
and I move the rest at the bottom. For example, if a page uses JQuery UI CSS, I always move it at the bottom of the <body>
, just before the links to JQuery javascript. At least, all the non Jquery item can already be drawn.
Head is designed for (Quoting the W3C):
"information about the current document, such as its title, keywords that may be useful to search engines, and other data that is not considered document content"
See the Global structure of an HTML document. As CSS is not document content, it should be in the head.
Also every other Web developer will expect to see it there, so don't confuse things by putting it in the body, even if it works!
The only CSS you should put in the body is inline CSS, though I usually avoid inline styles.
The standards (HTML 4.01: the style element) clearly specifies that the style tag is only allowed inside the head tag. If you put style tags in the body tag the browsers will try to make the best of it anyway, if possible.
It's possible that a browser would ignore a style tag in the body if you specify a strict document type. I don't know if any current browser does this, but I wouldn't count on all future versions to be so relaxed about where you place the style element.
Although the style
tag is not allowed in the body, the link
tag is, so as long as you are referencing an external stylesheet, all browsers should render and use the CSS correctly when used in the body
.
Source: https://html.spec.whatwg.org/multipage/semantics.html#the-link-element
Two conflicting answers:
From MDN page on link tag:
A
<link>
element can occur either in the<head>
or<body>
element, depending on whether it has a link type that is body-ok. For example, the stylesheet link type is body-ok, and therefore a<link rel="stylesheet">
is permitted in the body. This isn't however best practice; it makes more sense to separate your<link>
elements from your body content, putting them in your head.
From CSS The Definitive Guide (4th Edition/2017) page 10
To successfully load an external stylesheet, link must be placed inside the head element but may not be placed in any other element.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With