我正在按照以下解決方案給出的解決方案:如何疊加圖像上的文本,使用relative
和absolute
定位。問題是,與position: absolute
文本彈出其容器,去盡top
,right
等如何在容器中的圖像上疊加文本 - 並將文本保留在容器中
我很樂意使用背景圖片,但我需要技巧來獲得容器相匹配圖像的大小,我不知道如何讓它變得流暢。
看起來像一個非常簡單的問題,人們總是在尋找解決方案。也可以將文字疊加在不透明的圖像上,並需要使用:after
。希望有這些情況的直截了當的選擇。
.container {
margin: 0 10%;
}
.container img {
position: relative;
width: 100%;
}
#div1 .text {
position: absolute;
bottom: 0;
right: 0;
}
#div2 .text {
position: absolute;
top: 0;
left: 0;
}
<div id="div1" class="container">
<img src="http://placehold.it/800x200" />
<div class="text">
<h3>Top Image</h3>
<p>This text should be in the bottom right of the top image.</p>
</div>
</div>
<div><p>A bunch of miscellaneous text here.</p></div>
<div id="div2" class="container">
<img src="http://placehold.it/800x200" />
<div class="text">
<h3>Lower Image</h3>
<p>This should be in the top left of the lower image.</p>
</div>
</div>
臨屋非常好的解釋! – abalter