2015-12-13 17 views
0

我试过在图像上用下面的代码显示文本,如下所示:在Bootstrap中使用Div显示文本图像

enter image description here

<div class="wrapper"> 
<img src="https://cdn0.vox- cdn.com/thumbor/qLH7593e3D2SSSIdkwgBWdYhjjk=/289x0:802x684/400x533/filters:forma t(webp)/cdn0.vox-cdn.com/uploads/chorus_image/image/47859707/6621139883_e6be6a217a_b__1_.0.0.jpg" alt="" /> 
<div class="overlay"> 
    <h2>Restaurant Stowaway Devours $800 of Roast Goose and Wine</h2> 

</div> 
</div> 

CSS

body { 
    text-align: center; 
} 

.wrapper { 
    display: inline-block; 
    margin-top: 25px; 
    position: relative; 
} 

.wrapper img { 
    display: block; 
    max-width:100%; 
} 

.wrapper .overlay { 
    position: absolute; 
    top:0; 
    left:0; 
    width:100%; 
    height:100%; 
    background-color: rgba(0,0,0,0.25); 
    color:white; 
} 

但它出来是这样的: enter image description here

请帮我,你有什么我错过了?因为我希望输出像上面显示的第一个图像。

+1

创建一个div元素,其中有该图像作为背景!之后,在另一个div框中添加文本。尝试调整宽度并将其移动到底部。 –

回答

2

您必须将类添加到您的标题h2标记并向其添加以下样式。 添加以下类,

<h2 class="header">Restaurant Stowaway Devours $800 of Roast Goose and Wine</h2> 

h2.header { 
    bottom: 0; 
    position: absolute; 
    text-align: center; 
    width: 100%; 
} 
+0

它的作品谢谢你。 –