html - 我应该在 HTML 中为我的 IMG 指定高度和宽度属性吗?

标签 html css image

如果我知道要用图像标签显示的图像的高度和宽度,我应该包含高度和宽度属性,还是只将信息放在 CSS 中?还是两者兼而有之?

例如

<img src="profilepic.jpg" height="64" width="64" />

<img src="profilepic.jpg" height="64" width="64" style="height: 64px; width: 64px;" />

<img src="profilepic.jpg" style="height: 64px; width: 64px;" />

最佳答案

根据 Google Page Speed ,您应该始终在图像标签中定义宽度和高度。但是,要验证您不能使用样式标签。

此外,您应该始终指定与实际图像相同的高度和宽度,这样浏览器就不必像调整大小那样对其进行任何修改。

我建议这样做

<img src="..." height="20" width="50">

编辑:有人在评论中建议不添加任何属性会更快。根据谷歌的说法(并不是说它们是所有浏览器知识的终结):

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. - Read More

鉴于此,您可以在 CSS 中执行 img 尺寸,但要验证您必须在 CSS 文件中执行,而不是内联。

顺便说一句,Google Page Speed 是一系列专注于更快呈现页面的技巧。

关于html - 我应该在 HTML 中为我的 IMG 指定高度和宽度属性吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1247685/

相关文章:

javascript - 如何使用javascript查找文件?

jquery - JvectorMaps 的不同颜色标记

html - 正文不包含内容/与加权页脚结合

javascript - 在使用 Bootstrap 的响应式设计中,CSS Force 内容低于固定 DIV?

javascript - localstorage 不切换 View 并且在控制台中不产生任何错误

php - Magento 2 CSS 无法加载

html - 修复了滚动列表顶部的标题。所有列高度相同的 Flex 布局

ios - 是否可以在 AVCaptureVideoDataOutput 中设置 AVCaptureSessionPresetPhoto?

javascript - 在图像加载之前不要解决 promise

c# - 福斯康网络摄像机: get snapshot