0
我使用圖像wrapper將自舉網格中的所有圖像製作爲相同高度,然後通過讓它們的寬度獨立縮放(請參閱下面的代碼段)來保留它們的原始縱橫比。現在我想將它們水平放置在它們的div
s中,以使佈局更整潔。我嘗試使用下面的image-container
類,但它在我應用它的各種div
中沒有工作(目前只是讓它們消失)。有沒有辦法使用包裝屬性來水平居中圖像?我會很感激任何有用的推動。謝謝!將縮放到相同高度的響應圖像置於中心
我正在尋找一個成功的方法:
- 保持響應擴展到具有不同縱橫比相同的高度(如以下所述摘錄)圖像
- 水平居中的圖像同時保留其響應的縮放比例(上面的點)
請參閱下面的代碼片段,瞭解如何將圖像響應地縮放到相同的高度;註釋掉image-container
div是我在縮放後水平居中圖像的嘗試之一。
.thumbnail img{
max-width: 100%; /* do not stretch the bootstrap column */
}
.img-wrapper {
position: relative;
padding-bottom: 130%;
overflow: hidden;
width: 100%;
}
.img-wrapper img {
position: absolute;
top:0;
left:0;
width:auto;
height:100%;
}
.image-container {
display: flex;
justify-content: center;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<div class="row">
<div class="col-xs-12 col-sm-4 col-md-4 col-lg-4">
<!--<div class="image-container">-->
<div class="thumbnail">
<div class="img-wrapper">
<img src="http://via.placeholder.com/150x300"/>
</div>
</div>
<!--</div>-->
</div>
<div class="col-xs-12 col-sm-4 col-md-4 col-lg-4">
<!--<div class="image-container">-->
<div class="thumbnail">
<div class="img-wrapper">
<img src="http://via.placeholder.com/200x350"/>
</div>
</div>
<!--</div>-->
</div>
<div class="col-xs-12 col-sm-4 col-md-4 col-lg-4">
<!--<div class="image-container">-->
<div class="thumbnail">
<div class="img-wrapper">
<img src="http://via.placeholder.com/150x300"/>
</div>
</div>
<!--</div>-->
</div>
</div>
感謝您的快速響應!如你所說,我拿出了絕對的定位,但它似乎打破了圖像縮放,所有圖像變得異常伸展,失去了獨立的寬度。有什麼想法嗎? – gromiczek
嗯。從「padding-button:130%'(我不確定你爲什麼,但我已經離開)添加了高度,但除此之外,這些圖像對我來說似乎是正確的。我上面例子中的三張圖片有不同的寬度,我所做的只是刪除'position:absolute'。您可以添加一個問題的屏幕截圖,或者確保您已經創建了一個[**最小,完整和可驗證的示例**](http://stackoverflow.com/help/mcve)?圖片在我的回答中顯示的方式是您要查找的內容?如果沒有,請您正確地澄清*需要改變什麼? :) –
我會努力使上面的代碼成爲一個片段。同時,你的例子中的圖像是不同的高度。包裝是爲了將圖像重新縮放到相同的高度。拿出絕對定位似乎打破了這一點。 – gromiczek