如何使用not选择器定位ID中的元素?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何使用not选择器定位ID中的元素?相关的知识,希望对你有一定的参考价值。

是否可以使用not css选择器来定位除INSIDE类(或ID的子级)之外的所有元素之外的所有元素?

示例:以widget元素内的所有svg文件为目标:

svg:not(#widget) 
  fill: red;;


<div>
 <svg /> // red
 <div id="widget">
  <svg /> // not red
  <svg /> // not red
 </div>
 <svg /> // red
</div>
答案

svg 
  fill: red;


#widget svg
  fill : inherit;
<div>
 <svg style='width:200px;height:200px;' /> // red
 <div id="widget">
  <svg style='width:200px;height:200px;'  /> // not red
  <svg style='width:200px;height:200px;'  /> // not red
 </div>
 <svg style='width:200px;height:200px;'  /> // red
</div>
另一答案

div
    color: black;

div:not(#widget)
    color: red;
<div>
 <svg /> // red
 <div id="widget">
  <svg /> // not red
  <svg /> // not red
 </div>
 <svg /> // red
</div>

以上是关于如何使用not选择器定位ID中的元素?的主要内容,如果未能解决你的问题,请参考以下文章