If you can add a wrapper div to your HTML, you can use the hover on the wrapper to target the .action
element, then disable pointer-events
on the wrapper and reset it on the .hover
element, so it doesn't work when hovering the other part.
a {
display: block;
padding: 8px 10px;
border: 1px solid red;
max-width: 20%;
}
.wrapper:hover > .action{
background: yellow;
}
.wrapper{pointer-events:none;}
.hover{pointer-events:auto;}
<div class="wrapper">
<a href="#" class="action">Need This to be action</a>
<a href="#" class="hover">When Hover This</a>
</div><br/><br/>
<div class="wrapper">
<a href="#" class="hover">regular Hover</a>
<a href="#" class="action">Action</a>
</div><br/><br/>
<div class="wrapper">
<a href="#" class="hover">Hover sandwich</a>
<a href="#" class="action">Action</a>
<a href="#" class="hover">Hover sandwich</a>
</div><br/><br/>
<div class="wrapper">
<a href="#" class="action">multiple Action!</a>
<a href="#" class="hover">Hover</a>
<a href="#" class="action">multiple Action!</a>
</div>
Best part of this approach is that you can set simple, scalable rules that will apply correctly whatever the .hover
and .action
elements are positioned, even if using multiples :)
A noticeable limitation though, is that you'll loose pointer-events
on the target element, so if you need to hover / click or whatever on them, this might not be the best approach.
So it all depends on your use case.
Another approach would be to set the :hover on the container, then reset the initial values on the target's :hover.
a {
display: block;
padding: 8px 10px;
border: 1px solid red;
}
.wrapper{
display:inline-block;
width: 20%;
}
.wrapper:hover > .action{
background: yellow;
}
.action:hover{
background:initial !important;
}
<div class="wrapper">
<a href="#" class="action">Need This to be action</a>
<a href="#" class="hover">When Hover This</a>
</div><br/><br/>
<div class="wrapper">
<a href="#" class="hover">regular Hover</a>
<a href="#" class="action">Action</a>
</div><br/><br/>
<div class="wrapper">
<a href="#" class="hover">Hover sandwich</a>
<a href="#" class="action">Action</a>
<a href="#" class="hover">Hover sandwich</a>
</div><br/><br/>
<div class="wrapper">
<a href="#" class="action">multiple Action!</a>
<a href="#" class="hover">Hover</a>
<a href="#" class="action">multiple Action!</a>
</div>
Notice with this alternative method you don't loose pointer-events
on your .action
, but you can't really have the 1 .hover
, multiple .action
as in the first method