I have a parent class .parent-class
and I wanna apply some style on its only in the case if this class have a child class named as child-class
. I did this way and it didn't work:
.parent-class:has(.child-class){
background: red;
}
I have a parent class .parent-class
and I wanna apply some style on its only in the case if this class have a child class named as child-class
. I did this way and it didn't work:
.parent-class:has(.child-class){
background: red;
}
As far as I know, there is no way to do this using CSS.
I suggest using jQuery :has()
like below.
$(".box1:has(.box2)").addClass("box3");
.box1 {
width: 100px;
height: 100px;
background-color: red;
}
.box3 {
background-color: yellow;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="box1">
<div class="box2"></div>
</div>
<div class="box1"></div>