[英]How to make hover effect not appear when pseudo-element is hovered on
I want the hover effect (background change of pseudo-element to green) to only appear when the button is being hovered, however it also appears when the pseudo-element (green box) is hovered on.我希望 hover 效果(伪元素的背景变为绿色)仅在悬停按钮时出现,但是当悬停伪元素(绿色框)时它也会出现。
button { box-sizing: border-box; background-color: blue; padding: 10px 20px; color: white; position: relative; } button::before { content: ''; position: absolute; background-color: transparent; height: 20px; width: 100%; left: 0; bottom: -30px; } button:hover::before { background-color: green; }
<button> I am a button </button>
You can achieve that by first setting the default state of the pseudo-element to hidden by setting the display
property to display: none;
您可以通过首先将伪元素的默认值 state 设置为隐藏来实现这一点,方法是将
display
属性设置为display: none;
. .
On the hover event of the button
element you can make it visible by updating the display
property to display: block;
在
button
元素的 hover 事件中,您可以通过将display
属性更新为display: block;
使其可见。 But if you loose mouse over from the button
element it will be hidden again.但是,如果您将鼠标从
button
元素上松开,它将再次隐藏。
Hope this is not for some kind of drop-down menu, if so, this is not how you should go about doing it.希望这不是某种下拉菜单,如果是这样,这不是你应该如何做的 go。
button { box-sizing: border-box; background-color: blue; padding: 10px 20px; color: white; position: relative; } button::before { content: ''; position: absolute; background-color: transparent; height: 20px; width: 100%; left: 0; bottom: -30px; display: none; } button:hover::before { background-color: green; display: block; }
<button> I am a button </button>
@nad by adding pointer-events: none;
@nad 通过添加
pointer-events: none;
you can prevents all click and cursor events.您可以阻止所有点击和 cursor 事件。
button { box-sizing: border-box; background-color: blue; padding: 10px 20px; color: white; position: relative; } button::before { content: ''; position: absolute; background-color: transparent; height: 20px; width: 100%; left: 0; bottom: -30px; pointer-events: none; } button:hover::before { background-color: green; }
<button> I am a button </button>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.