简体   繁体   English

将鼠标悬停时显示隐藏的div

[英]Show hidden div when it is hovered over

I have aa hidden div and I would like to display it when it is hovered over using CSS. 我有一个隐藏的div ,当它悬停在CSS上时,我想显示它。 Unfortunately, no matter what I do, the CSS hover selector does not seem to work. 不幸的是,无论我做什么,CSS悬停选择器似乎都不起作用。 In the chrome developer's tools, I could show the div by forcing the :hover state, but otherwise it does not work. 在chrome开发人员的工具中,我可以通过强制:hover状态来显示div,但否则它将不起作用。

here is my code 这是我的代码

 .invisible{ display: none; border: 2px solid red; } .invisible:hover{ display: block; } 
 <div class="invisible"> Text text text </div> 

An element with display: none can not be hovered over as it takes up no space in the document. 具有display: none的元素display: none无法将其悬停,因为它不占用文档中的任何空间。 You can set the element's opacity to 0 initially and change the opacity to 1 when it is hovered over to make it not visible but still take up space in the document and thus will be able to be hovered over. 您可以将元素的opacity最初设置为0 ,并将其悬停在其上时将opacity更改为1 ,以使其不可见,但仍占据文档中的空间,因此可以将其悬停。

 .invisible{ opacity: 0; border: 2px solid red; } .invisible:hover{ opacity: 1; } 
 <div class="invisible"> Text text text </div> 

If you want the div to appear smoothly, you can use the transition property. 如果希望div平滑显示,则可以使用transition属性。 Since only the opacity changes, you can add an opacity transition like this: transition: opacity 1s . 由于仅opacity发生变化,因此您可以添加一个不透明度过渡,如下所示: transition: opacity 1s 1s is the duration of the transition (one second). 1s是过渡的持续时间(一秒)。 You can change it to make the div appear slower or faster. 您可以更改它以使div变慢或变快。

 .invisible{ opacity: 0; border: 2px solid red; transition: opacity 1s; } .invisible:hover{ opacity: 1; } 
 <div class="invisible"> Text text text </div> 

See this fiddle, This might help, what you are trying to achieve. 看到这个小提琴,这可能会有所帮助,您想要实现的目标。 You can adjust hover area using parent div dimensions. 您可以使用父div尺寸调整悬停区域。 https://jsfiddle.net/pcwudrmc/37399/ https://jsfiddle.net/pcwudrmc/37399/

 .parent { padding: 20px; } .parent .child { display: none; background: red; padding: 20px; text-align: center; } .parent:hover .child { display: block; } 
 <div class="parent"> <div class="child"> Hello, World! </div> </div> 

You could use visibility : 您可以使用visibility

 .content .invisible { visibility: hidden; border: 2px solid red; } .content:hover .invisible { visibility: visible; } 
 <div class="content"> <div class="invisible"> <span>Text text text</span> </div> </div> 

Make sure to add a parent div, otherwise it wouldn't work on Chrome 23+. 确保添加父div,否则在Chrome 23+上将无法使用。

 .invisible{ opacity: 0; border: 2px solid red; transition: all 1s; } .invisible:hover{ opacity: 1; transition: all 1.5s; } 
 <div class="invisible"> Text text text </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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