views:

625

answers:

6

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; }
A: 

I think there is probably not a canonical answer, however, my current practice is to always use CSS for image dimensions. However, that CSS may be applied as a style directly on the image tag, if needed:

<img src="" style="width: 200px; height: 200px;" alt="" />

In fact, this is the more common way for me. In certain very specific scenarios (typically the main images of a template, like perhaps the logo) I'll place the actual dimensions in the linked CSS file, but this is typically because I'm already styling the image there, and it will keep the output HTML cleaner.

I'm actually interested in seeing other responses to this myself, because I've been wondering if the width and height attributes of the img tag have been deprecated, or if they're actually accepted practice.

I'm also interested in knowing how or if this playing field changes with HTML 5.

John Rudy
Why on earth would you put HTML image dimensions in a style attribute instead of using the width and height attributes? All you’re doing is marginally increasing the size of the HTML file.
Paul D. Waite
I hadn't even considered this alternative :) Still, I would like to know which method is fastest, rendering-performance-wise: HTML, CSS or inline CSS as in your example...
Daan
“I've been wondering if the width and height attributes of the img tag have been deprecated” — no, they haven’t.
Paul D. Waite
@Paul: The HTML 4.0 spec indicates that "all `img` ... attributes that concern ... presentation have been deprecated in favor of style sheets." (See http://www.w3.org/TR/REC-html40/struct/objects.html#h-13.7) I have not checked the more recent specs, including HTML5, though, so I'm open to my interpretation being incorrect.
John Rudy
@John so why are you suggesting to use inline styles ... that's not a stylesheet?! GO BACK AND READ THE SPEC AGAIN(13.7.1 Width and height) ... it even tells you to use width and height as attributes, not styles.
Jonny Haynes
@Jonny Haynes: You're reading a recommendation where there is none. Where appropriate, I define them in the linked CSS file. This is not always appropriate. Going to the spec, 13.7.1 does *not* say to not use them as styles, nor does it say to prefer the attributes, nor does it say anything similar. But 13.7 explicitly says the presentation attributes are deprecated. Inline is OK where it's appropriate, linked CSS is preferred where it's appropriate. Granted, in the current HTML5 draft, I see nothing about deprecation of the attributes; maybe they're heading back that way in the new rev.
John Rudy
Also, where are style sheets defined as not including inlines? Inlines are, in fact, the final override point for a style sheet.
John Rudy
@John: as per the part of the spec you linked to, these <img> tag attributes have been deprecated: hspace, vspace, border, align. You can tell, because it says “deprecated” after them. width and height, in contrast, haven’t been deprecated. Also, they’re not purely presentational. Bitmap images have an inherent width and height, in pixels. That’s content information, a bit like text encoding for HTML files.
Paul D. Waite
@Paul: I'll accept that as a misread on my part. However, that simply softens my position to there is nothing inherently wrong with either approach; not that one is necessarily better than the other.
John Rudy
+1  A: 

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.

Kaleb Brasee
Actually you can do that with HTML too, by omitting one attribute! However CSS does give you more flexibility in that you can also use `min-width`, `max-height` et al.
bobince
In most browsers, if you only set one dimension the other is auto by default.
Andy E
+3  A: 

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.

bobince
“have the same images subject to scaling in different stylesheets” — literally the same image files? Don’t you find they tend to look a bit rubbish in IE?
Paul D. Waite
This makes sense from a coding / maintenance point of view, but in this case I'm specifically interested in the rendering performance: which method causes the page to render the fastest?
Daan
@Paul: not necessarily; some images like simple block-colour GIFs might be specifically designed to be scaled. Plus in IE7+ you get optional bicubic interpolation so other material needn't look bad. @Daan: there will be no measurable difference. In different browsers either the HTML or the CSS may in theory have a more direct path to the renderer (in Firefox, it's the CSS), but the difference will be beyond minuscule.
bobince
Ah, that’s true. “in IE7+ you get optional bicubic interpolation” — optional? How do you turn it on?
Paul D. Waite
`-ms-interpolation-mode: bicubic`. http://msdn.microsoft.com/en-us/library/ms530822%28VS.85%29.aspx (see also https://developer.mozilla.org/En/CSS/image-rendering )
bobince
+1  A: 

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.

Paul D. Waite
Many browsers won't render anything before the CSS is parsed anyway. Certainly the CSS will come down before images. (See ‘flash of unstyled content’ for discussion of when this doesn't happen.)
bobince
Yeah, that’s true.
Paul D. Waite
A: 

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.

Oliver White
+6  A: 

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.)

Wookai
This is the kind of answer I was looking for. But, coming from Google, does this only apply to Chrome, or other browsers as well?
Daan
Google does not discriminate between browsers. Never.
Josh Stodola
I can't speak for them, but I don't think they would discriminate between browsers. Google Page Speed is aimed at speeding the whole Internet, and for all users, thus giving advice specific to their browser (that has a very low market share) would not be very useful...
Wookai