CSS悬停会在属性后用伪造影响另一个元素

发布于 2025-01-18 00:01:29 字数 242 浏览 3 评论 0原文

以下CSS起作用(在按钮1上悬停时可以看到可见的按钮2),

#btn1:hover ~ #btn2 { visibility: initial;}

但是如果我想为伪元素#btn2进行相同的操作:之后,以下代码不起作用,

#btn1:hover ~ #btn2:after { visibility: initial;}

是否有原因或解决方法?

This following css works (it makes visible button2 when hovering on button1)

#btn1:hover ~ #btn2 { visibility: initial;}

But if I want to make the same for the pseudo element #btn2:after, the following code doesn't work

#btn1:hover ~ #btn2:after { visibility: initial;}

Is there a reason or a workaround for it ?

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

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

发布评论

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

评论(3

似最初 2025-01-25 00:01:29

可能您需要将 content 属性添加到 btn2

It could be that you need to add the content property to btn2 ????

#btn1:hover ~ #btn2 { 
  visibility: initial;
  background-color:red
}
#btn1:hover ~ #btn2:after{
  visibility: initial;
  content:' Soy el after';
}
<button id="btn1">Botón 1</button>
<button id="btn2">Botón 2</button>

原谅过去的我 2025-01-25 00:01:29

您还可以使用display:none;

使用visibilty:hidden;您的按钮仍将在您的身体元素中具有宽度和高度,但是当使用;您可以将任何元素放在该位置。

body {
  position: relative;
  height: 100vh;
  background-color: bisque;
  display: flex;
  flex-direction: row;
  justify-content: center;
  align-items: center;
  gap:1rem;
}
button{
  height: 50px;
  width:100px;
}

#btn2::after{
  content: '';
  width:20px;
  height: 20px;
  background-color: red;
  display: none;
}

#btn1:hover ~ #btn2::after {
   display: inline-block;
}
<button id="btn1">btn1</button>
<button id="btn2">btn2</button>

You can also use display:none;

When you use visibilty:hidden; your buttons will still have width and height in your body element but when using display:none; you can place any element to that position.

body {
  position: relative;
  height: 100vh;
  background-color: bisque;
  display: flex;
  flex-direction: row;
  justify-content: center;
  align-items: center;
  gap:1rem;
}
button{
  height: 50px;
  width:100px;
}

#btn2::after{
  content: '';
  width:20px;
  height: 20px;
  background-color: red;
  display: none;
}

#btn1:hover ~ #btn2::after {
   display: inline-block;
}
<button id="btn1">btn1</button>
<button id="btn2">btn2</button>

攀登最高峰 2025-01-25 00:01:29

显然你们都是对的。我做了一些调整,它起作用了...无法弄清楚

我使用了可见性参数出了什么问题

Obviously you are both right. I made some adjustments and it works... can't figure what was wrong

I used visibility parameter

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