将风格CSS样式标记呈现在身体? [英] Will style css tag render in body?

查看:163
本文介绍了将风格CSS样式标记呈现在身体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用CMS,显然它有一个错误,它不允许我将任何内容添加到博客文章的< head> 中。它将所有东西插入到大多数情况下可以正常工作的主体中,但是在这样的代码实例中:

 <样式类型= 文本/ CSS > 
.slideshow img {display:none}
.slideshow img.first {display:block}
< / style>

如果将这类代码放入< body> 标签在所有主流浏览器中? (IE8 +,Firefox,Chrome和Safari)。通常情况下,它始终位于页面的< head> 中。



注意:这似乎在FF 15中有效,但我不确定是否有其他浏览器。

身体是无效的代码。



每个浏览器都会尝试以有意义的方式使用无效代码。有些浏览器似乎总是使用样式规则,但是您不能依赖所有当前和未来浏览器的行为。另一种行之有效的做法是忽略无效的样式标签。



请注意,即使在同一浏览器中,行为也会因网页是否呈现而有所不同在标准兼容模式或怪癖模式下。在标准兼容模式(这是首选)中,浏览器往往更加严格,忽略无效的代码,而不是试图猜测作者想要的代码。




2014年更新:



在HTML5中,您可以在body元素中使用样式标签。在任何不知道HTML5的旧浏览器中,或者如果您没有HTML5文档类型,它仍然是无效的代码。


I am using a CMS and apparently it has a bug that will not allow me to add any content to the <head> of a blog post. It inserts everything into the body which in most cases that works okay but in an instance of code like this:

<style type="text/css">
 .slideshow img { display: none }
 .slideshow img.first { display: block }
</style>

Will that type of code run if put inside the <body> tag in all major browsers? (IE8+, Firefox, Chrome, and Safari.) Normally that is always in the <head> of the page.

Note: This appears to work in FF 15 but I am not sure of other browsers.

解决方案

Having style tags in the body is invalid code.

Every browser will try to use the invalid code in a way that makes sense. Some browsers seem to use the style rules anyway, but you can't rely on that being the behaviour of all current and future browsers. Another behaviour that would also make perfect sense would be to ignore the invalid style tag.

Note that even within the same browser the behaviour can differ depending on whether the page is rendered in standards compliance mode or quirks mode. In standards compliance mode (which is the preferred one) browsers tend to be more strict and ignore invalid code rather than try to guess what the author intended with the code.


Update 2014:

In HTML5 you can use style tags in the body element. In any older browsers that are not aware of HTML5, or if you don't have a HTML5 doctype, it's still invalid code.

这篇关于将风格CSS样式标记呈现在身体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆