在Header之外添加CSS规则可能吗? [英] Is adding CSS rules outside the Header possible?
问题描述
可能重复:
在HEAD之外声明CSS样式元素的HTML页
Possible Duplicate:
Declare CSS style outside the “HEAD” element of an “HTML” page ?
我正在创建一些在CMS内使用的内容我没有访问标头标记。是否有办法在文档的< BODY>
中添加CSS规则?
I am creating some content that is being used inside a CMS where I do not have access to the header tag. Is there a way to add CSS rules within the <BODY>
of the document?
this ...
.ClassName
{
border: 2px solid red;
margin: 5px;
padding: 5px;
}
我可以在元素内添加样式规则inline避免这种情况,因为CSS规则将在许多元素中使用。
I could add the style rules "inline" inside the element but I wanted to avoid this if possible since the CSS rules will be used in many elements.
我想避免这个...
<div style="border: 2px solid red; margin: 5px; padding: 5px">content</div>
推荐答案
您可以添加 style> in
body
,但您会获得验证错误:
You can add <style>
inside body
, but you'll get a validation error:
元素样式在此上下文中不允许作为元素体的子元素。 (抑制此子树的进一步错误。)
Element style not allowed as child of element body in this context. (Suppressing further errors from this subtree.)
(这是因为根据规范不允许, href =http://stackoverflow.com/questions/5251734/is-adding-css-rules-outside-the-header-possible/5251829#5251829> @ Oded的回答)
它在浏览器中工作正常。 浏览器不关心:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<style type="text/css">
.ClassName
{
border: 2px solid red;
margin: 5px;
padding: 5px;
}
</style>
<div class="ClassName">content</div>
</body>
</html>
这篇关于在Header之外添加CSS规则可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!