1

I have the following HTML:

<div class="wraper">
   <div class="elementA"></div>
   <div class="elementB"></div>
   <div class="elementC"></div>
</div>

I need change background-color of elementA when we hover on elementC. I've tried a lot of CSS examples but no success.

How I can do it by just using CSS? No JavaScript or JQuery.

1

5 Answers 5

2

it can be done with just CSS, although there are some limitations that might prevent you using it. I moved the :hover to the parent and turned off pointer-events on elementB. Unfortunately, this means for IE, IE11+ will be required. Chrome and Firefox have good support already. I also had to reset the background-color on elementA otherwise the color is also modified when hovering on elementA. Lastly, your markup and other CSS on your real page might be different, which could also prevent this being a good solution.

There is no other way I can think of to do this in CSS, so if this doesn't fit your requirements, then JavaScript is the only solution.

Demo

HTML

<div class="wrapper">
   <div class="elementA">A</div>
   <div class="elementB">B</div>
   <div class="elementC">C</div>
</div>

CSS

.wrapper:hover .elementA {
    background-color:red;
}

div {
    width:20px;
    height:20px;
}

.wrapper div {
    border:1px dashed lightskyblue;
}

.elementB {
    pointer-events:none;
}

.wrapper .elementA:hover {
    background-color:white;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Happy to help. It was an interesting problem!
2

You can not select a sibling that is before the selector.

However there's 2 possible solutions, either to do it with Javascript or you can simply re-order your elements in a postion which allows you to select then, re-order it's positions again by setting Position: Absolute;

Comments

1

If you want to do this in plain CSS, it's going to be difficult, but not impossible. You could play with floats (horizontal) or probably more dynamic, absolute elements. Of course you need to have a known width/height for the elements to position them right.

HTML:

<div class="wrapper">
    <div class="elementC"></div>
    <div class="elementA"></div>
    <div class="elementB"></div>
</div>

CSS (without position values).

.wrapper {
    position:relative;
}

.wrapper>div {
    position:absolute;
}

.elementC:hover+.elementA {
    background-color:#333;
}

With position values (top, right, bottom, left) you can change the visible positions of the elements. The plus symbol select the next element after the previous selected.

6 Comments

One more time :) POSITION OF ELEMENTS CAN BE ONLY NEXT: <div class="elementA"></div> <div class="elementB"></div> <div class="elementC"></div> NO "C" before "A"!
That's going to be impossible with pure CSS I'm afraid... What is the reason for the fixed order? For visible positions I can imagine, therefore the position fix in my answer.
Yes, it is about position, I can try to swap DIVs position by CSS. But nit sure how to do it for now, actually I am not strong in CSS
Well, absolute elements can have top, right, bottom and left attributes to position them. I've made a sample; jsfiddle.net/2wGrw, hope it will help you out.
Both fiddles have elementC before elementA
|
0

Yes, you can do only with CSS, but 'elementA' MUST be AFTER 'elementC' Here's the code

.elementC:hover ~ .elementA {
    color: #ccc /* example */
}

And here's the documentation of the General Sibling Selector

2 Comments

"but 'elementC' MUST be AFTER 'elementA'" Well I can't change element location :(
impossible only css. use js :)
0
wrapper{ background:black;}

.elementA {
    display: block;
    width: 100%; background:blue;
    height: 50px;
}

.elementB {
    display: block;
    color: #ffffff;
    background-color:red;
    text-align: center;
    width:100%;
    padding: 10px;
}


.elementC {
    display: block;
    color: #ffffff;
    background-color:pink;
    text-align: center;
    width:100%;
    padding: 10px;
}

.elementA:hover + .elementC + .elementB {
    display: block;
    background:green;
}

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.