childrens selectors from sibling parent divs using css for hover effect

后端 未结 1 1132
情深已故
情深已故 2021-01-27 16:58

I have two sibling divs(parent divs) and those have child divs. If I mouse hover on the child2 div inside parent1, I wanted to change the style of child4 div inside the parent

1条回答
  •  孤城傲影
    2021-01-27 17:33

    You can't go back in the DOM with pure CSS, that is why it is only possible to select childs, siblings and childs of siblings when hovering an element. The following demonstrates what currently can be selected by hovering the first parent:

    #parent1:hover > div {
      color: blue;
    }
    #parent1:hover > div > div {
      color: purple;
    }
    #parent1:hover ~ div {
      color: red;
    }
    #parent1:hover ~ div > div {
      color: orange;
    }
    #parent1:hover ~ div > div > div {
      color: green;
    }
    
    #parent1 {
      border: 1px solid blue;
    }
    div {
      margin: 5px 0;
    }
    div > div {
      margin-left: 15px;
    }
    Parent1 (target)
    Child1
    Child2
    Parent2
    Child1
    Child2

    0 讨论(0)
提交回复
热议问题