在这个小提琴看看:http://jsfiddle.net/5mneypc5/IMG填补其容器空间,而不拉伸
它包含以下代码:
<style type="text/css">
.container {
width: 400px;
height: 200px;
overflow: hidden;
background: grey;
margin-bottom: 10px;
}
#c-width {
width: 350px;
}
#c-height {
height: 150px;
}
.container > img {
max-width: 100%;
max-height: 100%;
}
</style>
<div class="container" id="c-width">
<img src="http://lorempixel.com/500/250/cats/5">
</div>
<div class="container" id="c-height">
<img src="http://lorempixel.com/500/250/cats/5">
</div>
是否有可能使它充满整个容器空间,而不被拉长?裁剪是允许的。
我试过min-height, max-width
和max-height, min-width
的情况,但每个只适用于我的一个容器。
纯CSS是优选的;但是,JavaScript/jQuery解决方案也受到欢迎。
请注意,图像大小已知(预定义),但容器大小基于vie wport大小。 – mrdaliri 2014-12-19 04:19:09