简体   繁体   English

如何让伪元素悬停时不出现hover效果

[英]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.

相关问题 如何让hover上的按钮落入伪元素? - How to make button fall in pseudo-element on hover? 悬停时不出现伪元素 - Pseudo element doesnt appear when hovered 如何使“主要内容”在“悬停”上移动并保持在伪元素 static 之前/之后? - How to make "main content" move on "hover" and keep before/after pseudo-element static? 伪元素背景揭示:悬停 - Pseudo-element Background Reveal on :hover 如何使CSS伪元素区域可点击? - How to make the area of CSS pseudo-element clickable? 当一个元素悬停并影响css中的另一个元素/类时,如何添加悬停效果? - How to add hover effect when one element hovered and affect on another element / class in css? 如何使这种悬停效果(悬停1按钮更改元素的ID,然后在悬停时将其更改回其原始ID?)[jquery,css3] - How do I make this hover effect (hover 1 button to change an element's ID then when hovered-out it changes back to it's original ID?) [jquery,css3] 当元素不再悬停时,停止jQuery悬停效果 - Stop jQuery hover effect when element is not hovered anymore 如何在使用 JavaScript 激活 a:hover 伪元素后更改 CSS 元素的内容? - How do I change the contents of a CSS element after a :hover pseudo-element activates using JavaScript? CSS 区分悬停在元素上和 ::before 伪元素 - CSS distinguish between hover on element and it's ::before pseudo-element
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM