首页 > 解决方案 > CSS选择器以不在另一个元素中的img为目标

问题描述

我想设计一个上面没有“旁边”的图像。我希望样式适用于所有 img 元素,如下所示:

<div class='content'>
  (assorted other divs and containers in here)
  <img src='http://example.com'>
  (could be more <img> elements in here too)
</div>

但我不希望它在这里应用:

<div class='content'>
  (assorted other divs and containers in here)
  <aside class='quote'>
    (assorted other divs and containers in here)
    <img src='http://example.com'>
  </aside>
</div>

这是我正在尝试的 CSS,但没有运气......

.content :not(aside.quote) img { width:100%; }

由于结构中可以有各种其他 div 和容器,我不能假设 img 将是第一个孩子(或不是第一个孩子)。

我怎样才能得到正确的选择器来做到这一点?

标签: htmlcss

解决方案


推荐阅读