(I'm interested in HTML 4.01 and HTML5, if there should be differences)
Does the lang
attribute on an img
element apply to the src
attribute, too? Or is it only for the alt
and title
attributes?
Example:
<img src="example.png" alt="a red foobar" lang="en" />
Is the image "example.png" considered to be in English? (think of screenshots of a forum thread, or a graphical representation of a word, or a scan of a document)
If it's true, images with non-linguistic content would need to get lang="zxx"
. But that would apply to the alt
/title
attributes, too, which would be incorrect.
The img element has two required attributes: src : The source location (URL) of the image file. alt : The alternate text. This is used to describe the image for someone who cannot see it because they are either using a screen reader or the image src is missing.
The <img> tag creates a holding space for the referenced image. The <img> tag has two required attributes: src - Specifies the path to the image. alt - Specifies an alternate text for the image, if the image for some reason cannot be displayed.
The lang (or sometimes the xml:lang ) attribute specifies the natural language of the content of a web page. An attribute on the html tag sets the language for all the text on the page.
In a nutshell Always add a lang attribute to the html tag to set the default language of your page. If this is XHTML 1. x or an HTML5 polyglot document served as XML, you should also use the xml:lang attribute (with the same value). If your page is only served as XML, just use the xml:lang attribute.
HTML 4.01 defines the lang
attribute as specifying “the base language of an element's attribute values and text content”, whereas HTML5 defines it as “the primary language for the element's contents and for any of the element's attributes that contain text”. The difference is apparently in the formulation only. The lang
attribute specifies the language of alt
and title
attribute as well as other attributes that may contain prose text, as opposite to code-like values like URLs or style
attributes, where (human) language is not applicable.
The src
attribute itself is not of any (human) language, logically. So the question is whether the lang
attribute extends to the image denoted by the src
attribute. This is a fairly theoretical question – what impact on software behavior could the answer possibly have? Anyway, the answer depends on what we understand as “text content” (images are text in a sense, in formatting, but probably HTML 4.01 means to refer to actual character data only) and as “element’s contents” (is an image part of the img
element’s contents?). Overall, it seems that the language of the image (though a feasible concept) cannot be specified in HTML.
So there is no need to worry about images with non-linguistic content. For text content that is “non-linguistic” (i.e. not text in any human language but e.g. some code notation, or a random sequence of character), using lang=""
is what HTML5 recommends. It’s also the practical approach. In the few cases whete lang
attribute has any impact, as in automatic hyphenation, lang=""
effectively means that no language rules are applied (e.g., no hyphenation). This is different from omitting the attribute, which means that the element inherits language information from its parent.
In HTML 4.01, section 13.8 says:
The language of the alternate text is specified by the
lang
attribute.
In HTML5, it says:
Specifies the primary language for the contents of the element and for any of the element’s attributes that contain text.
I can't find anything in the current HTML5 draft that defines the relationship between the alt
and lang
attributes as explicitly as the HTML 4.01 spec does. Hopefully it'll be clarified later, but I would assume for now that there isn't any change, and that what it does state is just as applicable.
Neither spec states an explicit relationship between the src
and lang
attributes, but I wouldn't assume that the language of the source image itself corresponds to the lang
attribute, since not all images will have linguistic content (as you've mentioned).
The same goes for title
and lang
, but this may depend on the element type, I'm not entirely sure. Again, what it does state in general above should be applicable to title
.
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