CSS 类应用于正文背景

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

CSS class apply to body background

cssbackground

提问by Deeptechtons

Using css (not a inline one, but external stylesheet) how do i override the styles applied to body background only for a single page. I right now got this and this does not work

使用 css(不是内联样式表,而是外部样式表)如何覆盖仅适用于单个页面的正文背景的样式。我现在得到了这个,但这是行不通的

body{background: url("../../images/mybg.png") repeat-x scroll 0 0 #7CC2FF;height: 100%}
body.index{background-color:#4466A7!Important}

I want the body background to change on index page only, so i used js hack as below

我希望正文背景仅在索引页面上更改,因此我使用了 js hack,如下所示

if(location.href.indexOf("index.aspx") != -1){
    $("body").addClass("index");
}

but seems the background image is still used and has higher precedence. How do i remove the previous style to apply the overrided styles

但似乎背景图像仍在使用并且具有更高的优先级。如何删除以前的样式以应用覆盖的样式

Solution

解决方案

so finally this is how it is solved

所以最后这就是它的解决方法

body{#4466A7 background: url("../../images/mybg.png") repeat-x scroll 0 0}
body.index{background-image:none !important}

回答by SpliFF

body.index{background-color:#4466A7 !important}

Note the space between the color and !important and capitalisation used on important.

注意颜色和 !important 之间的空格以及用于重要的大小写。

回答by dbrin

body.index{background-image: none}

回答by Shailender Arora

you can do this very easily through css like you need all pages with red background and index page with yellow background see the mentioned below example with css & HTML code :-

您可以通过 css 轻松完成此操作,就像您需要所有具有红色背景的页面和具有黄色背景的索引页面一样,请参阅下面提到的带有 css 和 HTML 代码的示例:-

body {
background:red;    
}

#index {
background:yellow;   
}


<body id="index"> (!--for yellow color index page--!) 
adfadfasf    
</body>

<body> (!--for red color all pages--!) 
adfadfasf    
</body>

or see the fiddle:-http://jsfiddle.net/n6y4q/

或查看小提琴:- http://jsfiddle.net/n6y4q/