2016-07-28 54 views
1

我想以文字中心覆盖图像为中心。这里是我的代码是什么:以中心为中心覆盖图像的文字

<div class="col-md-6"> 
    <div class="safaripreview"> 
     <img src="img/safari1.png" class="img-responsive" alt=""> 
     <h3>Our String Of Pearls</h3> 
    </div> 
</div> 

和我的CSS是目前

.safaripreview 
{ 
    position: relative; 
} 

.safaripreview h3 
{ 
    position: absolute; 
    bottom: 0; 
    width: 100%; 
    color: white; 
    padding: 10px; 
    font-family: 'Montserrat', sans-serif; 
    font-weight: 700; 
} 

本步骤是:

enter image description here

当我想要实现这个

enter image description here

有没有更好的方法来实现这个目标? Flexbox的?

+0

你可以使用图片作为背景,这样你就不需要文本的绝对位置。 –

回答

1

是。 Flexbox的

.safaripreview { 
 
    display: inline-block; 
 
    position: relative; 
 
} 
 
.safaripreview { 
 
    display: inline-block; 
 
} 
 
.safaripreview h3 { 
 
    position: absolute; 
 
    top: 0; 
 
    left: 0; 
 
    right: 0; 
 
    bottom: 0; 
 
    display: flex; 
 
    justify-content: center; 
 
    align-items: center; 
 
    color: white; 
 
}
<div class="safaripreview"> 
 
    <img src="http://www.fillmurray.com/460/300" class="img-responsive" alt=""> 
 
    <h3>Our String Of Pearls</h3> 
 
</div>

+0

刚刚添加了这个,完美的作品。谢谢。 –

0
position: absolute; 
top: 50%; 
right: 30px; 
left: 30px; 

transform: translateY(-50%); 

将为您达到此目的。

你也可以这样做:

position: absolute; 
top: 0; 
right: 0; 
bottom: 0; 
left: 0; 

align-items: center; 
justify-content: center; 

display: flex; 

但是,这不太支持,显然更行:)

+0

我补充说,它得到了这个: http://s32.postimg.org/5cxak69v9/screenshot_212.png 看起来两轴偏离中心 –

+0

第二个工作完美! –

0

改变CSS这样

.safaripreview h3 
{ 
    position: absolute; 
    top: 50%; 
    left:50%; 
    transform: translate(-50%, -50%); 
    width: 100%; 
    color: white; 
    padding: 10px; 
    font-family: 'Montserrat', sans-serif; 
    font-weight: 700; 
}