如何在 img 悬停上垂直居中字幕?

How do I vertically center caption on img hover?

我目前无法在鼠标悬停时将字幕置于 div 中心。 div 内的图像比 div 大,我希望标题在 div?

内垂直居中

HTML

<div class="container-fluid">

    <div id="page">

<!-- GRID ITEM -->
        <div class="item s1">
            <a href="#">
                <div class="grid-image">
                    <img         src="https://upload.wikimedia.org/wikipedia/commons/thumb/4/4e/Pleiades_large.jpg/1024px-Pleiades_large.jpg">
                </div>
                <div class="item-caption">
                    <h5>I want this to be center</h5>
                </div>
            </a>
        </div>
<!-- /GRID ITEM -->

    </div>
</div>

CSS

#page .item {
    width: calc(16.66% - 10px);
    display: inline-block;
    height: 0;
    float: left;
    padding-bottom: 16.66%;
    overflow: hidden;
    background-color: salmon;
    margin: 5px;
    position: relative;
}

#page .item.s1 {
    width: calc(50% - 10px);
    padding-bottom: 50%;
    overflow: hidden;
    background-color: navy;
}

.item > a {
    position: relative;
    display: block;
    overflow: hidden;
    color: white;
}
.item:hover .grid-image:after {
    background: rgba(0, 0, 0, .7);
}
.item:hover .grid-image > img {
    -webkit-transform: scale(1.1);
       -moz-transform: scale(1.1);
        -ms-transform: scale(1.1);
         -o-transform: scale(1.1);
            transform: scale(1.1);

}
.item:hover .item-caption {
    opacity: 1;
    z-index: 3;
  visibility: visible;

}
.item-caption,
.grid-image > img,
.grid-image:after {
    -webkit-transition: all 0.3s ease-in-out 0s;
       -moz-transition: all 0.3s ease-in-out 0s;
        -ms-transition: all 0.3s ease-in-out 0s;
         -o-transition: all 0.3s ease-in-out 0s;
            transition: all 0.3s ease-in-out 0s;
}
.item-caption {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  background: rgba(29, 106, 154, 0.72);
  color: #fff;
  visibility: hidden;
  text-align: center;
  opacity: 0;
}



.grid-image {
    position: relative;
    overflow: hidden;
}

.grid-image img {
    display: block;
    overflow: hidden;
}

.grid-image:after {
    position: absolute;
    display: block;
    content: "";
  height: 100%;
  width: 100%;
    top: 0;
    left: 0;
}

JavaScript

var $container = $('#page');
$container.masonry({
columnWidth: '.grid-sizer',
itemSelector: '.item',
percentPosition: true,
gutter: 10
});

Here's a fiddle

谢谢!

试试这个:http://jsfiddle.net/2stywe2t/1/

我做了什么:

  • 将新的 div 添加到您的 HTML,就在 .item-caption 下方。
  • .item-caption display: table 属性和其他一些属性
  • 给了新的divdisplay: table-cellvertical-align: middle
  • a 祖先
  • 中删除了 position:relative

有道理吗?

找到答案了!!稍微整理一下代码。

var $container = $('#page');
$container.masonry({
    columnWidth: '.grid-sizer',
    itemSelector: '.item',
    percentPosition: true,
    gutter: 10
});
#page .item {
    width: calc(16.66% - 10px);
    display: inline-block;
    height: 0;
    float: left;
    padding-bottom: 16.66%;
    overflow: hidden;
    background-color: salmon;
    margin: 5px;
    position: relative;
}
#page .item.s1 {
    width: calc(50% - 10px);
    padding-bottom: 50%;
    overflow: hidden;
    background-color: navy;
    position: relative!important;
}
.item > a {
    position: absolute;
    display: block;
    width: 100%;
    height: 100%;
    overflow: hidden;
    color: white;
}
.item:hover .grid-image:after {
    background: rgba(0, 0, 0, .7);
}
.item:hover .grid-image > img {
    -webkit-transform: scale(1.1);
    -moz-transform: scale(1.1);
    -ms-transform: scale(1.1);
    -o-transform: scale(1.1);
    transform: scale(1.1);
}
.item:hover .item-caption {
    opacity: 1;
    z-index: 3;
    visibility: visible;
}
.item-caption, .grid-image > img, .grid-image:after {
    -webkit-transition: all 0.3s ease-in-out 0s;
    -moz-transition: all 0.3s ease-in-out 0s;
    -ms-transition: all 0.3s ease-in-out 0s;
    -o-transition: all 0.3s ease-in-out 0s;
    transition: all 0.3s ease-in-out 0s;
}
.item-caption {
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    background: rgba(29, 106, 154, 0.72);
    color: #fff;
    visibility: hidden;
    text-align: center;
    opacity: 0;
    display: table;
    height: 100%;
    width: 100%;
}
.grid-image {
    position: relative;
    overflow: hidden;
}
.grid-image img {
    display: block;
    overflow: hidden;
}
.grid-image:after {
    position: absolute;
    display: block;
    content:"";
    height: 100%;
    width: 100%;
    top: 0;
    left: 0;
}
h5 {
    display: table-cell;
    vertical-align: middle;
}
<div class="container-fluid">
    <div id="page">
        <!-- GRID ITEM -->
        <div class="item s1"> <a href="#">
                <div class="grid-image">
        <img         src="https://upload.wikimedia.org/wikipedia/commons/thumb/4/4e/Pleiades_large.jpg/1024px-Pleiades_large.jpg">
                </div>
                <div class="item-caption">
                    <h5>I want this to be center</h5>
                </div>
            </a>

        </div>
        <!-- /GRID ITEM -->
    </div>
</div>
</body>

</html>

您只需在 CSS 中添加一行代码即可完成这项工作:

h5 { 
    margin-top: 50%;
    transform: translateY(-50%); /* not entirely necessary, but makes centering precise */
}

演示:http://jsfiddle.net/2stywe2t/4/