CSS 如何在一个css文件中为不同的页面设置不同的正文背景颜色?

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

How to set different body background color for different pages in one css files?

cssbackground

提问by yuli chika

I have a few pages. page1 <body id="page1">, page2 <body id="page2">, page3 <body id="page3">then I want write all the css rules in one css file 'style.css', set page1 background color:#000; page2 background color:#fff; page3 background color:#00f;

我有几页。page1 <body id="page1">, page2 <body id="page2">, page3<body id="page3">然后我想在一个 css 文件 'style.css' 中写入所有 css 规则,设置 page1 背景颜色:#000; page2 背景色:#fff; page3 背景色:#00f;

body #page1{background-color:#000;}
body #page2{background-color:#fff;}
body #page3{background-color:#00f;}

but it is wrong, how to write correctly? thanks.

但是写错了,怎么写才正确?谢谢。

回答by Pekka

Just remove the space (You want to specify all elements with the tag name bodyandthe ID page1):

只需删除空格(您要使用标签名称bodyID指定所有元素page1):

body#page1{background-color:#000;}

回答by yuli chika

#page1{background-color:#000;}
#page2{background-color:#fff;}
#page3{background-color:#00f;}

you can also write

你也可以写

body#page1{background-color:#000;}

butbody selector is redundant (and slower) in this case.

在这种情况下,butbody 选择器是多余的(而且速度较慢)。

回答by Soubhagya Kumar Barik

You can do this through jQuery also.See the below example.

您也可以通过 jQuery 执行此操作。请参见下面的示例。

.bg-white{
background-color: #fff;
}


$(document).ready(function () {
    $('body').addClass('bg-white');
});