简体   繁体   English

如何使用CSS为所有类覆盖背景颜色?

[英]How to override background-color for all classes using css?

I'm using a CSS theme that I'm not allowed to edit, I need a way to paint all the white backgroud-color with something less shiny. 我正在使用不允许编辑的CSS主题,我需要一种将所有白色backgroud-color用不太亮backgroud-color绘制的方法。

I'm new to CSS , what should I do to override the background-color for all classes using CSS ? 我是CSS新手,应该怎么做才能使用CSS覆盖所有类的背景颜色?

Here is a screen shot, I'm using Primefaces to generate the web content hence I'm unable to change the provided CSS 这是一个屏幕截图,我正在使用Primefaces生成Web内容,因此无法更改提供的CSS

A screen shot of what I'm trying to change 我尝试更改的屏幕截图

I already understand that every element has it's own class in the theme, but I don't know their names, nor which one of these classes provide the background-color for those elements, what I'm looking for is a simple way to repaint the white color in the whole page. 我已经知道每个元素在主题中都有自己的类,但是我不知道它们的名称,也不知道这些类中的哪一个为这些元素提供了background-color ,我正在寻找一种简单的重新绘制方法整个页面中的白色。

From the official documentation : 从官方文档中

Specificity is the means by which browsers decide which CSS property values are the most relevant to an element and, therefore, will be applied. 特异性是浏览器确定哪些CSS属性值与某个元素最相关的方法,因此将被应用。

And also: 并且:

Specificity is based on the matching rules which are composed of CSS selectors of different sorts. 特异性基于匹配规则,该匹配规则由不同种类的CSS选择器组成。

That means that the CSS would be applied depending of your browser and your CSS Selectors for that browser. 这意味着, CSS会根据您的浏览器和您的应用CSS Selectors该浏览器。

And you should also look at what CSS Selectors has a higher specificity. 您还应该查看哪些CSS选择器具有更高的特异性。

But as you want to override the background-color , that means to get the higher CSS Selector specificity I think what you are looking for is the !important exception . 但是,当您要覆盖background-color ,这意味着要获得更高的CSS Selector特异性,我认为您正在寻找的是!important异常

Again from the documentation: 再次从文档中:

this declaration overrides any other declarations. 该声明将覆盖所有其他声明。

that means that the property of CSS that you are going to set with !important exception will be applied overriding the rest of different configurations that has that property. 这意味着将使用!important异常设置的CSS属性将被应用,以覆盖具有该属性的其余不同配置。

But also, you have to care about to abuse of that property: 而且,您还必须关心滥用该财产:

Using !important is bad practice and should be avoided because it makes debugging more difficult by breaking the natural cascading in your stylesheets. 使用!important是不好的做法,应避免使用它,因为它会破坏样式表中的自然级联,从而使调试更加困难。

What I recommend: 我的建议:

  • Try to set your background-color with CSS Selectors with higher specificity. 尝试使用具有更高特异性的CSS Selectors设置background-color

  • If you cannot modify the property, and you have tried all the posibilities, then use !important exception. 如果您不能修改该属性,并且已经尝试了所有可能性,请使用!important异常。

You can paste this into the bottom of your page right before the tag. 您可以将其粘贴到标签底部的页面底部。

<script>
    document.body.style.backgroundColor = "#883377";
</script>

You can the change the #883377 to whatever color you would like. 您可以将#883377更改为#883377的任何颜色。

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

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