简体   繁体   English

CSS悬停文字悬停在图片上

[英]CSS hover text over image on hover

I am following a tutorial for hover effect on images where text pops up and background turns dark. 我正在关注一个在文本弹出且背景变暗的图像上悬停效果的教程。 Tutorial here . 教程在这里

I am having a small problem with the hover effect, when mouse hovers over the image, I want the image to go dark and the entire image is covered, when I set the ul.img-list li to height 100% it only takes up 100% of the text, I can set the height to the height of the image, but when the height changes based on the window size, it won't cover properly. 我的悬停效果有一个小问题,当鼠标悬停在图像上时,我希望图像变暗并覆盖整个图像,当我将ul.img-list li设置为高度100%时,它只会占用我可以将文本的100%设置为图片的高度,但是当高度根据窗口大小而变化时,它将无法正确覆盖。 Am I missing something? 我想念什么吗?

图片范例

HTML: HTML:

<div class="column large-6">
   <ul class="img-list">
     <li>
       <a href="#">
         <img src="https://www.ricoh.com/r_dc/r/r8/img/sample_08.jpg" />
         <span class="text-content">
           <span>Hello@!</span>
        </span>
      </a>
    </li>
 </ul>

CSS: CSS:

ul.img-list {
  list-style-type: none;
  margin: 0;
  padding: 0;
  text-align: center;
}
ul.img-list li {
  display: inline-block;
  height: 100%;
  margin: 0 1em 1em 0;
  position: relative;
  width: 100%;
}
span.text-content {
  background: rgba(0,0,0,0.5);
  color: white;
  cursor: pointer;
  display: table;
  height: 150px;
  left: 0;
  position: absolute;
  top: 0;
  width: 150px;
}

span.text-content span {
  display: table-cell;
  text-align: center;
  vertical-align: middle;
}

span.text-content {
  background: rgba(0,0,0,0.5);
  color: white;
  cursor: pointer;
  display: table;
  height: 100%;
  left: 0;
  position: absolute;
  top: 0;
  width: 150px;
  opacity: 0;
  -webkit-transition: opacity 500ms;
  -moz-transition: opacity 500ms;
  -o-transition: opacity 500ms;
  transition: opacity 500ms;
}

Here is my jsfiddle - http://jsfiddle.net/f32kn4h5/ 这是我的jsfiddle- http://jsfiddle.net/f32kn4h5/

I think part of your problem after looking at your fiddle is that none of your parent divs have a height property set to something static. 我认为看了小提琴之后的问题之一就是没有一个父div的height属性设置为static。 If you try changing the span styling to say, 300px , you will see it change: 如果您尝试将跨度样式更改为300px ,则会看到其变化:

span.text-content {
  height: 300px;
}

alternatively, set every parent of that text span's height to 100% or something, but height must be present for every parent: 或者,将文本范围的每个父级的高度设置为100%左右,但每个父级必须存在高度:

<div class="column large-6"> //set to 100% or something
   <ul class="img-list">  //set to 100% or something
     <li>  //set to 100% or something
       <a href="#">
         <img src="https://www.ricoh.com/r_dc/r/r8/img/sample_08.jpg" />
         <span class="text-content">
           <span>Hello@!</span>
        </span>
      </a>
    </li>
 </ul>

this should make it work, here's an update to your fiddle: http://jsfiddle.net/f32kn4h5/3/ 这应该可以使它起作用,这是您的提琴的更新: http : //jsfiddle.net/f32kn4h5/3/

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

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