2 回答

TA貢獻1820條經驗 獲得超2個贊
更新您的代碼,如下所示:
.container {
width: 80%;
height: 48vw;
/* 4:3 */
margin: 5px auto;
text-align: center;
}
.img-wrap {
background-color: #efe;
height: 100%;
display: inline-block;
position: relative;
}
img {
max-width: 100%;
height: 100%;
display: block;
object-fit: contain; /*or cover if you want to cover all the area*/
object-position: bottom;
}
.caption {
position: absolute;
left: 0;
right: 0;
bottom: 0;
color: #fff;
background-color: rgba(153, 153, 153, 0.541)
}
<div class="container">
<div class="img-wrap">
<img src="https://i.picsum.photos/id/10/400/600.jpg">
<div class="caption">Caption Content</div>
</div>
</div>
<div class="container">
<div class="img-wrap">
<img src="https://i.picsum.photos/id/10/600/300.jpg">
<div class="caption">Caption Content</div>
</div>
</div>

TA貢獻2021條經驗 獲得超8個贊
不要position absolute
圖像,
另外,如果標題是圖像的同級,
設置父級的大小,如果父級的寬度和高度將圖像設置為 100%
然后你可以簡單地使用text-align: center
標題上的 將其居中。
編輯 :
保留現有的標題樣式以進行定位
小提琴: https: //jsfiddle.net/hellooutlook/6ep4Lofz/4/
- 2 回答
- 0 關注
- 201 瀏覽
添加回答
舉報