Is it valid HTML to have two <style> elements inside your HTML <head> tag?

微笑、不失礼 提交于 2019-12-22 03:41:32

问题


Is it valid to have two <style> tags inside of an HTML head tag?

For example I am trying to write an HTML page for email clients with no external stylesheets added. Due to the way our template system works it is much easier for us to have

<head>
 <style>
  .someStyle{}
 </style>
 <style>
  .someOtherStyle{}
 </style>
</head>

instead of this...

<head>
 <style>
  .someStyle{}
  .someOtherStyle{}
 </style>
</head>

If option 2 is the only way of doing this I will do that but option 1 for various reasons works better for our framework code.


回答1:


It's perfectly fine to have multiple style elements in between your <head> and </head> tags. You can have as many as you want, together with <link> or <script> elements.




回答2:


Yes, this is perfectly valid. CSS stands for "cascading style sheets" (as in, layered on top of each other.) Multiple <style> tags allow you to include multiple style sheets or multiple style declarations.




回答3:


None of the answers have cited any sources, so the OP cannot be sure. (Note that neither the "cascading" part of "CSS" nor the fact that browsers tolerate many STYLE elements proves anything; e.g. they tolerate (thank God, BTW!) STYLE elements in the BODY as well, even though that's considered heresy by the specs, unless the scoped attr. is used, which is in turn largely unsupported by browsers, despite being valid...)

Here's what the current HTML spec says today (2017-02-08):

Contexts in which this element can be used:

  • If the scoped attribute is absent: where metadata content is expected.
  • If the scoped attribute is absent: in a noscript element that is a child of a head element.
  • If the scoped attribute is present: where flow content is expected, but before any other flow content other than other style elements and inter-element whitespace.

Which effectively means the answer to the original question is, indeed: yes. :)



来源:https://stackoverflow.com/questions/11677284/is-it-valid-html-to-have-two-style-elements-inside-your-html-head-tag

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!