简体   繁体   English

CSS-将鼠标悬停在元素上不会将样式应用于另一样式

[英]CSS - Hover on element doesn't apply style to another one

I have a problem with applying style to a div when another div is hovered. 当另一个div悬停时,将样式应用于div时出现问题。 Here is my scenario: 这是我的情况:

<div id="nav">
    <div class="logged"></div>
    <div class="nav_child"></div>
</div>
.logged:hover .nav_child {
    display: initial;
}
#nav .nav_child {
    display: none;
}

But it doesn't work. 但这是行不通的。 I also tried .logged:hover > .nav_child and .logged:hover + .nav_child and still nothing. 我还尝试了.logged:hover > .nav_child.logged:hover + .nav_child ,仍然没有。 It only works with #nav:hover .nav_child but I need to show it when .logged is hovered, not when #nav is hovered. 它仅适用于#nav:hover .nav_child但我需要表现出来时.logged悬停,而不是在#nav盘旋。

Any ideas? 有任何想法吗?

Just solved with the Adjacent sibling combinator . 刚刚用相邻的同级组合器解决了。 I declare and call 'logged' as id and add + between .logged:hover and .nav_child it works. 我声明并调用“ logged”作为id,并在.logged:hover和.nav_child之间添加+,它起作用了。

Reference: Adjacent sibling combinator: http://css-tricks.com/child-and-sibling-selectors 参考:相邻的同级组合器: http : //css-tricks.com/child-and-sibling-selectors

Old code: http://jsfiddle.net/8rdh7m2j/ 旧代码: http//jsfiddle.net/8rdh7m2j/

Working code: 工作代码:

<div id="nav">
<div class="logged" id="logged">Hello</div>
<div class="nav_child">Puff</div>
</div>
#logged:hover + .nav_child {
display: initial;
}

#nav .nav_child {
display: none;
}

Demo: http://jsfiddle.net/hwh3o8u0/1/ 演示: http//jsfiddle.net/hwh3o8u0/1/

Even just remove the #nav from child and + 甚至只需删除#nav即可删除孩子和+

.logged:hover+.nav_child {
    display:initial;
}
 .nav_child {
    display: none;
}

http://jsfiddle.net/dz22m10y/ http://jsfiddle.net/dz22m10y/

You were on the right track with the .logged:hover + .nav_child approach. .logged:hover + .nav_child方法使您.logged:hover + .nav_child正确的轨道上。 The reason it didn't work is that #nav .nav_child has higher specificity (because it includes an ID), and therefore takes precedence over the preceding rule. 它不起作用的原因是#nav .nav_child具有更高的特异性(因为它包含ID),因此优先于前面的规则。

Yes, you could solve that by moving to an ID for logged , as you did in your self-answer, but instead of doing that, you could raise specificity of the hovering rule by adding in the ID you've already got there too: 是的,您可以通过移至logged的ID来解决此问题,就像在自助回答中所做的那样,但是您可以通过添加已经存在的ID来提高悬停规则的特异性,而不是这样做:

#nav .logged:hover + nav_child {

By the way, such problems can nearly always be tracked down using the style inspector in your browser's dev panel. 顺便说一下,几乎可以使用浏览器开发面板中的样式检查器来跟踪此类问题。 In this case, you would see both rules, and you would see that the second was overriding the first. 在这种情况下,您将看到两个规则,并且您会看到第二个规则将覆盖第一个规则。 That would easily lead you to the conclusion that it is a specificity problem. 这很容易导致您得出结论,这是一个特异性问题。 Most style inspectors will give you the ability to "emulate" a hovering state. 大多数样式检查器将使您能够“模仿”悬停状态。

For more information on specificity, see https://developer.mozilla.org/en-US/docs/Web/CSS/Specificity . 有关特异性的更多信息,请参见https://developer.mozilla.org/en-US/docs/Web/CSS/Specificity In your case, your original first rule had a specificity of 20 (two classes), and the second one 101 (one ID and one class). 在您的情况下,您最初的第一条规则的特异性为20(两个类别),第二个为101(一个ID和一个类别)。

Here is a good resource that covers this topic http://jsfiddle.net/ThinkingStiff/a3y52/
<div id="nav">
    <div id="one" class="logged"></div>
    <div id="two" class="nav_child"></div>
</div>
#one:hover ~ two,
{
    background-color: black;
    color: white;
}

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

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