Strictly speaking, do style
tags need to be inside the head
of an HTML document? The 4.01 standard implies that, but it's not explicitly stated:
The STYLE element allows authors to put style sheet rules in the head of the document. HTML permits any number of STYLE elements in the HEAD section of a document.
I say "strictly speaking" because I have an app that puts style elements inside the body, and all the browsers I've tested with seem to use the style elements. I'm just wondering if that's actually legal.
Like the other replies have stated it doesn't actually need to be there. However, it will not validate. This may or may not matter in this instance, but please keep in mind that rendering of html is entirely up to the browsers. From what I know all used browsers of today will support putting it outside the head, but you cannot guarantee that for the future browsers and future browser releases.
Stick with the standard and you are safer. How much safer is up for very much debate.
While the other answers are correct, I'm surprised nobody has explained where the standards disallow styles outside of
head
.It's actually in the section on The
head
Element (and in the DTD):Yes, I know. DTDs are hard to read.
This is the only place where the
STYLE
element occurs, so implicitly it's invalid elsewhere.style
is supposed to be included only on thehead
of the document.Besides the validation point, one caveat that might interest you when using
style
on thebody
is the flash of unstyled content. The browser would get elements that would be styled after they are displayed, making them shift on size/shape/font and/or flicker. It is generally a sign of bad craftsmanship. Generally you can get away with puttingstyle
anywhere you want, but try to avoid it whenever it is possible.HTML 5 introduced a
scoped
attribute that allowedstyle
tags to be included everywhere in the body, but then they removed it again.You can use style tag inside head section or body section or also outside of html tag also(out side html is not recommended). In real time projects many time you can see they use style tag out side of html tag
According to the HTML 5.2 specification (in draft), the style tag is only allowed in the head of a document.
HTML 5.2 Draft on Style Tag (Aug 18, 2016)
They aren't supposed to go outside the head, but they work anyway; though you might notice a quick flicker. The site shouldn't validate with the style tag outside of the head, but does that really matter? Also, link tags work outside the head as well, even though they aren't supposed to.