Html 无法使用 CSS 重置更改正文背景颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6671846/
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 09:32:34  来源:igfitidea点击:

can't change body background color using CSS reset

htmlcssyuicss-reset

提问by FlintOff

Here is my HTML code:

这是我的 HTML 代码:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <title>Hover Zoom</title>
        <link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/3.3.0/build/cssreset/reset-min.css">
        <link rel="stylesheet" type="text/css" href="style.css">
        <meta http-equiv="content-type" content="text/html;charset=UTF-8" >
    </head>
    <body>

    </body>
</html>

Here is my CSS file code (style.css):

这是我的 CSS 文件代码 ( style.css):

body {
    background-color: #FF0000;
}

but the background color of bodydoes not change.

但 的背景颜色body不会改变。

Without the CSS reset, it works fine. Can you suggest me a better CSS reset, or any other solution?

没有 CSS 重置,它工作正常。你能建议我一个更好的 CSS 重置,或任何其他解决方案吗?

回答by brenjt

change your selector to

将您的选择器更改为

html, body{
    background-color: #FF0000;
}

回答by Devin Burke

brenjt's answer is correct, but I'll provide an explanation for what is wrong and why that solution works:

brenjt 的回答是正确的,但我会解释什么是错误的以及为什么该解决方案有效:

Your CSS reset file sets the background color of htmlwhich is the entire page. You are only setting the body's background color, but your body is extremely small in height since you have no content. Consequentially, you do not see the body's background color.

您的 CSS 重置文件设置html了整个页面的背景颜色。您只是设置了body的背景颜色,但是您的身体高度非常小,因为您没有内容。因此,您看不到正文的背景颜色。

Just set both the htmland bodyin CSS like this:

只需像这样在 CSS 中设置htmlbody

html, body { background-color: #FF0000; }

EDIT:

编辑:

Had you not set the htmlbackground color, then body's background color would represent the whole page. But since you are using an external source's CSS reset, you do not have the option of not setting the htmlproperties.

如果您没有设置html背景颜色,那么body的背景颜色将代表整个页面。但由于您使用的是外部源的 CSS 重置,因此您无法选择不设置html属性。