简体   繁体   English

图像变形并且不保持宽高比-我需要纯CSS解决方案

[英]Images Distored and not staying in aspect ratio - I need pure CSS solution

I'm trying to get all of these different size images to remain undistorted. 我试图让所有这些不同大小的图像保持不失真。 All the images look great except for the ones that have a smaller width than the container of 285px. 除了宽度小于285px的容器之外,所有图像看起来都很棒。 I'm ok with the images being blurry because I know we are stretching them. 我对图像模糊没有问题,因为我知道我们正在拉伸它们。 I just want all of them to keep their aspect ratios. 我只希望它们所有人都保持其纵横比。 The ones that are not working are images #2, #3, and #4. 无法使用的是图像#2,#3和#4。

I can't use any JavaScript for this. 我不能为此使用任何JavaScript。 It needs to be pure css. 它必须是纯CSS。

http://jsfiddle.net/pp74fb7b/9/ http://jsfiddle.net/pp74fb7b/9/

.squaregallerywrap {
width:  285px; 
height: 285px;
}

.squaregallerywrap img {
min-width: 100%;
min-height: 100%;
max-height: 286px;
min-width: 286px;
display:block;
margin:auto;
position:relative;
-moz-transform:translateX(-50%);
-webkit-transform:translateX(-50%);
-ms-transform:translateX(-50%);
-o-transform:translateX(-50%);
transform:translateX(-50%);
left:50%;
}

li {    
float: left;
overflow: hidden;
-webkit-transition: 0.4s all linear;
transition: 0.4s all linear;
}

How about using background-image on the div instead? 如何在div上使用background-image呢? Sizing the image will be much easier that way. 调整图像大小将更容易。 I made you a fiddle with square images using background-size: cover; 我用background-size: cover;了方形图像,使您无所适从 background-size: cover; , which crops them into squares but doesn't distort the images. ,将它们裁剪为正方形,但不会扭曲图像。 Here's also another fiddle , where they maintain their aspect ratios and don't crop using background-size: contain; 这也是另一个小提琴 ,它们保持纵横比并且不使用background-size: contain;裁剪background-size: contain; .

Is this what you want: jsfiddle 这就是您想要的: jsfiddle

If I understand you correctly, you want to maintain images from being undistorted but yet make all of them the same size container, set an explicit width and height on the img tag, and use object-fit: cover which acts like background-size: cover . 如果我对您的理解正确,则希望避免图像失真,但要使它们全部成为相同大小的容器,在img标签上设置明确的widthheight ,并使用object-fit: cover ,其作用类似于background-size: cover Though it isn't supported in IE. 尽管IE不支持它。

See http://caniuse.com/#feat=object-fit 参见http://caniuse.com/#feat=object-fit

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

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