是否有一种纯CSS的方法可以将图像缩放到边界框中(保持长宽比)?如果图像大于容器,则可以使用此方法:
img {
max-width: 100%;
max-height: 100%;
}
示例:
- 用例1(正常工作):http://jsfiddle.net/Jp5AQ/2/
- 用例2(正常工作):http://jsfiddle.net/Jp5AQ/3/
但我想将图像缩放到某个维度以达到容器的100%。
- 用例3(不起作用):http://jsfiddle.net/Jp5AQ/4/
是否有一种纯CSS的方法可以将图像缩放到边界框中(保持长宽比)?如果图像大于容器,则可以使用此方法:
img {
max-width: 100%;
max-height: 100%;
}
示例:
但我想将图像缩放到某个维度以达到容器的100%。
CSS3提供了一种解决方案!
该解决方案是将图像设置为background-image
,然后将background-size
设置为contain
。
HTML
<div class='bounding-box'>
</div>
CSS
.bounding-box {
background-image: url(...);
background-repeat: no-repeat;
background-size: contain;
}
在此测试: http://www.w3schools.com/cssref/playit.asp?filename=playcss_background-size&preval=contain
与最新的浏览器完全兼容:http://caniuse.com/background-img-opts
要使div居中对齐,您可以使用以下变体:
.bounding-box {
background-image: url(...);
background-size: contain;
position: absolute;
background-position: center;
background-repeat: no-repeat;
height: 100%;
width: 100%;
}
背景大小:覆盖;
- arxpoetica<div class=image style="background-image: url('/images/foo.jpg');"></div>
。所有其他样式应该使用CSS应用。 - Andrey Mikhaylov - lolmausobject-fit
可能是一个更好的解决方案:https://developer.mozilla.org/en-US/docs/Web/CSS/object-fit
不,没有纯CSS的方法可以在两个方向上实现这一点。您可以添加.fillwidth {
min-width: 100%;
height: auto;
}
.fillheight {
min-height: 100%;
width: auto;
}
$('div').each(function() {
var fillClass = ($(this).height() > $(this).width())
? 'fillheight'
: 'fillwidth';
$(this).find('img').addClass(fillClass);
});
.fillwidth {
width: 100%;
height: auto;
}
.fillheight {
height: 100%;
width: auto;
}
div {
border: 1px solid black;
overflow: hidden;
}
.tower {
width: 100px;
height: 200px;
}
.trailer {
width: 200px;
height: 100px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="tower">
<img src="http://placekitten.com/150/150" />
</div>
<div class="trailer">
<img src="http://placekitten.com/150/150" />
</div>
这里是我发现的一个不太正式的解决方案:
#image {
max-width: 10%;
max-height: 10%;
transform: scale(10);
}
这将使图像放大十倍,但将其限制在最终大小的10%以内 - 从而将其限制在容器内。
与background-image
解决方案不同,这也适用于<video>
元素。
交互式示例:
function step(timestamp) {
var container = document.getElementById('container');
timestamp /= 1000;
container.style.left = (200 + 100 * Math.sin(timestamp * 1.0)) + 'px';
container.style.top = (200 + 100 * Math.sin(timestamp * 1.1)) + 'px';
container.style.width = (500 + 500 * Math.sin(timestamp * 1.2)) + 'px';
container.style.height = (500 + 500 * Math.sin(timestamp * 1.3)) + 'px';
window.requestAnimationFrame(step);
}
window.requestAnimationFrame(step);
#container {
outline: 1px solid black;
position: relative;
background-color: red;
}
#image {
display: block;
max-width: 10%;
max-height: 10%;
transform-origin: 0 0;
transform: scale(10);
}
<div id="container">
<img id="image" src="https://upload.wikimedia.org/wikipedia/en/7/7d/Lenna_%28test_image%29.png">
</div>
transform-origin: top left
以停止裁剪。/Zombie - XwipeoutX今天,只需要使用 object-fit:contain。支持所有浏览器除了IE:http://caniuse.com/#feat=object-fit
object-fit
的支持仍在开发中。 - BairDevobject-fit
的情况下工作:https://dev59.com/Vmkw5IYBdhLWcg3wSovA#46456673 - gabrielmaldihtml:
<div class="container">
<img class="flowerImg" src="flower.jpg">
</div>
CSS:
.container{
width: 100px;
height: 100px;
}
.flowerImg{
width: 100px;
height: 100px;
object-fit: cover;
/*object-fit: contain;
object-fit: scale-down;
object-position: -10% 0;
object-fit: none;
object-fit: fill;*/
}
object-fit: scale-down
或不使用都可):
figure {
margin: 0;
}
.container {
display: table-cell;
vertical-align: middle;
width: 80px;
height: 80px;
border: 1px solid #aaa;
}
.container_image {
display: block;
max-width: 100%;
max-height: 100%;
margin-left: auto;
margin-right: auto;
}
.container2_image2 {
width: 80px;
height: 80px;
object-fit: scale-down;
border: 1px solid #aaa;
}
Without `object-fit: scale-down`:
<br>
<br>
<figure class="container">
<img class="container_image" src="https://i.imgur.com/EQgexUd.jpg">
</figure>
<br>
<figure class="container">
<img class="container_image" src="https://i.imgur.com/ptO8pGi.jpg">
</figure>
<br> Using `object-fit: scale-down`:
<br>
<br>
<figure>
<img class="container2_image2" src="https://i.imgur.com/EQgexUd.jpg">
</figure>
<br>
<figure>
<img class="container2_image2" src="https://i.imgur.com/ptO8pGi.jpg">
</figure>
另一种解决方案,无需背景图片,也无需容器(尽管必须知道边界框的最大大小):
img{
max-height: 100px;
max-width: 100px;
width: auto; /* These two are added only for clarity, */
height: auto; /* as the default is auto anyway */
}
img {
max-height: 100%;
max-width: 100%;
width: auto; /* These two are added only for clarity, */
height: auto; /* as the default is auto anyway */
}
div.container {
width: 100px;
height: 100px;
}
对于这个问题,您可以有如下解决方案:
<table>
<tr>
<td>Lorem</td>
<td>Ipsum<br />dolor</td>
<td>
<div class="container"><img src="image5.png" /></div>
</td>
</tr>
</table>
$( window ).resize(function(){});
。function stretchImg(){
$('div').each(function() {
($(this).height() > $(this).find('img').height())
? $(this).find('img').removeClass('fillwidth').addClass('fillheight')
: '';
($(this).width() > $(this).find('img').width())
? $(this).find('img').removeClass('fillheight').addClass('fillwidth')
: '';
});
}
stretchImg();
$( window ).resize(function() {
strechImg();
});
这里有两个if条件。第一个条件不断检查图像高度是否小于div并应用.fillheight
类,而下一个条件检查宽度并应用.fillwidth
类。
在两种情况下,使用.removeClass()
删除另一个类。
以下是CSS样式:
.fillwidth {
width: 100%;
max-width: none;
height: auto;
}
.fillheight {
height: 100vh;
max-width: none;
width: auto;
}
如果你想在一个div中拉伸图片,可以用100%
代替100vh
。这个例子能让图片成比例地伸展以适应整个窗口。
你想要向上扩展,但不想向下缩小吗?
div {
border: solid 1px green;
width: 60px;
height: 70px;
}
div img {
width: 100%;
height: 100%;
min-height: 500px;
min-width: 500px;
outline: solid 1px red;
}
然而,这并不锁定宽高比。
首先是一些 CSS:
div.image-wrapper {
height: 230px; /* Suggestive number; pick your own height as desired */
position: relative;
overflow: hidden; /* This will do the magic */
width: 300px; /* Pick an appropriate width as desired, unless you already use a grid, in that case use 100% */
}
img {
width: 100%;
position: absolute;
left: 0;
top: 0;
height: auto;
}
HTML代码:
<div class="image-wrapper">
<img src="yourSource.jpg">
</div>