CSS 图像缩放以适应区域而不扭曲
有没有办法使用 CSS 或其他方式使图像适合某个区域。假设我有多个不同尺寸的图像,我希望它们全部适合在 150 像素 x 100 像素的 div 内。我不想缩放图像,因为有些图像可能很高,有些可能很窄,我只是希望它们适合该区域,而其余部分则隐藏。
我考虑过使用 overflow:hidden
但它似乎在 IE6 中没有隐藏。
有什么想法吗?
Is there a way with CSS or otherwise of making an image fit within an area. Lets say I have multiple images of different sizes and I want them all to fit within a div of 150px by 100px. I don't want to scale the images though as some may be tall and others narrow I simply want them to fit within this area with the rest hidden.
I thought about using overflow:hidden
but it appears to not be hidden in IE6.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
你应该尝试使用这个:
编辑:看起来 IE6 不支持 max-width 和 max-height 属性。但是,您可以实施此处给出的解决方法: IE6 的最大宽度、最大高度
摘录(以防链接的文章停止工作):
You should try using this:
Edit: Looks like IE6 doesn't support max-width and max-height properties. However, you can implement the workaround given here: max-width, max-height for IE6
Excerpt (in case linked article stops working):
当你说“适合这个区域”而其余部分隐藏时,我觉得你希望图像根本不按比例缩小,并且基本上剪掉任何多余的部分。
我可能会解释你的问题是错误的,但试试这个,看看它是否能产生你想要的效果。
When you say "fit within this area" with the rest hidden I feel like you want the image to not be scaled down at all and basically crop off any excess.
I might be interpreting you're question wrong, but try this and see if it produces the effect you're looking for.
我知道这是一个旧问题,但因为我发现它是为了寻找同一问题的答案,所以我想它也对其他人有用。
自从答案发布后,CSS 属性 object-fit 就被带到了我们身边。它完全符合问题中曾经要求的内容。
供参考:https://www.w3schools.com/css/css3_object-fit.asp< /a>
I know this is an old one, but because I found it in search of answer for the same question, I guess it could be of use for someone else, too.
Since the answers were posted, CSS property object-fit was brought to us. It does exactly what was once requested in the question.
For reference: https://www.w3schools.com/css/css3_object-fit.asp
这在 IE6 中不起作用(按照 OP 的要求),但为了完整起见,您可以使用 CSS3 的
background-size:cover
并将图像设置为居中背景图像在较新的浏览器上实现所需的效果。就像这样:This won't work in IE6 (as required by the OP), but for completeness you can achieve the required effect on newer browsers using CSS3's
background-size:cover
and setting the image as a centered background image. Like so:这对我有用:
它试图对容器进行“完美适配”,在保持图像比例的同时拉伸自身以适应边界。没有用IE6测试过。
JSFiddle: http://jsfiddle.net/4zudggou/
This worked for me:
It tries to do a "perfect fit" of the container, stretching itself to fit the bounds while maintaining image proportion. Haven't tested it with IE6.
JSFiddle: http://jsfiddle.net/4zudggou/
希望我没有迟到;)
如果您希望显示完整图像,请使用下面的代码
应该可以解决问题。
Hope I am not late to the party ;)
if however you want the full image to display, use the code below
this should do the trick.