简体   繁体   English

在 div 内按比例适合图像 - 图像不应该被剪裁

[英]Fit an image proportionally inside a div - image should never be clipped

i've read a bunch of posts on fitting an image inside a div, but none solved my problem yet:我已经阅读了一堆关于在 div 中拟合图像的帖子,但还没有解决我的问题:

I have a responsive webdesign that sets the height of the container div to the browser window height, and then loads images into a flexslider.我有一个响应式网页设计,它将容器 div 的高度设置为浏览器窗口的高度,然后将图像加载到 flexslider 中。 Most of the images will be larger than the browser viewport.大多数图像将大于浏览器视口。

My problem is that I can make the photos fit the width correctly, but images never adjust the height properly.我的问题是我可以使照片正确适应宽度,但图像永远不会正确调整高度。 See http://stineheilmann.dk/portfolio/shoes/ (picture no. 3)http://stineheilmann.dk/portfolio/shoes/ (图3)

How do I ensure that the picture always stays inside the containing div, while maintaining the aspect ratio?如何确保图片始终保留在包含的 div 内,同时保持纵横比?

Are there any browser restriction?有浏览器限制吗? Are you looking for something like this demo ?你在寻找类似这个演示的东西吗?

CSS CSS

.frame{width:300px;height:300px;padding: 2px; border:1px solid #ccc;margin:5px;}
.frame img{max-width:100%;max-height:100%;}

HTML would be something like HTML 将类似于

<div class="frame">
    <img src="http://farm8.staticflickr.com/7251/8165766003_0e57a95b55.jpg"/>
</div>
<div class="frame">
    <img src="http://farm8.staticflickr.com/7251/8165766003_0e57a95b55.jpg"/>
</div>

Update: I have updated the demo with three images of different aspect ratios, that fit in the div without getting skewed or overflow out of the div.更新:我已经用三个不同纵横比的图像更新了演示,它们适合 div,而不会从 div 中倾斜或溢出。

Just add this CSS to the following selector:只需将此 CSS 添加到以下选择器:

.flexslider .slides img {
    height:100%;
    ....
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM