我想在用户在css中悬停对象1时设置对象2的样式。例如:
<style>
.object1:Hover then style object2{
Styling of object2 Goes Here
}
</style>
我该怎么做
答案 0 :(得分:1)
只要html元素1和2满足某些条件,您就可以通过多种方式执行此操作:
此类选择器的示例:
.object1:hover .object2 { your css rules here }
适用于html结构,其中.object2
是.object1
的子元素:
<div class="object1">
<div class="object2">Some content</div>
</div>
.object1:hover + .object2 { your css rules here }
适用于(一个!)立即跟随兄弟 .object2
:
<div class="object1"></div
<div class="object2">This will be affected.</div>
<div class="object2">This will NOT be affected.</div>
.object1:hover ~ .object2 { your css rules here }
将适用于所有(可能是许多!)同级.object2
的风格(但+
不适用于儿童.object2
}):
<div class="object1">
<div class="object2">This will NOT be affected.</div>
</div>
<div class="object2">This will be affected.</div>
<someElementWhichisNotAffected></someElementWhichisNotAffected>
<p class="object2">This will be affected.</p>
答案 1 :(得分:0)
*{
padding: 0;
margin: 0;
box-sizing: border-box;
}
div{
width: 200px;
height: 200px;
border: 2px solid #ccc;
margin: 10px auto;
}
.div1:hover{
border: 2px solid #000;
}
.div1:hover ~ .div2{
background: #f00;
}
&#13;
<div class="div1"></div>
<div class="div2"></div>
&#13;