【问题标题】:CSS NOT selector to exclude a class and its childrenCSS NOT 选择器排除一个类及其子类
【发布时间】:2020-05-03 17:39:59
【问题描述】:

我使用此代码来确保响应式 div 不会溢出:

.responsive * {width:100%}

但是我有一些子元素想要从上述规则中排除它们及其子元素。所以我将规则更新为:

.responsive  * :not(.fixedSize,.fixedSize *){max-width:100%;}

上面的代码似乎排除了一切。

div {width:200px;border:1px solid #ff0000;line-height:40px;}
input {width:300px;}
.responsive  * :not(.fixedSize,.fixedSize *){max-width:100%;}
<div class="responsive">
   <input>
   <div class="fixedSize">
      <input>
   <div>
</div>

编辑:

我也试过了,但还不行:

 .responsive  * :not(.fixedSize):not(.fixedSize *){max-width:100%;}

【问题讨论】:

  • 您的选择器正在查看第三个子级别,:not() 只接受一个简单的选择器。这里.responsive *{max-width:100%;box-sizing:border-box} 不包括一个类:.responsive :not(.fixedSize) {max-width:100%;box-sizing:border-box} 可以完成这项工作。如果之前没有应用,请注意盒子大小规则。
  • 我不知道 :not()* 之间的空格是拼写错误,或者您在真实代码中错过了它(删除它),并且正如 ppl 提到的 :not() 只接受单个选择器但你可以使用类似.elmenet:not(.this):not(that)
  • 您的建议不排除 fixedSize 元素及其子元素。@G-Cyr
  • @AliSheikhpour .responsive *:not(.fixedSize){max-width:100%;}.responsive *:not(.fixedSize) *{max-width:100%;}
  • 请将您的答案发布为 sn-p。我检查了它,但还没有工作。 @ColinCline

标签: css responsive-design css-selectors


【解决方案1】:

您是否试图实现这一目标?正如其他人在 cmets 中提到的那样,:not 是一个单一的选择器,因此您只能在其中创建一个单独的 id 或类。

.responsive {
  width: 200px;
  border: 1px solid #ff0000;
  line-height: 40px;
}

input {
  width: 300px;
}

.responsive *:not(.fixedSize) {
  max-width:100%;
}
<div class="responsive">
  <input>
  <input>
  <input class ="fixedSize">
</div>

【讨论】:

  • 为什么限制在第一深度 lvl 孩子?
  • &gt; 表示只有第一个 lvl 深度的子元素和&gt;* 在第一个 lvl 深度中的每个元素(无论是 div 左右)
  • @ColinCline 我无意中添加了这一点,因为该示例只有 1 个孩子,但对其进行了更新。
  • 新版本还没有排除更深层次的固定尺寸元素。
  • :not()目前只接受一个选择器,但 4 级规范确实允许多个选择器。
猜你喜欢
  • 2017-06-30
  • 1970-01-01
  • 1970-01-01
  • 2021-07-31
  • 2013-01-26
  • 1970-01-01
  • 2020-04-27
  • 2022-01-23
  • 2013-02-07
相关资源
最近更新 更多