【问题标题】:How do I vertically center caption on img hover?如何在 img 悬停时垂直居中标题?
【发布时间】:2015-11-13 11:24:00
【问题描述】:

我目前在悬停时无法将标题居中在 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

谢谢!

【问题讨论】:

标签: html css centering


【解决方案1】:

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

我做了什么:

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

有意义吗?

【讨论】:

  • 有趣。谢谢!只是一个问题..您通常什么时候使用表格单元格?
  • display: table 和 display: table-cell 用于模仿表的行为。所以这是一个明显的用例。根据我的经验,当您无法保证元素的尺寸时,它也适用于垂直对齐内容
  • 你好马丁,当我使用你帮助我的代码时.. 它不在 safari 浏览器中。有针对这个的解决方法吗?我试着环顾四周,但似乎找不到解决方案。 :s
【解决方案2】:

知道答案了!!稍微整理一下你的代码。

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>

【讨论】:

  • 与我的方法非常相似,但您将display:table-cell 直接添加到h5。智能,但可能不可扩展(例如,如果 OP 想要同时添加标题和描述)
  • 是的。你的方法更好。我不想在代码中添加额外的标记。 :)
  • 谢谢大家!你一直很有帮助!学到了很多!干杯!
【解决方案3】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-27
    • 2020-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-02
    相关资源
    最近更新 更多