【发布时间】: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