Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I specify height and width attributes for my IMGs in HTML?

Tags:

html

css

image

If I know the height and width of an image that I'm going to display with an image tag, should I include the height and width attributes, or just put the information in CSS? Or both?

Ex.

<img src="profilepic.jpg" height="64" width="64" /> 

or

<img src="profilepic.jpg" height="64" width="64" style="height: 64px; width: 64px;" /> 

or

<img src="profilepic.jpg" style="height: 64px; width: 64px;" /> 
like image 517
Josh Gibson Avatar asked Aug 08 '09 01:08

Josh Gibson


People also ask

Why is it important to specify an image width and height in image tag?

Adding the height and width attributes to your IMG SRC HTML tag allows the browser to know how much space to leave for an image. Without these values, the browser doesn't initially create a space for the image, which means elements surrounding the image are adjusted after it has loaded.

What happens if you do not put a width and height on an image in HTML?

If you do not supply height and width explicitly the <img> element will be rendered at 0x0 until the browser can size it based on the file. When this happens it causes a visual reflow of the page once the image loads, and is compounded if you have multiple images on the page.

Why should you always use the height and width attributes when coding the image tag in HTML?

You should also always use the height and width attributes for every image you create on a page. The reason for this is that it dramatically speeds the display of your web page in the browser. src - This lets the browser know where to find the image file on the web server.

What happens if you you do not set the width or height attributes of an element?

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: Images with a defined width and height can still be responsive.


1 Answers

According to Google Page Speed, you should always define the width and height in the image tag. But, to validate you can't use the style tag.

Also, you should always specify the same height and width as the actual image so the browser doesn't have to do any modifications to it like resizing.

I'd suggest doing it

<img src="..." height="20" width="50"> 

Edit: Someone suggested in the comments that it would be faster to just not add any attributes. According to Google (not that they are the end all of browser knowledge):

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. - Read More

Given that, you could do the img dimensions in CSS, but to validate you would have to do it in a CSS file, not inline.

BTW, Google Page Speed is a series of tips focused on rendering the page faster.

like image 173
Tyler Carter Avatar answered Sep 25 '22 12:09

Tyler Carter