简体   繁体   English

仅限CSS的max-height和max-width

[英]max-height AND max-width with CSS only

First post for me here. 我在这里的第一篇文章。

I'm using a div to crop thumbnail images all in the same proportions (180wx170h). 我正在使用div以相同的比例裁剪缩略图(180wx170h)。 I'm getting stuck when dealing with portrait as well as landscape images. 在处理肖像和风景图像时,我会陷入困境。 If am using this which is fine for portrait style images: 如果我使用这个肖像风格的图像很好:

.crop img {max-height:170px; width:auto} 

.. and is fine for landscape style images: ..并且适用于风景图像:

.crop img {max-width:180px; height: auto;} is fine for landscape style images.  

So I basically want to crop the sides if landscape and top/bottom if portrait. 所以我基本上想要如果横向和顶部/底部如果肖像裁剪侧面。 Kind of like a prioritized max-height and max-width. 有点像优先最大高度和最大宽度。

I know this could be done easily with PHP but I really only know CSS so that would be my first preference. 我知道这可以通过PHP轻松完成,但我真的只知道CSS,这将是我的第一选择。

I need to maintain the aspect ratio of the image. 我需要保持图像的宽高比。

the solutions after going through loads of other 'solutions' 经过其他“解决方案”后的解决方案

max-width:100%;
max-height:100%;
height: auto;
width:auto;

Edit 2019: 编辑2019:

If you want to keep <img> tags, please look into object-fit css property, support of it across browsers is quite good. 如果你想保留<img>标签,请查看object-fit css属性,跨浏览器支持它是相当不错的。

And if you want to keep aspect ratio on width change, try padding-hack . 如果你想保持宽高比的宽高比,试试padding-hack


As I understand you have blocks 180x170 px and you want to fill them completely with images. 据我所知,你有块180x170像素,你想完全用图像填充它们。 Try to move images to background and use background-size:cover . 尝试将图像移动到背景并使用background-size:cover

Demo http://jsfiddle.net/heuku/1/ 演示http://jsfiddle.net/heuku/1/

<div style="background-image:url(http://placekitten.com/100/200)"></div>
<div style="background-image:url(http://placekitten.com/200/100)"></div>
<div style="background-image:url(http://placekitten.com/200/200)"></div>

div {
  width:180px;
  height:170px;
  background-repeat:no-repeat;
  background-size:cover;
}

Note that this solution not working in IE8 and below. 请注意,此解决方案不适用于IE8及更低版本。

Edit: I have improved the solution, see here: https://stackoverflow.com/a/34774905/1663572 编辑:我已经改进了解决方案,请看这里: https//stackoverflow.com/a/34774905/1663572


I'm using this solution, which i found, when I was trying to fit and center images into squares (or whatever). 我正在使用这个解决方案,当我试图将图像放入正方形(或其他任何东西)时,我发现了这个解决方案。 It is brilliant in combination, where you set padding-bottom and height 0 to its container - that makes it responsive with fixed ratio. 它的组合非常出色,您可以在其容器中设置填充底部和高度0,这使其具有固定比率的响应。

Works in IE9 and higher. 适用于IE9及更高版本。 For IE8 and below some CSS hacks needed. 对于IE8及以下,需要一些CSS黑客攻击。

HTML HTML

.container {
    height: 0;
    padding-bottom: 100%; /* Or 75% for 4:3 aspect ratio */
    position: relative;
    overflow: hidden;
}
.container img {
    display: inline-block;
    max-width: 90%; /* You can use different numbers for max-width and max-height! */
    max-height: 75%;
    width: auto;
    height: auto;

    position: absolute;
    left: 50%; /* This sets left top corner of the image to the center of the block... */
    top: 50%; /* This can be set also to bottom: 0; for aligning image to bottom line. Only translateX is used then. */
    -webkit-transform: translate(-50%,-50%); /* ...and this moves the image 50 % of its width and height back. */
    -ms-transform: translate(-50%,-50%);
    -o-transform: translate(-50%,-50%);
    transform: translate(-50%,-50%);
}

/* Fallback for IE8 */
@media all\0 { 
    .container img {
        margin: auto;
        left: 0;
        right: 0;
        top: 0;
        bottom: 0;
    }
}

Try it here: http://jsfiddle.net/thiemeljiri/uhdm4fce/4/ 在这里试试: http//jsfiddle.net/thiemeljiri/uhdm4fce/4/

Notice: If using bootstrap change the class .container to something else. 注意:如果使用bootstrap,请将类.container更改为其他内容。

You could try 你可以试试

.crop img {max-height:170px; max-width:180px;}

Since max-height and max-width are maxima, it should work. 由于max-heightmax-width最大值,因此应该可以工作。 The browser will make the image as big as possible, without going over your dimensions. 浏览器将使图像尽可能大,而不会超出您的尺寸。

Note that this is untested, but based on this W3Schools page . 请注意,这是未经测试的,但基于此W3Schools页面

Hope this helps!! 希望这可以帮助!!

You have the answer in you! 你有答案!

Try: 尝试:

.crop img {max-height: 170px; max-width: 180px;}

这可能有点晚了,但我发现在<figure>包装所述图像会缩放保持纵横比的图像。

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

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