If I've got this:
<div class="parent">
<a href="#" id="trigger">Trigger</a>
</div>
<div class="sibling">
<div id="change">Hello</div>
</div>
Is there a selector to grab #change when #trigger is hovered upon?
Like upon hover of #trigger, change the background of .sibling's #change element.
I'm looking for a pure CSS solution without javascript.
Adjacent Sibling Selector (+) The adjacent sibling selector is used to select an element that is directly after another specific element. Sibling elements must have the same parent element, and "adjacent" means "immediately following".
It's easy to apply style to a child element, but if you want to apply style to a parent class that already has child elements, you can use the CSS selector child combinator ( > ), which is placed between two CSS selectors. For example, div > p selects all <p> elements where the parent is a <div> element.
Yes, it's possible. We can check with CSS @supports rule like the following.
CSS All Next Siblings Selector - CSS ~ Sign Selector CSS all next siblings selector matches all element they are siblings of specified element. This Selector identify by ~ (tilde sign) between two selector element. All next siblings selected match all elements whose are sibling of specified element.
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
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With