css - 不能从 css * 选择器中排除类

标签 css css-selectors

我想对一个类的所有子级应用样式,除了 .fixedsize 子级:

    .inner *:not(.fixedsize){
        max-width:100%
    }
    
    .fixedsize > *{
    max-width:none
    }
    <div class="inner">
        <div>
        <div id="map" class="fixedsize">
           inspect the map here
           <div id="childOfMap">inspect the child of map here</div>
        </div>
        </div>
    </div>

好像不行。如何从 * 选择器中排除它及其所有子项?

编辑: 当我检查 stackoverflow 代码段中的主要元素( map )时,它没有 max-width:100% ,这没问题。但是在运行时,当我检查 map 时可能是更复杂的代码,它有 max-width:100% 从这个 * 选择器计算。

最佳答案

问题是 :not() 选择器更具体,因此您需要增加其他选择器的具体性或使用 !important

.inner *:not(.fixedsize) {
  border:1px solid red;
}
/*Adding not() will make the specifity of this one higher*/
.fixedsize *:not(#randomId) {
  border:none;
}
<div class="inner">
  <div>
    <div id="map" class="fixedsize">
      no border
      <div id="childOfMap">no border <span>here also</span></div>
    </div>
    <div>Content</div>
  </div>
  <div>some other content<span>here</span></div>
</div>

!important:

.inner *:not(.fixedsize) {
  border:1px solid red;
}
.fixedsize * {
  border:none!important;
}
<div class="inner">
  <div>
    <div id="map" class="fixedsize">
      no border
      <div id="childOfMap">no border <span>here also</span></div>
    </div>
    <div>Content</div>
  </div>
  <div>some other content<span>here</span></div>
</div>

关于css - 不能从 css * 选择器中排除类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50085802/

相关文章:

css - 如何结合 nth-child hover 和 before

java - 使用属性匹配为 webDriver 编写 cssselector 表达式

jquery 选择第 n 个子级跳过隐藏元素

javascript - jquery 自动完成插件是否存在浏览器兼容性问题?

css - Scrapy 在 Xpath 或 Css 中找不到文本

css - 1px 边框未在 Chrome 移动版中显示

html - 如何在第三行和第四行中选择第二个div

css - 自动组合/合并 CSS 选择器与预处理器

html - 如何防止图像加载时内容跳转?

html - 无法设置数据图标对齐和填充