悬停时在图像上显示标题

时间:2016-10-11 11:31:44

标签: html css

当有人在图片上盘旋时,我试图显示标题。但我无法找到办法完成这项任务。

我正在使用 Twitter Bootstrap

以下是其中一张图片的HTML。我不知道如何将内容放入其中,因此只有当有人用鼠标移动它时才会显示。

  <div class="col-lg-2 col-md-3 col-sm-4 col-xs-12 thumb">
        <a class="thumbnail caption-style-1 model-thumbnail " href="#">
          <img class="img-responsive" src="http://placehold.it/245x347" alt>
        </a>
  </div>

enter image description here

Bootply

2 个答案:

答案 0 :(得分:1)

这是一个受this website启发的很酷的例子。

ul.img-list {
  text-align: center;
}

ul.img-list li {
  display: inline-block;
  height: 150px;
  position: relative;
  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;
  display: table;
  height: 150px;
  left: 0;
  position: absolute;
  top: 0;
  width: 150px;
  opacity: 0;
}

ul.img-list li:hover span.text-content {
  opacity: 1;
}
<ul class="img-list">
  <li>
      <img src="http://placehold.it/150x150"/>
      <span class="text-content"><span>1st image</span></span>
  </li>
    <li>
      <img src="http://placehold.it/150x150"/>
      <span class="text-content"><span>2nd image</span></span>
  </li>
    <li>
      <img src="http://placehold.it/150x150"/>
      <span class="text-content"><span>3rd image</span></span>
  </li>
</ul>

答案 1 :(得分:0)

div {width: 245px; height: 245px;}
a {position: relative; display: inline-block;}
a + span {display: none; position: absolute; top: 0; left: 0; bottom: 0; right: 0;  background: rgba(0,0,0,0.5);}
a:hover + span {display: block; pointer-events: none;}
<div class="col-lg-2 col-md-3 col-sm-4 col-xs-12 thumb">
  <a class="thumbnail caption-style-1 model-thumbnail " href="#">
    <img class="img-responsive" src="http://placehold.it/245x347" alt>
  </a>
  <span>Text</span>
</div>