<html>
<body>
<style type="text/css">
p.first {color:blue}
p.second {color:green}
</style>
<p class="first">Hello World</p>
<p class="second">Hello World</p>
<style type="text/css">
p.first {color:green}
p.second {color:blue}
</style>
<p class="first">Hello World</p>
<p class="second">Hello World</p>
</body>
</html>
一个浏览器应该如何渲染css是不连续的?它是否应该生成一些数据结构,使用页面上所有的css样式,并使用它进行渲染?
还是按照看到的顺序使用样式信息进行渲染?
当我看到大型站点的内容管理系统经常将一些<style>元素(一些,而不是全部)放置在依赖于这些类的内容附近时,我就得出结论说,情况已经不一样了。
去看看cnn.com, nytimes.com, huffingtonpost.com,你最近的大城市报纸,等等。他们都这样做。
If there's a good reason to put an extra <style> section somewhere in the body -- for instance if you're include()ing diverse and independent page elements in real time and each has an embedded <style> of its own, and the organization will be cleaner, more modular, more understandable, and more maintainable -- I say just bite the bullet. Sure it would be better if we could have "local" style with restricted scope, like local variables, but you go to work with the HTML you have, not the HTML you might want or wish to have at a later time.
当然,正如其他人所阐述的那样,遵循正统有潜在的缺点和好的(如果不总是令人信服的)理由。但在我看来,<style>在<body>中经过深思熟虑的使用已经越来越成为主流。
当我看到大型站点的内容管理系统经常将一些<style>元素(一些,而不是全部)放置在依赖于这些类的内容附近时,我就得出结论说,情况已经不一样了。
去看看cnn.com, nytimes.com, huffingtonpost.com,你最近的大城市报纸,等等。他们都这样做。
If there's a good reason to put an extra <style> section somewhere in the body -- for instance if you're include()ing diverse and independent page elements in real time and each has an embedded <style> of its own, and the organization will be cleaner, more modular, more understandable, and more maintainable -- I say just bite the bullet. Sure it would be better if we could have "local" style with restricted scope, like local variables, but you go to work with the HTML you have, not the HTML you might want or wish to have at a later time.
当然,正如其他人所阐述的那样,遵循正统有潜在的缺点和好的(如果不总是令人信服的)理由。但在我看来,<style>在<body>中经过深思熟虑的使用已经越来越成为主流。
对于“体内风格”爱好者来说,有一个坏消息:W3C最近在与WHATWG的HTML战争中失败了,WHATWG的无版本HTML“生活标准”现在已经成为官方标准,唉,它不允许体内风格。短暂的快乐时光一去不复返。,)W3C验证器现在也根据WHATWG规范工作。(谢谢@FrankConijn的提醒!)
(注意:这是“今天”的情况,但由于没有版本控制,链接可能在任何时候无效,而不通知或控制。除非你愿意链接到它在GitHub上提交的单个源代码,否则你基本上不能再稳定地引用新的官方HTML标准AFAIK。请纠正我,如果有一个规范的方法来正确地做这件事。)
过时的好消息:
耶,STYLE终于在BODY中生效了,从HTML5.2开始!
(scoped也消失了。)
来自W3C规范(注意最后一行!):
4.2.6. 风格元素
...
可以使用此元素的上下文:
需要元数据内容的地方。
在noscript元素中,它是head元素的子元素。
在正文中,期望流内容的地方。
元附注:
尽管遭受了“浏览器战争”的破坏,但我们仍然必须继续针对两个相互竞争的“官方”HTML“标准”(1个标准+ 1个标准< 1个标准)进行开发,这意味着“退回到战壕常识”的web开发方法从未真正停止应用。
This may finally change now, but citing the conventional wisdom: web authors/developers and thus, in turn, browsers should ultimately decide what should (and shouldn't) be in the specifications, when there's no good reason against what's already been done in reality. And most browsers have long supported STYLE in BODY (in one way or another; see e.g. the scoped attr.), despite its inherent performance (and possibly other) penalties (which we should decide to pay or not, not the specs.). So, for one, I'll keep betting on them, and not going to give up hope. ;) If WHATWG has the same respect for reality/authors as they claim, they may just end up doing here what the W3C did.