当鼠标位于不同div上方时如何显示其他div?

发布于 2025-01-14 11:28:23 字数 644 浏览 2 评论 0原文

我想当光标悬停在 .a div 上时显示 .b div。可以做吗? 我知道当我将 .b 放入 .a div 时可以做到这一点,但我宁愿不这样做。

<html> 
<head> 
    <title> 
        How to 
    </title>
    <style>
        .b{
            background-color: royalblue;
    display:none;
        }
        .a:hover > b{
            display: block;
        }
.a:hover{
    background-color: pink;
}
.a{
    background-color: lawngreen;
}

    </style>
</head> 
  
<body>
    <div class="a" style="width: 200px; height: 200px; "></div>
    <div class="b" style="width: 100px; height: 100px;"></div>
</body> 

i want to display .b div when cursor is hover on .a div. Is it possible to do?
I know that its possible to do when i put .b into .a div, but i would rather not to do it.

<html> 
<head> 
    <title> 
        How to 
    </title>
    <style>
        .b{
            background-color: royalblue;
    display:none;
        }
        .a:hover > b{
            display: block;
        }
.a:hover{
    background-color: pink;
}
.a{
    background-color: lawngreen;
}

    </style>
</head> 
  
<body>
    <div class="a" style="width: 200px; height: 200px; "></div>
    <div class="b" style="width: 100px; height: 100px;"></div>
</body> 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

弃爱 2025-01-21 11:28:23

您可以使用 css + 加选择器

重要通知:
仅当 a 位于 b 旁边,并且 a 之间有(例如)c 时,此功能才有效和c,它不会工作

<html> 
<head> 
    <title> 
        How to 
    </title>
    <style>
        .b{
            background-color: royalblue;
    display:none;
        }
        .a:hover + .b{
            display: block;
        }
.a:hover{
    background-color: pink;
}
.a{
    background-color: lawngreen;
}

    </style>
</head> 
  
<body>
    <div class="a" style="width: 200px; height: 200px; ">A</div>
    <div class="b" style="width: 100px; height: 100px;">B</div>
</body>

You could you use the css + plus selector

Important notice:
This will only work if a is next to b, if there is (for example) a cbetween a and c, it will not work

<html> 
<head> 
    <title> 
        How to 
    </title>
    <style>
        .b{
            background-color: royalblue;
    display:none;
        }
        .a:hover + .b{
            display: block;
        }
.a:hover{
    background-color: pink;
}
.a{
    background-color: lawngreen;
}

    </style>
</head> 
  
<body>
    <div class="a" style="width: 200px; height: 200px; ">A</div>
    <div class="b" style="width: 100px; height: 100px;">B</div>
</body>

苏别ゝ 2025-01-21 11:28:23
.a{
    background-color: green;
}
.a:hover{
    background-color: pink;
}

.b{
    background-color: royalblue;
    display:none;
 }
 .a:hover + .b {
    display: block;
 }
<body>
  
  <div class="a" style="width: 200px; height: 200px; "></div>
  <div class="b" style="width: 100px; height: 100px;"></div>
    
</body> 

是的,您可以使用 相邻同级选择器(+)

这是一个 演示

这里唯一需要注意的是样式将被应用到下一个元素。

您还可以检查 通用兄弟选择器(~)

.a{
    background-color: green;
}
.a:hover{
    background-color: pink;
}

.b{
    background-color: royalblue;
    display:none;
 }
 .a:hover + .b {
    display: block;
 }
<body>
  
  <div class="a" style="width: 200px; height: 200px; "></div>
  <div class="b" style="width: 100px; height: 100px;"></div>
    
</body> 

Yes, you can do it by using adjacent sibling selector (+)

Here is a demostration

Only catch here is that style will be applied to the next element.

You can also check General sibling selector(~)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文