“body {background-color}”在HTML中有效,但在CSS中无效

问题描述 投票:14回答:2

能够在内联<style>命令中设置HTML主体的background-color属性,但在相同命令移动到外部样式表时则不能。下面给出一个具体的例子。

在test1.html中,background-color设置为“blue:在HTML中。文件test2.html与test1.html相同,除了<style>命令被注释掉。文件style.css包含背景颜色的规范,也用于<H1>元素(用于测试浏览器是否正在读取样式表)。

第一次测试在蓝色背景下产生橙色文本。第二次测试产生橙色文本,但是在白色背景下。我在Firefox 21,Chrome 19和IE 9上试过这个;都给出了相同的结果。

这是怎么回事?任何帮助,将不胜感激。

以下是三个示例文件:

test1.html:

<HTML>
<head> <link type="text/css" rel="stylesheet"  href="style.css">
<style type="text/css">
  body {background-color: blue}
</style> 
</head>
<body> <h1>This is a test.</h1> </body> </html>

test2.html:

<HTML>
<head> <link type="text/css" rel="stylesheet"  href="style.css">
<!-- <style type="text/css">
       body {background-color: blue} 
     </style> -->
</head>
<body> <h1>This is a test.</h1> </body> </html>

style.css中:

<style type="text/css">
   body {background-color: green;}
   h1 {color: orange; }
</style>

谢谢!

html css background-color
2个回答
13
投票

不要在style.css中使用<style type="text/css"></style>


1
投票
<style type="text/css"></style>

是html标签,你不应该在你的.css文件中,

用这个替换style.css中的代码。只需复制并粘贴即可。

   body {background-color: green;}
   h1 {color: orange; }
© www.soinside.com 2019 - 2024. All rights reserved.