繁体   English   中英

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

[英]“body {background-color}” works in HTML but not in CSS

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

在test1.html,背景颜色设置为“蓝色:在HTML文件test2.html是相同,除了到test1.html <style> 。命令注释文件的style.css包含用于背景颜色一个规范和也用于<H1>元素(测试浏览器是否真的在读取样式表)。

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

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

以下是三个示例文件:

测试1.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>

测试2.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>

样式.css:

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

谢谢!

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

<style type="text/css"></style>

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

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

   body {background-color: green;}
   h1 {color: orange; }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM