Just wond开发者_如何学Cering what the best practice is in HTML. I have noticed that I tend to removed the width and height attributes, because they are a pain when I tweak an image, so best to only use if necessary.
Is this best practice? I am I incurring performance issue? Is this legacy stuff?
If you specify the size, browsers can render the page with proper spacing even before loading the images.
I'd say it's good practice to leave the width and height attributes in so that the page doesn't keep shifting around as the images load. In fact, it annoys me quite a bit when I'm trying to view a gallery and they've omitted these attributes! I suppose you could do it with CSS instead, or put the width/height on some container element... just keep in mind that the images will "expand" once the client finishes downloading them and their dimensions can be known.
It can be useful to have the image size so that they page layout can be done before the image is loaded. Otherwise the page renderer doesn't know how much room an image will take up until it knows the size of the image, and this can result in a lot of shifting around as the page loads.
This may not be a big deal for content images, but it can be distracting for images that have an impact on your page layout such as in headers, footers and sidebars.
As Archonix said in his comment, Google has a good overview.
精彩评论