简体   繁体   English

图像在移动设备上无法正确缩放,但在响应式窗口中可以

[英]Image doesn't scale properly on mobile, but does in responsive window

I have a webpage containing several images in a column.我有一个网页,在一栏中包含多个图像。 During debugging I have now placed only one image, the problem persist.在调试过程中,我现在只放置了一张图片,问题仍然存在。 The problem is, when I use the Chrome Device toolbar, or try to resize the window to very small, the image scales correctly:问题是,当我使用 Chrome 设备工具栏或尝试将窗口大小调整为非常小时时,图像会正确缩放:

Chrome iPhone 模拟

调整 Chrome 窗口的大小

But when I access the page on an actual mobile device, it gets deformed.但是当我在实际的移动设备上访问页面时,它会变形。

实际 iPhone 8 屏幕截图

I tried iPhone 7 and 8 and an iPad, chrome and safari on all of them, the problem is the same, the image is always stretched vertically, regardless of what image I use.我尝试了 iPhone 7 和 8 以及 iPad、chrome 和 safari,问题是一样的,无论我使用什么图像,图像总是垂直拉伸。

My code at the moment:我目前的代码:

<div class="container contentContainer justify-content-center">
    <div class="row justify-content-center mt-5 mb-1">
        <img src="..." class="fileImage">
    </div>
</div>

And CSS:和 CSS:

.contentContainer{
height:100%;
margin-top:-56px
padding-top:61px;
padding-bottom:5px;
text-align:center;
}
.fileImage{
display:block; //already an attempt to fix, no effect
height:auto; //same as above
width:100%;
}

Any ideas on what causes this and how to fix it are greatly appreciated.非常感谢有关导致此问题的原因以及如何解决此问题的任何想法。

UPDATE更新

I managed to get the image scaled correctly using object-fit:contain .我设法使用object-fit:contain正确缩放图像。 When set the cover, it was scaled correctly, but went offscreen no matter what size I tried to set.设置封面时,它被正确缩放,但无论我尝试设置什么尺寸,它都会离开屏幕。 There is an issue with this as well.这也有一个问题。 I have set a border, to illustrate.我已经设置了一个边界,以说明。 My CSS:我的CSS:

.fileImage{
object-fit: contain;
object-position: center;
box-sizing:content-box; //again, just an attempt to fix current issue, no effect...
max-width:100%;
border:1px solid red;
}

Result:结果: 对象匹配后

SOLVED So, after like 2 hours of fiddling with different CSS for sizing etc., this CSS and removing the row element worked.解决了所以,在摆弄不同的 CSS 以调整大小等 2 个小时之后,这个 CSS 和删除行元素起作用了。 I'm not quite sure why, but it did.我不太确定为什么,但确实如此。

<div class="container contentContainer justify-content-center">
        <img src="..." class="fileImage mt-5 mb-1">
</div>

CSS: CSS:

.fileImage{
object-fit: contain;
max-width:100%;
}

And works as expected, scales on mobile the same way as in desktop chrome, as per screenshot in original question.并且按预期工作,在移动设备上的缩放方式与桌面 chrome 中的缩放方式相同,根据原始问题中的屏幕截图。 Hope this helps anyone!希望这可以帮助任何人!

Could be an issue with the meta.可能是元的问题。 Have you defined viewport meta tags in your head tag?您是否在 head 标签中定义了视口元标签?

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

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