将样式应用于除特定div之外的所有div

时间:2022-02-11 20:24:39

I'm trying to apply styles to all my divs, except one specific. I'm doing this but it doesn't work:

我正在尝试将样式应用于我的所有div,除了一个特定的。我这样做但它不起作用:

#toolbar div[class~="olControlNavigationHistory"]{
   float: left;
   background-repeat: no-repeat;
   margin: 2px 12px 2px 12px;
}

So I need to apply this style to all the divs in #toolbar EXCEPT the div with a class called "olControlNavigationHistory".

所以我需要将这个样式应用于#toolbar中的所有div除了div之外的一个名为“olControlNavigationHistory”的类。

How can I do this? Is this possible?

我怎样才能做到这一点?这可能吗?

Thanks in advance!

提前致谢!

2 个解决方案

#1


11  

Just apply the rule to all divs first:

首先将规则应用于所有div:

#toolbar div {
   float: left;
   background-repeat: no-repeat;
   margin: 2px 12px 2px 12px;
}

Then you need to zero the values out for the specific case:

然后,您需要为特定情况将值清零:

#toolbar div.olControlNavigationHistor {
   float: none;
   background-repeat: repeat;
   margin: 0;
}

Of course this assumes that the property values that specific div would have had without the first rule applied are each properties defaults (such as margin: 0 and float: none.)

当然,这假定特定div在没有应用第一个规则的情况下具有的属性值是每个属性默认值(例如margin:0和float:none。)

EDIT: However in the future when CSS3 is supported everywere, you could also just rewrite your original rule as #toolbar div:not(.olControlNavigationHistory) and it would work correctly and elegantly.

编辑:但是在将来每次支持CSS3时,您也可以将原始规则重写为#toolbar div:not(.olControlNavigationHistory),它将正常且优雅地工作。

#2


2  

I was just doing the same and found this answer Use the :not selector:

我只是做同样的事情并找到了这个答案使用:not selector:

div:not(#div1){
    color:red;
}

it was posted in Apply CSS Style on all elements except with a SPECIFIC ID

除了具有SPECIFIC ID之外,它在所有元素上的Apply CSS Style中发布

#1


11  

Just apply the rule to all divs first:

首先将规则应用于所有div:

#toolbar div {
   float: left;
   background-repeat: no-repeat;
   margin: 2px 12px 2px 12px;
}

Then you need to zero the values out for the specific case:

然后,您需要为特定情况将值清零:

#toolbar div.olControlNavigationHistor {
   float: none;
   background-repeat: repeat;
   margin: 0;
}

Of course this assumes that the property values that specific div would have had without the first rule applied are each properties defaults (such as margin: 0 and float: none.)

当然,这假定特定div在没有应用第一个规则的情况下具有的属性值是每个属性默认值(例如margin:0和float:none。)

EDIT: However in the future when CSS3 is supported everywere, you could also just rewrite your original rule as #toolbar div:not(.olControlNavigationHistory) and it would work correctly and elegantly.

编辑:但是在将来每次支持CSS3时,您也可以将原始规则重写为#toolbar div:not(.olControlNavigationHistory),它将正常且优雅地工作。

#2


2  

I was just doing the same and found this answer Use the :not selector:

我只是做同样的事情并找到了这个答案使用:not selector:

div:not(#div1){
    color:red;
}

it was posted in Apply CSS Style on all elements except with a SPECIFIC ID

除了具有SPECIFIC ID之外,它在所有元素上的Apply CSS Style中发布