0

I have an i tag which I'd like to style when hovering a button tag. This is my structure:

<a class="container">
  <form>
    <button class="origin">HOVER</button>
  </form>
  <p>
    <i class="destination">TARGET</i>
    <p>
</a>

Is this possible with CSS only? (I know how to do it with JS/jQuery)

I guess I'm looking to target the cousing element here, lol...

To be more specific, I'm looking to make the TARGET text color green when hovering the button:

<style>
    a.container{ border: 1px solid black; display: block; }
    a.container:hover{border-color: red; }
    a.container:hover p i{color: yellow; }
    a form button:hover{ color: red; }
    a form button:hover p i{ color: green; } /* not working... */
    .origin:hover .destination{ color: green; } /* not working... */
</style>
<a class="container">
  <form>
    <button class="origin">HOVER</button>
  </form>
  <p>
    <i class="destination">TARGET</i>
    <p>
</a>

2

2 Answers 2

3

If you only want a hover effect when you mouseover the button, then css pointer-events can do this:

.container {
    pointer-events: none;
}

.container .origin {
    pointer-events: auto;
}

.container:hover .destination {
    color: #00cc00;
}

https://developer.mozilla.org/en-US/docs/Web/CSS/pointer-events


However, since you want one hover effect when mouse over the container, and then a different one when you mouse over the button, then you're going to need to use javascript.

If you're looking for a vanilla javascript script, I've created a fiddle https://jsfiddle.net/j6hs5fmL/40/

Sign up to request clarification or add additional context in comments.

3 Comments

wait, this is really good! But I don't quite get it... I modified my question to make it more specific. Would appreciate if you can update your fiddle, if this is a solution! :)
Oh, so I think you are disabling the default hover on the container to make it work. I want to have the hover on the container work AND the hover on the origin work, separately. Hovering container causes TARGET to be yellow, hovering origin causes TARGET to be green. Hovering over container and over origin can still cause each of them to change their own style...
Yeah, that kind of behavior is beyond pure css, sorry. I'll try and come back in the morning with a js solution without jquery
2

You have to use jquery/js for this....

Use onmouseover and onmouseout and addClass/removeClass in css style the class

function func(){
  $(".destination").addClass("hovering");
}
function outer(){
 $(".destination").removeClass("hovering");
}
.hovering{
color:red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a class="container">
  <form>
    <button class="origin" onmouseover="func()" onmouseout="outer()">HOVER</button>
  </form>
  <p>
    <i class="destination">TARGET</i>
    <p>
</a>

3 Comments

yes I see nice solution and I upvoted for this I stay this answer to other OP that prefer this solution
I was actually looking for a CSS only solution :-(
Following clarification from the OP, this falls back into requiring a js solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.