2015-11-28 20 views
2

当图像悬停时,我有图像和显示在图像中心的文字。图像上的垂直居中文本 - 动态

HTML看起来像这样:

<article> 
<div class="entry-content"> 
<a href="http://www.linktopost.com"> 
<h3 class="entry-title">Ring #1</h3> 
<img width="620" height="387" src="http://i.telegraph.co.uk/multimedia/archive/02725/scotch-whisky_2725818b.jpg" class="aligncenter" alt="Platzhalter_3"> 
</a> 
</div> 
</article> 

CSS:

article { 
    float:left; 
    width:30%; 
    display:block; 
} 

.entry-content { 
    width: 620px; 
    margin: 0 auto; 
    position: relative; 
    height: 387px; 
} 

.entry-content:hover .entry-title { 
    color: #000; 
    display: table-cell; 
    background-color: #fff; 
    opacity: 0.75; 
} 

.entry-title { 
    position: absolute; 
    width: 100%; 
    height: 100%; 
    z-index: 9999; 
    line-height: 387px; 
    text-align: center; 
    display: none; 
} 

article img { 
    position:absolute; 
} 

你可以在这里看到: http://codepen.io/anon/pen/rOXOez

是否有机会在CSS不使用固定的像素值 - 使悬停效果有效并适用于任何图片?在这个例子中,我不得不使用CSS中图片的宽度和高度来实现我想要的。

谢谢!

+0

你可以使用flex ...这是最好的现代方式来做到这一点;)谷歌flex对齐中间 – Akxe

回答

2

解决方案1:

相对/绝对定位和中心与transform: translate()

https://jsfiddle.net/94efk8kz/

article { 
    position: relative; 
} 

.hover-content { 
    position: absolute; 
    width: 100%; 
    height: 100%; 
    top: 0; 
    left: 0; 
    right: 0; 
    bottom: 0; 
    background: white; 
    color: black; 
    opacity: 0; 
    -webkit-transition: all 0.3s ease-in-out; 
    -moz-transition: all 0.3s ease-in-out; 
    -o-transition: all 0.3s ease-in-out; 
    transition: all 0.3s ease-in-out; 
} 

.hover-content h3 { 
    position: absolute; 
    top: 50%; 
    left: 50%; 
    margin: 0; 
    -webkit-transform: translate(-50%, -50%); 
    -moz-transform: translate(-50%, -50%); 
    -ms-transform: translate(-50%, -50%); 
    -o-transform: translate(-50%, -50%); 
    transform: translate(-50%, -50%); 
} 

溶液2

Flexbox的

https://jsfiddle.net/94efk8kz/1/

article { 
    position: relative; 
} 

img { 
    width: 100%; 
} 

.hover-content { 
    width: 100%; 
    height: 100%; 
    top: 0; 
    display: flex; 
    position: absolute; 
    align-items: center; 
    justify-content: center; 
    background: white; 
    color: black; 
    opacity: 0; 
    -webkit-transition: all 0.3s ease-in-out; 
    -moz-transition: all 0.3s ease-in-out; 
    -o-transition: all 0.3s ease-in-out; 
    transition: all 0.3s ease-in-out; 
} 

解决方案3个

CSS表

https://jsfiddle.net/94efk8kz/2/

.entry-content a { 
    float: left; 
    width:30%; 
    position: relative; 
    height: 100%; 
} 


.hover-content { 
    width: 100%; 
    height: 100%; 
    top: 0; 
    left: 0; 
    position: absolute; 
    background: white; 
    color: black; 
    opacity: 0; 
    bottom: 0; 
    right: 0; 
    -webkit-transition: all 0.3s ease-in-out; 
    -moz-transition: all 0.3s ease-in-out; 
    -o-transition: all 0.3s ease-in-out; 
    transition: all 0.3s ease-in-out; 
} 


.v-align { 
    display: table; 
    height: 100%; 
    margin: 0 auto; 
} 

.hover-content h3 { 
    display: table-cell; 
    vertical-align: middle; 
} 
+1

这是真正的最佳解决方案,因为你使用'transform:translate()'。问题:你没有解释你的答案,所以OP不知道你在说什么。第二个问题是兼容性。使用您的代码,在iOS中不起作用,或者在IE9/10等旧版浏览器中运行。你需要添加'-webkit-transform'和'-ms-transform'。 –

0

这些变化尝试

.entry-content { 
    width: initial; 
} 
.entry-content:hover .entry-title { 
    margin: 0; 
} 
.article img { 
    display: block; 
    /* position: absolute; remove this,avoid using positon absolute where you can */ 

    width: 100%; 
}