简体   繁体   English

CSS悬停更改另一个div(在另一个单独的包装div中)

[英]CSS hover change another div (within another seperate wrapper div)

I am trying to alter another div element with css hover. 我试图用css悬停更改另一个div元素。 I got it working when they are under the same container. 当它们在同一容器中时,我可以正常工作。 But on my site the element that has to be changed has to be under another separate wrapper div. 但是在我的网站上,必须更改的元素必须位于另一个单独的包装div下。

HTML HTML

<div class="a">Div A</div>
<div id="box">
    <div class="c">Div C</div>
</div>

CSS CSS

.a:hover ~ .c {
    background: #3F6;
}

#box {
}

Here's what I want to achieve... works in the first set when you hover over Div A. Does not on the second set. 这是我想要实现的...当将鼠标悬停在Div A上时,它在第一组中起作用。在第二组上却不起作用。 https://jsfiddle.net/69017jwc/ https://jsfiddle.net/69017jwc/

In the second example, .c isn't a sibling of the .a element. 在第二个示例中, .c不是.a元素的同级。

You would need to select the sibling #box element and then select the descendant .c element from there, .a:hover ~ #box .c : 您需要选择同级#box元素,然后从那里选择后代.c元素.a:hover ~ #box .c

Updated Example 更新示例

.a:hover ~ .c,
.a:hover ~ #box .c {
    background: #3F6;
}

If the element that contains the .c element doesn't have a class or an id, then you could just use the universal selector: 如果包含.c元素的元素没有类或ID,则可以使用通用选择器:

.a:hover ~ * .c {
    background: #3F6;
}

Im not too sure what you are looking to do but from what I understand you want it to highlight each different "question" sepertly. 我不太确定您要做什么,但据我了解,您希望它分别突出显示每个不同的“问题”。 If so try this: 如果是这样,请尝试以下操作:

<div>
<div class="a">Div A</div>
<div class="b">Div B</div>
<div class="c">Div C</div>
</div>
<br /><br />
<div class="a">Div A</div>
<div class="b">Div B</div>
<div class="c">Div C</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM