在哪里指定图像尺寸以实现最快渲染:在 HTML 中还是在 CSS 中?

发布于 2024-08-15 12:48:35 字数 335 浏览 3 评论 0原文

我了解到明确指定图像尺寸是最佳实践。然后,浏览器可以在仍然下载图像本身的同时布局页面,从而缩短(感知的)页面渲染时间。

这是真的吗?如果是这样,在 HTML 或 CSS 中指定尺寸是否有区别?

  • HTML:
  • 内联 CSS:
  • 外部 CSS: #myImage { width: 200px;高度:200px; }

I've learned that it is a best practice to explicitly specify image dimensions. The browser can then already layout the page while still downloading the images themselves, thereby improving (percieved) page rendering time.

Is this true? And if so, is there a difference in specifying the dimensions in either HTML or CSS?

  • HTML: <img src="" width="200" height="100">
  • Inline CSS: <img src="" style="width: 200px; height: 100px">
  • External CSS: #myImage { width: 200px; height: 200px; }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

酒与心事 2024-08-22 12:48:35

根据 Google Page Speed,这并不重要如果您通过 CSS 或 HTML 指定尺寸,只要您的 CSS 面向 IMG 标签本身而不是父元素:

当浏览器布局页面时,它需要能够围绕可替换元素(例如图像)流动。它甚至可以在下载图像之前开始渲染页面,前提是它知道包裹不可替换元素的尺寸。如果包含的文档中未指定尺寸,或者指定的尺寸与实际图像的尺寸不匹配,则下载图像后浏览器将要求回流并重新绘制。为了防止重排,请在 HTML 标记或 CSS 中指定所有图像的宽度和高度。

但是,请注意,他们建议不要使用这些尺寸调整图像大小,即始终使用实际尺寸:

不要使用宽度和高度规范来动态缩放图像。如果图像文件实际上为 60 x 60 像素,请勿在 HTML 或 CSS 中将尺寸设置为 30 x 30。如果图像需要更小,请在图像编辑器中缩放并设置其尺寸以匹配(有关详细信息,请参阅优化图像。)

According to Google Page Speed, it does not really matter if you specify the dimensions via CSS or HTML, as long as your CSS targets the IMG tag itself and not a parent element :

When the browser lays out the page, it needs to be able to flow around replaceable elements such as images. It can begin to render a page even before images are downloaded, provided that it knows the dimensions to wrap non-replaceable elements around. If no dimensions are specified in the containing document, or if the dimensions specified don't match those of the actual images, the browser will require a reflow and repaint once the images are downloaded. To prevent reflows, specify the width and height of all images, either in the HTML tag, or in CSS.

However, note that they advise not to resize the image using these dimensions, ie to always use the real dimensions :

Don't use width and height specifications to scale images on the fly. If an image file is actually 60 x 60 pixels, don't set the dimensions to 30 x 30 in the HTML or CSS. If the image needs to be smaller, scale it in an image editor and set its dimensions to match (see Optimize images for details.)

后知后觉 2024-08-22 12:48:35

我倾向于在 CSS 中做到这一点。当有多个具有相同尺寸的图像时,这当然是一个胜利(因此您可以执行诸如 .comment img.usergroup { width: 16px; height: 16px; } 之类的操作),并且具有相同的图像可以在不同的样式表中进行缩放,例如用户可选择的主题。

当您拥有仅在网站上使用一次的完全独立的图像时,将它们的大小抽象为 CSS 并没有真正意义,因此 HTML 版本可能更合适。

I tend to do it in the CSS. This is certainly a win when there are multiple images with the same dimensions (so you can do stuff like .comment img.usergroup { width: 16px; height: 16px; }), and have the same images subject to scaling in different stylesheets like user-selectable themes.

When you have completely independent images that are used on the site only once, it doesn't really make sense to abstract their size out to CSS, so the HTML version would probably be more appropriate there.

硬不硬你别怂 2024-08-22 12:48:35

我认为 CSS 为您提供了更大的灵活性:您可以专门设置宽度或高度,同时将其他尺寸设置为 auto。但是当设置两个尺寸时,我不认为有什么区别。

I think CSS gives you more flexibility: you can specifically set the width or height while setting the other dimension to auto. But when setting both dimensions, I don't thing there's a difference.

空城缀染半城烟沙 2024-08-22 12:48:35

这并不能直接回答您的问题,但我不会依赖图像的尺寸来进行页面布局。相反,将图像包含在块级元素中。这使得 HTML 和 CSS 不必保存它们实际上不应该保存的信息,因为图像可能会不时发生变化。

This does not answer your question directly, but I would not rely on the dimensions of your image for page layout. Instead include the image in a block level element. This relieves both the HTML and CSS from having to hold information that it really shouldn't as the image may change from time to time.

一瞬间的火花 2024-08-22 12:48:35

如果您将大图像放入没有尺寸的 HTML 页面中,您一定会注意到下载图像时页面布局发生变化(通过互联网连接,如果不是本地下载)。

根据其他答案,无论是在 HTML 还是 CSS 中执行此操作都没有太大区别。

If you put a large image in an HTML page without dimensions, you should definitely notice the page layout shifting as the image is downloaded (over an internet connection, if not locally).

As per other answers, it doesn’t make much difference whether you do this in the HTML or the CSS.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文