简体   繁体   中英

CSS change div when hover on another div

Im trying to make a div change when I hover on another div. I have done this a million times before but for some reason I just can not see why this one isnt working.

CSS

#floatimg:hover > #float_tools
{
opacity: 1;
}
#float_tools
{
width: 150px;
margin: auto;
opacity: 0.3;
}

HTML

<div id='floatBox'>
        <div id='float_image'>
            <img id="floatimg" src="">
        </div>
        <div id='float_tools'>
            <div id='float_prev'><a href='javascript:void' onclick=''><img src='images/Button_Prev.png' width='50px'></a></div>
            <div id='float_close'><a href='javascript:void' onclick=''><img src='images/close.png' width='50px'></a></div>
            <div id='float_next'><a href='javascript:void' onclick=''><img src='images/Button_Next.png' width='50px'></a></div>
        </div>
    </div>

It's simply because #float_tools div is not a direct child of #floatimg .

Try something like:

<div id='floatBox'>
        <div id='float_image'>
            <img id="floatimg" src="">
            <div id='float_tools'>
                <div id='float_prev'><a href='javascript:void' onclick=''><img src='images/Button_Prev.png' width='50px'></a></div>
                <div id='float_close'><a href='javascript:void' onclick=''><img src='images/close.png' width='50px'></a></div>
                <div id='float_next'><a href='javascript:void' onclick=''><img src='images/Button_Next.png' width='50px'></a></div>
            </div>
        </div>
    </div>

CSS:

#float_image:hover > #float_tools
{
opacity: 1;
}
#float_tools
{
width: 150px;
margin: auto;
opacity: 0.3;
}

When you try to use the greater than sign ( > ), the affected element should be a direct child of the hovered element. In your HTML, the hovered element and the affected element are not directly related to each other.

It would be better this way:

#float_image:hover + #float_tools
{
opacity: 1;
}
#float_tools
{
width: 150px;
margin: auto;
opacity: 0.3;
}

Here is a fiddle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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