gpt4 book ai didi

html - 悬停一个元素时如何影响其他元素

转载 作者:行者123 更新时间:2023-11-28 07:56:10 26 4
gpt4 key购买 nike

我想做的是当某个 div 悬停时,它会影响另一个 div 的属性。

例如,在 this JSFiddle demo 中,当您将鼠标悬停在 #cube 上时,它会更改 background-color 但我想要的是,当我将鼠标悬停在 #container 上时, #cube受到影响。

div {
outline: 1px solid red;
}

#container {
width: 200px;
height: 30px;
}

#cube {
width: 30px;
height: 100%;
background-color: red;
}

#cube:hover {
width: 30px;
height: 100%;
background-color: blue;
}
<div id="container">
<div id="cube">
</div>
</div>

最佳答案

如果立方体直接在容器内:

#container:hover > #cube { background-color: yellow; }

如果立方体紧挨着容器(在容器结束标签之后):

#container:hover + #cube { background-color: yellow; }

如果立方体在容器内的某处:

#container:hover #cube { background-color: yellow; }

如果立方体是容器的兄弟:

#container:hover ~ #cube { background-color: yellow; }

关于html - 悬停一个元素时如何影响其他元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30105181/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com