I found a similar question here, with the answer: “you should always define the width and height in the image tag.” But it is from 2009.
In the meantime, many things has changed on frontend. We are all doing responsive page design now, for many devices and sizes simultaneously (mobile, tablet, desktop…).
So, I wonder is it still necessary to specify the width and height attributes, and for what reason (for responsive, page speed, SEO…)?
7
Answers
An
img
element has width and height attributes, but they’re not required under anyDOCTYPE
.Width and height attributes were only ‘required’ or relevant to reserve the space on the page and prevent the page moving around as it loads – which is important. This can be achieved using CSS instead providing the CSS loads quickly enough – it is likely to load before the images anyway, so all should be good.
It is also possible (and valid) to specify just one attribute, width or height and the browser will calculate the omitted value in order to maintain the correct aspect ratio.
You can specify percent values in the attributes if required. You don’t need to use CSS for this, if that is what you are implying.
Also, it is relevant to add – Under HTML5 the width and height can only take a pixel value, in other words a valid non-negative integer.
Whether you use the width and height attributes can depend on your design. If you have lots of differently sized images, do you want to lump all the dimensions in the CSS or include them with the img?
Good standards are always worth a recommendation. With a little extra code it’s quite easy to merge static (px) values of the img tag and generic (em, %) values supplied by CSS. And simpler still, get rid of the img tag altogether and set the picture as background of a div with a unique ID. If you have multiple images, use sprites and assign each picture to its corresponding div. Your mark-up sources would then look something like
<div id="image_001"></div>
– that’s all. Scales all by itself; no need for bloatware like JQuery, etc.If we’re talking ’bout responsive, you may use bootstrap (if not, start doing this).
When working with images, you should add the class img-responsive, this will modify the width of the image if necessary and the height will be auto, so if width decreases, height will decrease too.
You will always have an image that keeps the same % of its container and will never loose the aspect ratio.
There’s no relation with SEO and image size declarations.
Page speed will be the same always, so if the image is 800 x 600 px, you’ll load the full image, even if you declare it as 60 x 40 px.
You must think that, even using img-responsive, the max width and height of this image will be the real size of the image. So if we have a 800 x 600 px image, it will not enlarge it (because it’ll become loosing quality).
So in 2016, it’s recommendable to NOT declare height and width of an image. Instead use bootstrap’s img-responsive class, other responsive framework class that gets the same result, or hand-made the proper jquery and css to reach the same.
Hope it helps!
Well, the basic answer to this question (as with most coding issues) is this: it depends on the situation at hand.
I would say that the “best practice” of always specifying the
height
andwidth
attributes of images making a significant difference to page rendering speeds hark back to the days when designers laid out their websites using tables and spacer GIFs. We have come a long way since then.An indication for the future is the introduction of the new
picture
element being drafted into HTML. Thepicture
element is effectively a wrapper for the existingimg
element, which allows you to specify several images of different sizes via asource
element, and the user-agent itself actually determines which version is used.As you can see from this example code above (taken from the Intel Developer Zone’s article on the HTML5
picture
element) there are noheight
orwidth
attributes on theimg
element itself.Here are a selection of resources that will help you to decide the most appropriate method of declaring image sizes:
picture
elementYES, you want to declare the width and the height of an image in 2016.
If you want your image to be retina-ready, you should define a width and an height lower than the actual pixels. If the image is 800×600 specify
<img width="400" height="300" />
.Without the width and the height the image does not know how large it is, which causes an unwanted jump in the page as it loads (it reflows). Declaring height and width solves this problem.
Note that:
max-width
andmax-height
to your CSS. This will cause the image to scale down (not up) when it does not fit the screen (see this sweet retina-ready, responsive kitten). Defining amin-width
andmin-height
will do the opposite.Yes, It is still relevant to specify width and height attribute on images in HTML.
Hence, specifying width and height attribute on image will improve the webpage performance by protecting from delay in loading.
Yes, it is necessary to add height and width attributes to the img tag along with the src and alt attributes to prevent page-jumping. When our page loads, the specified space will be preserved for the image so that the it can occupy that place peacefully.
But, there is another problem that will arise here Responsiveness.
Once we give height and width attribute to img tag, the image will tend to stay in the same height for all screen-sizes which will make the image to shrink.
To avoid this, we need to add height: auto; to the image in the CSS file.