1 回答

TA貢獻1852條經驗 獲得超7個贊
Bootstrap 正在這樣做:
.modal-dialog {
max-width: 500px;
}
該.modal-dialog元素是塊元素,因此默認情況下希望寬度為 100%,但 Bootstrap 的樣式將其寬度保持為 500px。
您明確將每張圖像的寬度設置為 200 像素,這(暫時忽略邊距)僅加起來為 400 像素。
兩個可能的修復方法是:
1. 在這里重寫 Bootstrap 的模態樣式,將模態限制為更窄的寬度:
/*
The value below is empirically tested,
allows for your given margins & floating. I originally expected it to be
420px = (200px width + 5px left margin + 5px right margin) * 2
but 422px was necessary to avoid wrapping when tested in jsfiddle
*/
.modal-dialog {
max-width: 422px;
}
https://jsfiddle.net/nftj9s7y/1/
如果圖像的寬度恰好為 200 像素很重要,那么這就是您的解決方案。然而,這對我來說似乎是一個脆弱的解決方案 - 如果您決定更改圖像寬度,它就會崩潰,并且可能無法在較小的屏幕尺寸下工作。
更靈活的解決方案是:
2.使用flexbox讓圖像擴展以填充模態框
.gallery {
display: flex;
flex-wrap: wrap;
padding: 5px;
}
/* now that the parent element is display: flex, we don't need floats anymore */
.gallery-item {
padding: 5px;
width: 50%;
}
.gallery-item img {
height: 150px;
object-fit: cover;
width: 100%; /* allow image to fill width of containing element */
}
https://jsfiddle.net/vzs98n6b/2/
最后一點,除非您在該元素上指定或,.gallery { overflow-y: auto }否則不會有任何效果。heightmax-height
- 1 回答
- 0 關注
- 183 瀏覽
添加回答
舉報