如何確保容器內的img
能夠集中並正確地從移動設備擴展到桌面?這是一個demo on Codepen。縮放到移動,以便更好地理解問題用響應<img>填充div並集中位置
HTML
<div class="image-container">
<img src="https://placeimg.com/470/310/people" alt="" />
</div>
CSS
.image-container{
width: 100%;
height: 500px;
max-height: 500px;
background: red;
overflow: hidden;
text-align: center;
position: relative;
display: block;
}
img{
height: auto;
width: 100%;
max-width: 100%;
position: absolute;
top: -50%;
left: 0;
}
我問這個,因爲像失去了它的高度上移動,看起來不正確。我有點像這樣工作,像`background-size:cover。我想要圖像完全填充容器
說實話,我是相當新了flexbox。我在波旁網格中使用了你的代碼,但它不起作用。 – samsos