Is it a good idea / practice to specify the unit of measurement on img elements in html markup?
I've always done, and thought yes. I assume it avoids browser from guessing either % to px?
Maybe I am wrong here, and it's quicker, more efficient, and suggested, to simply specify numeral value without trailing UOM.
I've read through开发者_运维技巧 the W3C, and found nothing specific to my question.
<img src="/path/to/image/thegood.jpg" width="100px" height="100px" />
vs.
<img src="/path/to/image/andthebad.jpg" width="100" height="100" />
The spec specifically states the length without a percent indicates pixels.
<!ENTITY % Length "CDATA" -- nn for pixels or nn% for percentage length -->
I prefer to use px as it preserves the image ratio as you intended. Width of 100% can stretch the image on a wide screen leading to a poor image!
Performance wise, dont even consider it.
Otherwise if you'd want to restrict the image size, you should specify it. If you'd just want the fullsize image dont specify it at all. And where possible use CSS.
You should always specify the images' measurement. When the browser loading your page, the screen will less flicker because the browser know the image sizes in advance.
So you #1 code is preferred:
<img src="/path/to/image/thegood.jpg" width="100px" height="100px" />
精彩评论