How to affect other elements when one element is hovered
P粉769413355
2023-08-27 12:48:40
<p>What I want to do is when one <code>div</code> is hovered, it affects the properties of another <code>div</code>. </p>
<p>For example, in this JSFiddle demo, when you hover over <code>#cube</code> it changes the <code>background-color</code> but I want The thing is, when I hover over <code>#container</code>, <code>#cube</code> is affected. <!-- p-->
</p><p><br /></p>
<pre class="brush:css;toolbar:false;">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;
}</pre>
<pre class="brush:html;toolbar:false;"><div id="container">
<div id="cube">
</div>
</div></pre>
<p><br /></p>
In this specific example you can use:
This example only works if
cube
is a child ofcontainer
. For more complex scenarios, you'll need to use different CSS, or use JavaScript.If the cube is directly inside the container:
If the cube is located next to the container (after the container's closing tag):
If the cube is located somewhere inside the container:
If the cube is a sibling of the container: