简体   繁体   English

使用CSS在鼠标悬停时显示Div文本

[英]Display Div text on mouse hover using css

I am new to css and ui designing. 我是CSS和ui设计的新手。 Here's the case. 就是这种情况。 I have a div(divmain). 我有一个div(divmain)。 Inside that divmain, I have an image and another div (sub_div). 在该divmain中,我有一个图像和另一个div(sub_div)。 There are texts inside sub_div which I want to display on mouse hover, hidden otherwise using just css (no javascript/jquery). 我想在鼠标悬停时显示sub_div中的文本,否则仅使用css隐藏这些文本(不使用javascript / jquery)。 Kindly suggest the approach I should follow. 请提出我应该遵循的方法。

Right now I am doing something like: 现在我正在做类似的事情:

Html: HTML:

<div class="divmain">
<img src="" />
<div class="sub_div">Some Text</div>
</div>

Css: CSS:

.divmain .sub_div {
    height: 70px;
    opacity: 0;
    position: absolute;
    bottom: 0px;
    left: 0px;
    right: 0px;
    padding: 2px 0px;
    color: #ffffff;
    text-decoration: none;
    vertical-align: middle;
    text-align: center;
    -webkit-transition: opacity 500ms;
    -moz-transition: opacity 500ms;
    -o-transition: opacity 500ms;
    transition: opacity 500ms;
    margin-top: 35px;
    line-height: 70px;

}

.divmain:hover .sub_div {
    opacity: 0.8;
}

The Below Code should help you: 以下代码可以帮助您:

 .divmain{ width: 200px; } .sub_div{ display:none; /* This code hides the text initially */ /* Your sub_div styles can go here*/ } .divmain:hover .sub_div{ display:block; /*Here we are making the .sub_div visible on hover*/ } 
 <div class="divmain"> <img src="http://wallpapercave.com/wp/EC4hMSt.png" width="200px" height="auto" /> <div class="sub_div">Some Text</div> </div> 

This should help you 这应该对你有帮助

<div class="divmain">
<img src="mrm.gif" alt="Smiley face" />
<div class="sub_div">Some Text</div>
</div>

.divmain .sub_div {
      display: none;
}

.divmain:hover .sub_div {
    display: block;
}

Click to view CodePen 点击查看CodePen

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

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