Does <STYLE> have to be in the <HEAD>

2019-01-01 15:35发布

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.

10条回答
裙下三千臣
2楼-- · 2019-01-01 16:05

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.

查看更多
梦该遗忘
3楼-- · 2019-01-01 16:07

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):

<!-- %head.misc; defined earlier on as "SCRIPT|STYLE|META|LINK|OBJECT" -->
<!ENTITY % head.content "TITLE & BASE?">

<!ELEMENT HEAD O O (%head.content;) +(%head.misc;) -- document head -->

Yes, I know. DTDs are hard to read.

This is the only place where the STYLE element occurs, so implicitly it's invalid elsewhere.

查看更多
余生无你
4楼-- · 2019-01-01 16:08

style is supposed to be included only on the head of the document.

Besides the validation point, one caveat that might interest you when using style on the body 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 putting style anywhere you want, but try to avoid it whenever it is possible.

HTML 5 introduced a scoped attribute that allowed style tags to be included everywhere in the body, but then they removed it again.

查看更多
牵手、夕阳
5楼-- · 2019-01-01 16:08

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

查看更多
君临天下
6楼-- · 2019-01-01 16:09

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)

A style element is restricted to appearing in the head of the document.

查看更多
若你有天会懂
7楼-- · 2019-01-01 16:17

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.

查看更多
登录 后发表回答