简体   繁体   English

如何调整html中的图像大小以适应屏幕大小?

[英]How to resize images in html to fit screen size?

I'm trying to load an image using HTML and resize it (maybe by using CSS?) to fit my screen size using WKWebView.我正在尝试使用 HTML 加载图像并使用 WKWebView 调整它的大小(也许使用 CSS?)以适应我的屏幕大小。

I'm able to adjust the sizes manually, but I'd like it to be dynamic for different devices.我可以手动调整大小,但我希望它对于不同的设备是动态的。

I create my file in HTML, then load the image using an img tag.我在 HTML 中创建我的文件,然后使用 img 标签加载图像。 The CSS file is a separate file. CSS 文件是一个单独的文件。 To load the image using Swift and xcode, I use Webkit and load it from a local URL.要使用 Swift 和 xcode 加载图像,我使用 Webkit 并从本地 URL 加载它。

Everything loads just fine.一切都加载得很好。 I can edit my CSS file and the changes occur on my phone when testing.我可以编辑我的 CSS 文件,测试时我的手机上会发生变化。 The only thing I can't figure out is how to resize the image in the HTML file.我唯一想不通的是如何调整 HTML 文件中的图像大小。 Normally, if the image is loaded as a UIImage I can set the size to view.bound.size.width (or something similar depending what I want).通常,如果图像作为 UIImage 加载,我可以将大小设置为 view.bound.size.width (或类似的东西,取决于我想要的)。 However, I'm not sure how to get those dimensions INSIDE the HTML file.但是,我不确定如何在 HTML 文件中获取这些尺寸。 Or maybe there is another way I'm not sure about?或者也许还有另一种我不确定的方式?

To scale images with browser size you will want to make sure these images are not set to a fixed measurement.要使用浏览器大小缩放图像,您需要确保这些图像未设置为固定尺寸。

Meaning you would use vh , vw , em , % , vmin , vmax , rem这意味着您将使用vhvwem%vminvmaxrem

etc etc.等等等等

This would solely depend on how your page is marked up, and which media queries you have in place if any这完全取决于您的页面是如何标记的,以及您有哪些媒体查询(如果有的话)

Some people set font-size with their media queries.有些人使用他们的媒体查询设置font-size

When this is done they would use em as a basis of measurement to make their page responsive.完成后,他们将使用em作为衡量的基础,以使他们的页面具有响应性。

It all depends on how the page is utilizing certain measurements这完全取决于页面如何使用某些度量

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

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