CSS Selector for Child of Parent's Sibling Element

前端 未结 2 606
深忆病人
深忆病人 2021-01-04 00:22

If I\'ve got this:


相关标签:
2条回答
  • 2021-01-04 00:37

    No. You cannot achieve this using CSS only. Javascript is a good option in this case..

    You can however detect the .parent being hovered (which will solve your problem if the parent surrounds exactly the trigger):

    .parent:hover + .sibling div#change{background:red;}
    

    (markup stays the same jsFiddle)

    0 讨论(0)
  • 2021-01-04 00:42

    In a word: no.

    Given the current structure of your HTML (and the current state of CSS selectors), this is not possible. Perhaps we will get something like this in CSS4, but traversal like this is best left up to Javascript.

    You can obviously restructure your markup, and use the sibling selector:

    HTML

    <div class="parent">
        <a href="#" id="trigger">Trigger</a>
        <div class="sibling">
           <div id="change">Hello</div>
        </div>
    </div>
    

    CSS

    #trigger:hover + .sibling #change {
      color:red; 
    }
    

    codepen

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