Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between the HTML width / height attribute and the CSS width / height property on the img element?

The HTML <img> element can have the width / height attribute, and it can also have the CSS width / height properties:

<img src="xxx.img" width="16" height="16" style="width: 16px; height: 16px"></img> 

What's the difference between the HTML attribute and CSS property and should they have the same effect?

like image 400
Morgan Cheng Avatar asked Aug 25 '10 01:08

Morgan Cheng


People also ask

What is the width and height attribute in HTML?

The height and width attribute in HTML is used to specify the height and width of the element. The values are set in px i.e. pixels. To add an image in an HTML page, <img> tag is used. With that, we need to use the attributes height and width to set the height and width of the image in pixels.

What is the use of height and width properties in CSS?

The height and width properties are used to set the height and width of an element. The height and width properties do not include padding, borders, or margins. It sets the height/width of the area inside the padding, border, and margin of the element.

What is height property in CSS?

The height CSS property specifies the height of an element. By default, the property defines the height of the content area. If box-sizing is set to border-box , however, it instead determines the height of the border area.

What is CSS width?

The width CSS property sets an element's width. By default, it sets the width of the content area, but if box-sizing is set to border-box , it sets the width of the border area.


1 Answers

A hot debate about the subject can be found here: Width attribute for image tag versus CSS

To sum it up:

The gain from declaring a width value and an height value (which may not be the original physical dimensions of the image) or from css declarations (like width: [valueX]; height: [valueY];) is that it helps speed up the rendering of the page. The browser knows how much space to allocate a particular area of the page: it will even draw image placeholders on a first draw, a first parsing+rendering of the page. When one does not define any width and height, then the browser has to download the image and then figure out its dimensions, space to allocate and then redraw the page.

This seems to be the most beneficial effect in my opinion.

like image 116
Ibrahim AshShohail Avatar answered Sep 19 '22 15:09

Ibrahim AshShohail