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?
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With