May 19, 201511 yr I recently tested a website in 'Nibbler' which someone on here linked to (http://nibbler.silktide.com/) One problem it found was: 'No images have defined sizes. Add width and height attributes to all image tags. The width and height you specify should be the same as the source image. If you want to resize an image in the browser, use CSS.' I didn't realise this was necessary and typically only really set min/max widths on my images with CSS as I want them to be responsive. The w3 validator doesn't have a problem with it, so do I really need to do anything about it?
May 20, 201511 yr You're right, let your CSS deal with any width or height dimensions. The main thing developers often forget is to add the alt tag, this is important for accessibility purposes. I don't agree with some of their recommendations too like not using the <b> tag. When you wish to bold a bit of text but want to give it zero semantical meaning then it is ok to use, see this post by Harry Roberts http://csswizardry.com/2010/01/a-reconsiderationin-defence-of-b-and-i-or-people-fear-what-they-dont-understand/ They're also saying this is an error: A meta element with an http-equiv attribute whose value is X-UA-Compatible must have a content attribute with the value IE=edge. When the tag in question is <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> Which is also totally valid. There's some good information to be gleaned by using this tool but it's unfortunate that some of the erros it spouts out are not in fact errors. For further testing: Run your code through the W3C validators. Note: sometimes it will give errors when it actually isn't an error, but not so much like the tool you posted too. Look for things like missing off closing tags etc. Read through your HTML and make sure you are using the correct tags to describe the content, an article for an article, nav for a nav, figure for the main image of an article, correct heading levels. Ignore the CSS validators they are not great, unfortunately, often things that are valid trip them up. Test in all the popular browsers. Install the Y-slow plugin for Google Chrome and use that to test your page speed metrics, this is a very good tool. Edited May 20, 201511 yr by rbrtsmith
May 20, 201511 yr I think the reason that nibbler recommends width and height attributes is because it can can slightly enhance the rendering process (or rather the users perception of the rendering process). If a browsers knows the original dimensions of an image it can "reserve" the required space before the image has been loaded which can be very useful especially if you have images below the viewport. The page will apear to be fully rendered and the scrollbars will have the correct size even before the images at the bottom of the page has loaded. If you only use CSS width/max-width the image elements height will change while the page is rendering. EDIT: If you run an audit of your page using Chromes developer tools you will get the same message. Specify image dimensions ([number of images without width and height]) A width and height should be specified for all images in order to speed up page display. The following image(s) are missing a width and/or height: ... ... ... Edited May 20, 201511 yr by Nillervision
May 20, 201511 yr I think the reason that nibbler recommends width and height attributes is because it can can slightly enhance the rendering process (or rather the users perception of the rendering process). If a browsers knows the original dimensions of an image it can "reserve" the required space before the image has been loaded which can be very useful especially if you have images below the viewport. The page will apear to be fully rendered and the scrollbars will have the correct size even before the images at the bottom of the page has loaded. If you only use CSS width/max-width the image elements height will change while the page is rendering. EDIT: If you run an audit of your page using Chromes developer tools you will get the same message. This isn't strictly true for images with max-width:100%; and height auto; CSS properties specified, the images shrink depending on the dimensions of their container (Should the images original size be larger) so the pre-rendered space will not be valid based on those values. When you set a width and height in the CSS it actually overrides the width and height attributes of the image so in this case they don't improve rendering at all. In the lazy-load script I wrote http://rbrtsmith.com/2015/02/building-a-high-performance-lazy-load-module/ deals with this by taking the original width and height of the image and sizes the container to match that aspect ratio, so there's no height recalculations needed as images load in no matter what the screen size is. This improves the rendering so the page doesn't grow in height as images are loaded in. So if you have width and height declared in your CSS the properties set on your HTML will not make any difference to the rendering. Edited May 20, 201511 yr by rbrtsmith
May 20, 201511 yr This isn't true for images with max-width:100%; and height auto; CSS properties specified, the images shrink depending on the dimensions of their container (Should the images original size be larger) so the pre-rendered space will not be valid based on those values, the images also don't maintain their aspect ratio with width and height attributes applied when when shrunk by the container. In the lazy-load script I wrote http://rbrtsmith.com/2015/02/building-a-high-performance-lazy-load-module/ deals with this by taking the original width and height of the image and sizes the container to match that aspect ratio, so there's no height recalculations needed as images load in no matter what the screen size is. Yes the width of the element can be calculated when you have a max-width property of 100% (it will always stretch to fit the container) but auto-height can not be calculated if the browser has not loaded the image and no attributes are supplied. Providing original dimensions in the attributes means that the browser can calculate the height (by evaluating the css max-width and compare it to the image dimensions) before the image has loaded. Edited May 20, 201511 yr by Nillervision
May 20, 201511 yr The image doesn't stetch to fit -- stretch implying that it gets bigger., if the container is larger the image remains at it's original size, when the container is smaller the image shrinks to match this container size, hence the property name max-width: 100% But for an image that is going to be scaled down with the max-width height auto attritubes the original declared height-attribute will be incorrect because of this scaling. So the page will have to re-render anyway. Thats assuming that the height attribute is not overriden by the css:height auto in which case it will initially render with a height of zero, which I believe to be the case I ran into this issue when some parallax I wrote that was incorrectly calculating the viewport position within the document due to the document growing in height as images were being lazy-loaded in (Those images already had width & height attributes specified but max-width and height set also) When I re-wrote my lazy load plugin to pre-set container dimensions this problem was resolved as everything was given a width and a padding-bottom % to match the aspect ratio. then all their positions are cached in an array so there's no need to keep querying the Dom for their position everytime you scroll. That to me was evidence that the width and height attributes are of little use now. Edited May 20, 201511 yr by rbrtsmith
May 20, 201511 yr ^ OK. But the reason I gave must still be why nibbler recommends attributes (as well as the audit in Chromes developer tools) as I can't find any other reason. But it looks like you (rbrtsmith) has done quite a bit of testing which proves that conventions should always be questioned. Nice one EDIT: I suppose the reason is still valid if you do not have a max-width property Edited May 20, 201511 yr by Nillervision
May 20, 201511 yr It's pretty confusing I agree I guess these things take time to update, and if a user is not using width and height in the CSS then I agree that the width and height should be defined in the markup. This gets more complicated again when we look at responsive images which will serve up a different src depending on the device and/or viewport dimensions. I guess this also invalidates them. Things are moving so fast I can see why official standards cannot keep up. In my opinion the things that matter the most in markup validation are the semantics in terms of SEO and accessability so correct use of headers, tags, closing tags properly etc. The image dimensions debate we are having is only concerning rendering and there is no definitive answer as to which is right or wrong. 'it depends'. I think Nibbler should mark the missing attribute as a note, rather than an error, especially if it can detect if width & height are being applied in the CSS. Edited May 20, 201511 yr by rbrtsmith
Create an account or sign in to comment