是否应该在 html 中指定图像大小?
我记得很久以前对任何图像的宽度和高度进行硬编码是最佳实践(通常因此在加载时分配适当的空间),但现在大多数人都在高速并且事物通常更加动态,对此的最佳实践是什么?任何内容图像是否仍然首选使用 html 设置内联尺寸?
I recall it was long ago best practice to hardcode width and height for any image (generally so it allocated appropriate amount of space while loading), but now with most people on high speed and things generally more dynamic, what is the best practice for this? Is it still preferred that any content image have inline size set with html?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
使用 HTML 属性或在样式表中设置大小并不重要,但您仍然应该指定图像的大小。
尽管现在图像加载速度快得多,但页面显示和图像弹出之间仍然存在明显的延迟。当图像加载时页面布局发生变化仍然令人恼火。
It doesn't matter if you set the size using HTML attributes or in a stylesheet, but you should still specify the size for images.
Eventhough images are loaded a lot faster nowadays, there is still a noticable delay between the page being displayed and the images pop up. It's still irritating when the layout of a page changes while the images are loading.
是的,它仍然是首选。
很多人都没有高速连接,而移动设备正变得越来越普遍。
Yes, it is still preferred.
Plenty of people are not on high speed connections, and mobile is becoming more common.
它不必是内联的 - 您可以在外部 CSS 中完成。一些较旧的浏览器,如果不指定大小,只会将其视为 0px;
It doesn't have to be inline - you can do it in external CSS. Some older browsers, if you don't specify the size, will just treat it as 0px;
最好使用 CSS
你可以像这样硬编码图像的高度和宽度
Its always best to use CSS
You could hardcode the image height and width like this
大多数情况下,如果您特别担心加载速度慢,则图像文件本身应该是您希望其显示的大小!如果您有 500X800 像素的图像,而您只想显示为 100X200,请将其缩小!文件大小会小得多,因此加载速度会更快:)
your image file itself should be the size you want it to display as, for the most part, if your concerned about slow loading especially! if you've got a 500X800 px image, that you only want to show as 100X200, scale it down! the file size will be much smaller so it will load faster :)
如果您想确保在图像未加载或文档加载期间包含空白区域,我会说“是”。但如果您使用这些属性缩放/调整图像大小,则不行,因为它会给浏览器带来不必要的负载并导致图像失真。
I would say yes if you want to make sure the white space is included in case of the image does not load or during document load. But no if you're scaling/resizing the image with those attributes, as its unnecessary load on the browser and causes image distortion.
如果您正在设计跨浏览器兼容性,那么您至少应该在 CSS 中指定图像本身的高度和宽度。我发现如果没有专门为图像指定尺寸,FireFox、IE、Opera 等之间会不一致。由于每个浏览器(更不用说不同版本)对 HTML 标准的遵守情况都不同。我发现有些浏览器会尽力推断 HTML 设计者的意图,而其他浏览器只是在出现第一个错误时发出警告。如果您打算从平板电脑等移动设备查看网站,我还建议使用 em 尺寸,而不是像素或 %。我会说,但是我刚刚开始使用 HTML5,所以我不知道 HTML5 在图像方面的差异。
If you are designing for cross browser compatibility, then you should at the very least specify the height and width in your CSS for the image itself. I have found inconsistency between FireFox, IE, Opera, etc if sizes are not specified specifically for the image. Due to the fact that each browser, not to mention different versions, handle adherence to HTML Standards differently. I have found that some browsers will do its best to extrapolate the HTML designers intent, while others just croak on the first error. I would also recommend em sizes, rather than pixel or %'s if you intend for the website to be viewed from a mobile device such as a tablet. I will say, however I have just started playing with HTML5 so I don't of the difference in HTML5 with respect to images.
我刚刚在 WordPress Stack Exchange 和 Webmaster Stack 上回答了类似问题。我发在这里是为了澄清和帮助更多的人。 (管理员/版主:如果不允许这样做,请让我知道提供帮助的正确方法)。
I just answered a similar question on Wordpress Stack Exchange and also on Webmaster Stack. I am posting it here intending to clarify and help more people. (admins/moderators: if this isn't allowed, let me know the proper way to help).