Is it possible to change the parent element’s css when there is a different child?

Asked

Viewed 453 times

0

With CSS, is it possible to modify the parent CSS if the child has a different class without changing the parent class name? I just need to actually change the default bottom of the page, when the content changes..., instead of setting new class in the bottom div, I would like to do this only if the content changes...

Example, below would be the default:

<div class="el-pai">
    
    <div class="el-filho"></div>

</div>

Then the son would change the name of the class, and the el-pai would look a different color:

<div class="el-pai">
    
    <div class="el-filho-diferente"></div>

</div>

CSS standard:

.el-pai {
   color: red;
}

.el-pai > .el-filho {
   color: blue;
}

Changing the child’s color would change to black, for example:

.el-pai {
   color: black;
}
  • Man. It depends a lot on your real situation there. If you want to change the background only, it may be that way yes, using for example a pseudo element with position Absolute in the child. But changing the father’s style depending on whether you have a child with an X class inside is not yet possible with CSS alone

  • "It’s not possible yet" and probably never will be. They’ve proposed it a few times and it never stuck. That’s because CSS is all heritage-based and cascade-based.

1 answer

1


No, it is not possible. If you want to be able to change father and son, you need to put the class in the parent element.

  • That’s what I figured...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.