0

I'd like to manipulate an elements' width by hovering and at the same time edit its neighbors elements width.

I thought I could do it like so:

.sw_2-technik:hover{
    width:60%;
}

.sw_2-technik:hover .sw_2.emotionen{
    width:40%;
}

Whereas the hovering of .sw_technik makes its width to 60% and .sw_2-emotionen to 40%. But that doesn't seem to work.

Do I really need to use JS for something like that?


Additional info:

the 2 containers are next to each others, like so

<div class="sw_2-technik"></div><div class="sw_2-emotionen"></div>

And I therefore also tried this code:

.sw_2-technik:hover + .emotionen{
    width:40%;
}

but that doesn't work neither.

5
  • 2
    Possible duplicate of How to affect other elements when a div is hovered Commented Feb 16, 2017 at 17:22
  • Could be indeed. Though after this explaination: .sw_2-technik:hover + .emotionen{ width:40%; } would be the correct answer. But that doesn't work for me neither Commented Feb 16, 2017 at 17:30
  • 1
    Might just by mistake here, but with your classes shouldn't it be .sw_2-technik:hover + .sw_2-emotionen{ width:40%; } Commented Feb 16, 2017 at 17:32
  • My fault with wrong spelling. Thanks for guiding me to the right reference. Commented Feb 16, 2017 at 17:32
  • @Daiaiai Glad we can help :) Commented Feb 16, 2017 at 17:35

3 Answers 3

1

You can use ~ or +. Difference being is that + requires the element to be directly after the one being hovered, and ~ looks for the closest.

.sw_2-technik:hover ~ .sw_2.emotionen {
    color: blue;
}

Here is the JSFiddle: https://jsfiddle.net/ka1yxv8z/

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

Comments

1

The correct answer is as already answered in a similar question that one:

.sw_2-technik:hover{
    width:60%;
}

.sw_2-technik:hover + .sw_2-emotionen{
    width:40%;
}

Comments

0

In reply to your comment, this seems to work for me as indicated below.

.sw_2-technik, .sw_2-emotionen {
  background: blue;
  padding: 2px;
}

.sw_2-technik + .sw_2-emotionen {
  padding: 5px;
  background: red;
}
<div class="sw_2-technik"></div>
<div class="sw_2-emotionen"></div>

Comments

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.