【问题标题】:Change background color for all child elements without hiding images更改所有子元素的背景颜色而不隐藏图像
【发布时间】:2016-10-10 23:42:39
【问题描述】:

我正在创建一个可以编辑任何网页格式的应用程序。我正在尝试更改所选 div 的所有子 div 的背景颜色,但我当前的代码使 div 中的图像消失。

.blah *:not(img) {
    background-color: #fffdd0 !important;
}

我尝试使用 :not() 使格式不适用于图像,但它没有改变任何东西。有没有办法使背景颜色不适用于图像或不隐藏图像?

【问题讨论】:

标签: css


【解决方案1】:

我认为您的 imgs 不是 children 但它们是您的 .blahdiv 的 grandchildren。这就是你的代码不起作用的原因。所以img(在我的例子中是.child)的父级获得了背景,所以背景出现在图像下方,尽管图像没有获得background-color 样式

你可以试试这样的(不知道你的 HTML 结构或者它有多复杂)

div *:not(.child) { background-color: #fffdd0 !important;}
.parent > .child *:not(img){ background-color: red !important;}
<div class="parent">
<p>Child Text</p>
<h1>Child Heading</h1>
     <div class="child">
      <img src="http://placehold.it/350x150">
      <p>grandChild Text</p>
    </div>
</div>

【讨论】:

  • 好吧。正如我解释的那样。即使 img 没有获得 background-color 样式,它也是 parent ,它是 div 的子代,获得背景颜色。这就是为什么背景颜色出现在图像下方的原因。
  • 据我了解这个问题。他说他当前的代码使图像消失然后他尝试使用:not(img)选择器,但仍然将背景应用于img(不是,它应用于图像的父级,它是div的子级). Is there a way to make the background color not apply to images 我回答了他的问题。问题是 img 不是 div.blah 的直接子代,而是 .grandchild
  • 从这个角度来看……猜测这是一个很好的答案,因为问题本身缺乏信息……将删除我的 cmets 并删除不赞成票以避免你的答案的垃圾邮件。干得好队友
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-04
  • 1970-01-01
  • 2014-04-26
  • 2011-11-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多