简体   繁体   English

如何在 css 中排除特定的 class?

[英]How to exclude a particular class in css?

Here's my css:这是我的 css:

.single-post .container {
    width: 60%;
}

not(.single-post) .container{
            min-width:92%;
    }

What I am trying to achieve is: I want the container width to be 60% on single post pages and 92% on every other page.我想要实现的是:我希望容器宽度在单个帖子页面上为 60%,在其他每个页面上为 92%。 So, I am trying to exclude .single-post class from 2nd line of code by using not(.single-post) but it's not working.所以,我试图通过使用not(.single-post)从第二行代码中排除.single-post class 但它不起作用。

However, First line of code is working fine.但是,第一行代码工作正常。

You can declare the general case first and the more specific later.您可以先声明一般情况,然后再声明更具体的情况。 The order of declarations matters (cascading).声明的顺序很重要(级联)。 Run the following sample to check it out.运行以下示例进行检查。

 .container{ width:92%; background-color: blue; }.single-post.container { width: 60%; background-color: red; }
 <;-- normal page --> <div> <div class="container"> &nbsp; </div> </div> <p>&nbsp;</p> <!-- single post post page --> <div class="single-post"> <div class="container"> &nbsp; </div> </div>

:not(.single-post) .container{
        min-width:92%;
}

: should be added. :应该加上。

Secondly, you may need to add !important after 92%其次,您可能需要在92%之后添加!important

so final code will be所以最终代码将是

:not(.single-post) .container{
        min-width:92% !important;
}

As far as I know you can't use:not() on nested classes: nesting inside css:not() selectors据我所知,您不能在嵌套类上使用:not(): 嵌套在 css:not() 选择器中

You could try using !important on single posts?您可以尝试在单个帖子上使用!important吗?

.container {
  background-color: lightblue;
  margin: 1em 0;
  padding: 1em;
  width: 92%;
}

.single-post .container {
  width: 60% !important;
}

Working fiddle: https://jsfiddle.net/rmgawnu8/1/工作小提琴: https://jsfiddle.net/rmgawnu8/1/

Note that you'll have to use the same CSS reference.请注意,您必须使用相同的 CSS 参考。

Assuming that the .single-post class is on the body element or another parent element, you could just write the following:假设.single-post class 位于body元素或另一个父元素上,您可以编写以下内容:

.container {
  width: 92%;
}
.single-post .container {
  width: 60%;
}

This will set the width of the .container element to 92% on all pages—except when it's on the .single-post page, where it will be set to 60% because that ruleset is defined later in the cascade and is more specific than the previous ruleset.这会将所有页面上的.container元素的宽度设置为92% 60%除非它在.single-post页面上,因为该规则集稍后在级联中定义,并且比 .single-post 页面更具体之前的规则集。

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

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