我有一张图片,我想给它设置一个特定的宽度和高度(像素)
但是如果我使用css (width:150px;高度:100px),图像将被拉伸,并且它可能是丑陋的。
如何使用CSS填充图像到特定的大小,而不是拉伸它?
填充和拉伸图像示例:
原始图像:
拉伸图片:
填充图片:
请注意,在上面的填充图像示例中:首先,图像被调整为150x255(保持长宽比),然后,它裁剪为150x100。
我有一张图片,我想给它设置一个特定的宽度和高度(像素)
但是如果我使用css (width:150px;高度:100px),图像将被拉伸,并且它可能是丑陋的。
如何使用CSS填充图像到特定的大小,而不是拉伸它?
填充和拉伸图像示例:
原始图像:
拉伸图片:
填充图片:
请注意,在上面的填充图像示例中:首先,图像被调整为150x255(保持长宽比),然后,它裁剪为150x100。
当前回答
基于@Dominic Green使用jQuery的回答,这里有一个解决方案,应该适用于图像宽度大于它们的高度或高于它们的宽度。
http://jsfiddle.net/grZLR/4/
可能有一种更优雅的方式来处理JavaScript,但这确实有效。
function myTest() {
var imgH = $("#my-img").height();
var imgW = $("#my-img").width();
if(imgW > imgH) {
$(".container img").css("height", "100%");
var conWidth = $(".container").width();
var imgWidth = $(".container img").width();
var gap = (imgWidth - conWidth)/2;
$(".container img").css("margin-left", -gap);
} else {
$(".container img").css("width", "100%");
var conHeight = $(".container").height();
var imgHeight = $(".container img").height();
var gap = (imgHeight - conHeight)/2;
$(".container img").css("margin-top", -gap);
}
}
myTest();
其他回答
基于@Dominic Green使用jQuery的回答,这里有一个解决方案,应该适用于图像宽度大于它们的高度或高于它们的宽度。
http://jsfiddle.net/grZLR/4/
可能有一种更优雅的方式来处理JavaScript,但这确实有效。
function myTest() {
var imgH = $("#my-img").height();
var imgW = $("#my-img").width();
if(imgW > imgH) {
$(".container img").css("height", "100%");
var conWidth = $(".container").width();
var imgWidth = $(".container img").width();
var gap = (imgWidth - conWidth)/2;
$(".container img").css("margin-left", -gap);
} else {
$(".container img").css("width", "100%");
var conHeight = $(".container").height();
var imgHeight = $(".container img").height();
var gap = (imgHeight - conHeight)/2;
$(".container img").css("margin-top", -gap);
}
}
myTest();
我帮助构建了一个名为Fillmore的jQuery插件,它在支持它的浏览器中处理背景大小:封面,并为那些不支持它的浏览器提供了一个垫片。看看吧!
解决方案不需要图像作为背景,将自动调整大小而不被切断或扭曲。
另一种解决方案是将图像放在具有所需宽度和高度的容器中。使用此方法,您不必将图像设置为元素的背景图像。
然后你可以用img标签来做这件事,只需要在元素上设置max-width和max-height。
CSS:
.imgContainer {
display: block;
width: 150px;
height: 100px;
}
.imgContainer img {
max-width: 100%;
max-height: 100%;
}
HTML:
<div class='imgContainer'>
<img src='imagesrc.jpg' />
</div>
现在,当你改变容器的大小时,图像将自动增长到最大,而不会超出边界或扭曲。
如果你想让图像垂直和水平居中,你可以改变容器css为:
.imgContainer {
display: table-cell;
width: 150px;
height: 100px;
text-align: center;
vertical-align: middle;
}
这是一个JS小提琴http://jsfiddle.net/9kUYC/2/
增强了@afonsoduarte接受的答案。 以防你使用自举
Providing width:100% on the style. This is helpful if you are using bootstrap and want the image to stretch all the available width. Specifying the height property is optional, You can remove/keep it as you need .cover { object-fit: cover; width: 100%; /*height: 300px; optional, you can remove it, but in my case it was good */ } By the way, there is NO need to provide the height and width attributes on the image element because they will be overridden by the style. so it is enough to write something like this. <img class="cover" src="url to img ..." />
据我所知,有一个插件可以让这变得简单。
jQuery插件:自动转换<img>为背景样式
<img class="fill" src="image.jpg" alt="Fill Image"></img>
<script>
$("img.fill").img2bg();
</script>
此外,这种方式也满足了可达性需求。因为这个插件不会从你的代码中删除你的<img>标签,屏幕阅读器仍然会告诉你ALT文本,而不是跳过它。