【发布时间】:2015-12-14 00:58:45
【问题描述】:
我正在为图片库制作一个简单的灯箱。问题是图像没有占据#wrapper div 的全部高度/宽度。
我尝试使用 max-width 和 max-height css 属性,但图像没有占用完整的宽度/高度。在小屏幕中,图像会相应地占据整个高度/宽度,但在大屏幕宽度中,它不会占据整个宽度/高度。
也尝试过width:100%; height:auto;,但如果图像的高度很大,它只会与#wrapper div 重叠。
codepen.io 上的演示。
html:
<div id="snap-modal">
<div id="wrapper">
<img src="http://placehold.it/450x450">
</div>
</div>
css:
#snap-modal {
width: 90%;
height: 90%;
outline: 1px solid lightgrey;
top: 0;
left: 0;
position: fixed;
}
#snap-modal #wrapper {
width: 100%;
height: 100%;
position: relative;
}
#snap-modal #wrapper img {
max-width: 100%;
max-height: 100%;
position: absolute;
top: 0;
right: 0;
left: 0;
bottom: 0;
margin: auto;
}
如何根据 div 内的图像大小(如果可能,在 css 中)使图像具有完整的高度/宽度,同时保持纵横比?谢谢。
【问题讨论】:
-
@yjs 抱歉,我想保留图片的纵横比。