0

I know that this can be done when the objects are siblings of eachother or one is a child. But how could one achieve this effect when that is not the case?

Since this couldn't be done via the parent or sibling method (at least as far as I am aware), I turned to the internet but couldn't find any sensible solutions.

<div class="x">
  <div class="y">
    <div class="z">
      <p class="hover-me"></p>
    </div>
  </div>
</div>
<div class="the-hover-should-effect-me"></div>

I expect the code to at the hover of .hover-me make me change .the-hover-should-effect-me, which as they aren't siblings or children I have yet to be to do.

AMG
  • 1
  • 2

2 Answers2

1

This can't be done with plain CSS, so you'll need to use JavaScript:

let hoverMe = document.querySelector('.hover-me');
hoverMe.addEventListener('mouseenter', () => {
    // apply changes to .the-hover-should-effect-me
});
hoverMe.addEventListener('mouseout', () =>  {
    // revert changes to .the-hover-should-effect-me
});

If you have multiple pairings of .hover-me and .the-hover-should-effect-me then you can perform queries based on a common container that each pairing has.

FThompson
  • 28,352
  • 13
  • 60
  • 93
0

There isn't a solution as of yet, that allows a child element of parent A, to affect parent B, via a pure CSS solution.

This can be achieved however with javsacript.

const hover = document.querySelector(".hover-me");
const hover_target = document.querySelector(".the-hover-should-effect-me");

hover.addEventListener('mouseover', () => {
    hover_target.textContent ="hi";
})
alex067
  • 3,159
  • 1
  • 11
  • 17