简体   繁体   English

CSS范围和多个类

[英]CSS Scope and Multiple Classes

I've ran into an issue where the parent scope is having an argument with its children. 我遇到了一个问题,即父范围与其子级有一个参数。

What I was trying to accomplish is by setting the default color scheme for my page in the top level div (or body tag in my case). 我要完成的工作是在顶层div中为页面设置默认的配色方案(在我的情况下为body标签)。 And have the ability to change the colors deeper down the DOM. 并能够在DOM的更深处更改颜色。

However when I use the following the inner DIV's are purple, and not red (see fiddle for demo). 但是,当我使用以下内容时,内部的DIV是紫色的,而不是红色的(演示请参见小提琴 )。

Now if I swap the classes around in the file it works (the order in which they reside in the file), however this is not a solution in my case as its also possible for a 'red page' with purple panels and the same problem would arise. 现在,如果我在文件中四处交换类,则它可以工作(它们在文件中的驻留顺序),但是,对于我而言,这不是解决方案,因为对于带有紫色面板且存在相同问题的“红色页面”,这也是可行的会出现。

Fiddle: http://jsfiddle.net/16t7g9cm/4/ 小提琴: http : //jsfiddle.net/16t7g9cm/4/

HTML: HTML:

<div class="purple">
    <div class="red panel"> .red.panel </div>
    <div class="red">
       <div class="panel"> .red .panel </div>
    </div>
    <div class="panel"> .panel </div>
</div>

CSS: CSS:

.red .panel,
.panel.red {
    background-color: #cc3300;
    color: #ffffff;
}

.purple .panel,
.panel.purple {
    background-color: #990099;
    color: #ffffff;
}

Can anyone explain/ point me to what causes this behavior and any possible solutions. 任何人都可以向我解释/指出导致此行为的原因以及任何可能的解决方案。

Regards. 问候。

They have the same specificity however purple comes after red in the CSS and therefore is overriding it. 它们具有相同的特异性,但是purple在CSS中排在red之后,因此将其覆盖。

You can simply switch them around to solve the issue. 您可以简单地切换它们以解决问题。

http://jsfiddle.net/16t7g9cm/7/ http://jsfiddle.net/16t7g9cm/7/

.purple .panel,
.panel.purple {
    background-color: #990099;
    color: #ffffff;
}

.red .panel,
.panel.red {
    background-color: #cc3300;
    color: #ffffff;
}

However if you can't reorder the classes you could make the red selectors would specific: 但是,如果您无法对类进行重新排序,则可以使红色选择器具体化:

.purple .red .panel,
.purple .panel.red {
    background-color: #cc3300;
    color: #ffffff;
}

This is a specificity issue the closer you are to the DOM the more specific you are so if you just replace the position of your css statements: 这是一个特殊性问题,您离DOM越近,就越具体,因此,如果您只替换css语句的位置,则:

.purple .panel,
.panel.purple {
    background-color: #990099;
    color: #ffffff;
}

.red .panel,
.panel.red {
    background-color: #cc3300;
    color: #ffffff;
}

it would work :) http://jsfiddle.net/16t7g9cm/6/ 它将起作用:) http://jsfiddle.net/16t7g9cm/6/


Another way if ordering is a bit hard is to make it more specific (its hacky but works): 如果订购有点困难,另一种方法是使其更具体(它虽然很笨拙但可以工作):

.red.red .panel,
.panel.red.red {
    background-color: #cc3300;
    color: #ffffff;
}

.purple .panel,
.panel.purple {
    background-color: #990099;
    color: #ffffff;
}

as you see instead of .red I have .red.red which is the same thing but give you more specificity http://jsfiddle.net/16t7g9cm/8/ 如您所见,我使用的是.red.red而不是.red,这是一回事,但是为您提供了更多的特异性http://jsfiddle.net/16t7g9cm/8/

Add the following code and you code will work fine 添加以下代码,您的代码将正常运行

 .red .panel, .panel.red { background-color: #cc3300 !important; color: #ffffff !important; } 

your parent styles are applied to the child element, hence current specified code will override the existing parent theme when conditions are met. 您的父样式将应用于子元素,因此,当满足条件时,当前指定的代码将覆盖现有的父主题。

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

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