CSS:如果div的父级有特定的类,则修改div的样式

  • 本文关键字:div 样式 修改 如果 CSS css
  • 更新时间 :
  • 英文 :


我一直被这个css问题所困扰。这里有一个简化的代码:

<div class="parent type1">
<div class="child">
red
</div>
</div>
<div class="parent type2">
<div class="child">
blue
</div>
<div>

我不知道如何编码(或者我是否可以编码(,但我需要这样的东西:"如果类是"类";"孩子";并且父母的类是";type2";,将div放在蓝色(不更改红色div(中";。

知道在我的特定情况下,我不能更改html,所以我不能添加一些id。

提前感谢您,祝您度过美好的一天!:-(

链式CSS可以做到这一点:

.type2 .child {
color: blue;
}
/* OR combined classes + child */
.parent.type2 .child {
color: blue;
}
/* OR direct child of type2 */
.type2 > .child {
color: blue;
}

.type2 div{
color : blue;
}
.type2 .child{
color : blue;
}
<div class="parent type1">
<div class="child">
red
</div>
</div>
<div class="parent type2">
<div class="child">
blue
</div>
<div>

最新更新