div中的标题用作叠加层。如何将文本置于其中?

时间:2017-04-05 12:50:56

标签: html css twitter-bootstrap css3

我不能为我的生活弄清楚如何在图像悬停时让标题文本居中。我尝试了垂直对齐并将其指定为内联块,以不同的方式进行游戏。我觉得我错过了一些明显的东西。

https://jsfiddle.net/b5tmndvk/2/

HTML

<div class="container">
  <div class="row">
    <div class="col-md-6 col-md-offset-3">
      <div class="unit-thumbnail">
        <img src="http://i.imgur.com/yIKP2YB.jpg?1" width="100%">
        <div class="unit-caption">
          <p>Updoot this Stack Overflow post and help me figure this out.</p>
        </div>
      </div>
    </div>
  </div>
</div>

CSS

.unit-thumbnail {
    position:relative;
    overflow:hidden;
}

.unit-caption {
    position:absolute;
    bottom:-100%;
    right:0;
    background:rgba(37, 139, 202, 0.75);
    width:100%;
    height:100%;
    padding:2%;
    text-align:center;
    color:#fff !important;
    z-index:2;
    -webkit-transition: all 0.9s ease-in-out;
    -moz-transition: all 0.9s ease-in-out;
    -o-transition: all 0.9s ease-in-out;
    -ms-transition: all 0.9s ease-in-out;
    transition: all 0.9s ease-in-out;
}
.unit-thumbnail:hover .unit-caption {
    bottom:0%;
}

1 个答案:

答案 0 :(得分:0)

添加此css

.unit-caption p{
    position: relative;
    top: 50%;
    transform: translateY(-50%);
}

https://jsfiddle.net/b5tmndvk/3/