在 HTML 与 CSS 中指定页面加载的图像尺寸

发布于 2024-10-04 05:32:45 字数 417 浏览 7 评论 0原文

我很久以前就从某个地方了解到,在 HTML 文档中指定 元素的宽度和高度可以加快并改善页面加载体验,并且通常坚持这种做法

<img src="" width="100" height="100" />

:现在面临的情况是,我在单个页面上有大量图像,并且我更喜欢通过 CSS 设置尺寸,以便更轻松地控制和减少重复的 HTML 代码:

.whatever img {width: 100px; height: 100px;}

尽管这并不是一个非常严重的问题问题,我想知道在 CSS 中声明尺寸是否与在 HTML 中声明尺寸具有相同的好处,或者是否应该直接在 HTML 中完成?

任何见解都会受到欢迎。

谢谢

I've learnt from somewhere a long time ago that specifying width and height for <img> elements in an HTML document speeds up and improves the page loading experience, and have generally kept to this practice:

<img src="" width="100" height="100" />

I'm now faced with a situation where I have a very large number of images on a single page, and I'd prefer to set the dimensions via CSS for both easier control and less repetitive HTML code:

.whatever img {width: 100px; height: 100px;}

Although this isn't a hugely serious issue, I'm wondering whether declaring the dimensions in CSS will have the same benefits as declaring the dimensions in HTML, or whether it ought to just be done in HTML directly?

Any insight would be welcome.

Thanks

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

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

发布评论

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

评论(2

听风吹 2024-10-11 05:32:45

我想如果样式表立即可用,图像尺寸将立即应用于布局,这就是练习的重点。

Google 的 pagespeed 规则支持这一猜测。他们似乎很擅长以这种方式指定图像(强调我的):

指定所有图像的宽度和高度可以消除不必要的回流和重绘,从而加快渲染速度。

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

I guess if the style sheet is available immediately, the image dimensions will immediately apply to the layout, which is the whole point of the exercise.

That guess is supported by Google's pagespeed rules. They seem to be fine with specifying images that way (emphasis mine):

Specifying a width and height for all images allows for faster rendering by eliminating the need for unnecessary reflows and repaints.

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 <img> tag, or in CSS.

我是男神闪亮亮 2024-10-11 05:32:45

在属性中定义宽度和高度(与在 CSS 中相对)的关键区别在于,它会变成数据,而不是表示参数。想象一下管理以下样式表

img[src='/images/tree.jpeg'] {
    width: 100px;
    height: 100px;
}

这不必要地将 CSS 与图像纠缠在一起。它还限制了自适应布局,即当推断出图像尺寸之一时(例如 width: 100%),您不能再让尚未加载的图像占用正确的空间。

另外,请考虑 CSS 规则,例如 object-fit< /代码>。可能会出现关于 widthheigth 样式属性含义的混淆。

The crucial difference between defining width and height in an attribute (as opposed to in CSS) is that it then becomes data, not a presentation parameter. Just imagine managing a following stylesheet

img[src='/images/tree.jpeg'] {
    width: 100px;
    height: 100px;
}

This needlessly entangles CSS with images. It also restricts adaptive layout, i.e. you can no longer have not-yet-loaded images take up correct space when one of their dimensions is inferred (e.g. width: 100%).

Also, consider CSS rules such as object-fit. Confusion about what width and heigth style properties then mean may arise.

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