Hacker News new | ask | show | jobs
by d_bud 1320 days ago
MDN says that img tag purpose is not just to serve as a fallback, but also to specify size “and other attributes”. Does it also include alt attribute? This is important. Can someone confirm that?
4 comments

The article says

  The final <img> tag also specifies common attributes like width, height and alt text. Those attributes will apply regardless of which source the browser chooses!

The official spec for the picture element is here https://html.spec.whatwg.org/multipage/embedded-content.html... and it says

  The picture element is a container which provides multiple sources to its contained img element to allow authors to declaratively control or give hints to the user agent about which image resource to use, based on the screen pixel density, viewport size, image format, and other factors. It represents its children.
So I read that as the alt text (as well as other attributes other than the actual src url) come from the img tag
It's also the img tag that you style in CSS. So for all layout purpose, it's still the img element that defines the container and the picture element is more a syntactical bracket to associate other source-sets with it. (This isn't very HTML-y, but, well…)
Adding something I don’t currently see mentioned explicitly: the img tag is the actual presented media element, picture/source are not displayed without an img tag. So the img tag itself isn’t even a fallback, only its src attribute.
Yes, all attributes from <img> applies to the selected image, not just width or height.