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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17280880/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 10:25:51  来源:igfitidea点击:

"body {background-color}" works in HTML but not in CSS

htmlcssbackground-color

提问by Curious George

Am able to set the background-color attribute for HTML body in an inline <style>command but not when the identical command is moved to an external stylesheet. A specific example is given below.

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

In test1.html, background-color is set to "blue: in the HTML. File test2.html is identical to test1.html except the <style>command is commented out. File style.css contains a spec for background-color and also for the <H1>element (to test that the browser really is reading the stylesheet).

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

First test produces orange text against a blue background. Second test produces orange text, but against a white background. I've tried this on Firefox 21, Chrome 19, and IE 9; all give the same results.

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

What's going on? Any help would be appreciated.

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

Here are the three sample files:

以下是三个示例文件:

test1.html:

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

test2.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>

style.css:

样式.css:

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

Thank you!

谢谢!

回答by Arun Kumar

don't use <style type="text/css"></style>in style.css

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

回答by aaron lilly

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

is html tags, and you shouldnt have them in your .css file,

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

replace the code within style.css with this. Just copy and paste.

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

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