lang (HTML attribute)
Example
Specifying the language of a page as English in an XHTML Strict document:
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
Indicating a change of language in the middle of a document:
<p>I told the guy in the best French I could muster up: "<span lang=”fr”>Mais j’ai oublié mes pantalons</span>” and hoped for the best.</p>
Description
The lang attribute is used to
identify the language of the content on a web page (by applying it to the
html element) or within a given section on a web page
(applying to a div, span,
a or any other element where there may be a change of
language). By changing the language at a lower level in the document tree,
it overrides the language code set higher up, but only for the
duration of the nested element to which the changed language code is
applied.
The intention of the lang attribute
is to allow browsers (and other user agents) to adjust display
accordingly, for example if using the q element, a browser should be
able to identify the language in use and present the appropriate style of
quotation marks accordingly. In practice, though, because of poor support
for the q element, few developers use it and instead
hard-code quotation marks. It is something of a Catch-22
situation.
The lang attribute is useful for
users with assistive technology such as screen readers that can adjust
pronunciation depending on the language in use. For example the word
penchant, meaning ‘a habitual liking for something’ is French in origin.
When the screen reader JAWS encounters the word, it pronounces it similar
to ‘penchunt’ but when it is marked up as <span
lang=”fr”>penchant</span>, it reads it properly using
the French pronunciation ‘pon-shont’.
It may also be possible that marking up documents or sections of a document in this way may benefit search engines when displaying results to users who have filtered their search based on language preferences. However, how search engines actually deal with content marked up in this way is a secret they tend to keep to themselves. As such, the search engine friendliness aspect should be considered little more than a lucky bonus.
Value
Approved International Standards Organisation (ISO) 2-letter language codes only (refer to the language codes reference)
Compatibility
| IE | 5.5 | Partial |
|---|---|---|
| 6.0 | Partial | |
| 7.0 | Partial | |
| Firefox | 1.0 | Partial |
| 1.5 | Partial | |
| 2.0 | Partial | |
| Safari | 1.3 | Partial |
| 2.0 | Partial | |
| 3.0 | Partial | |
| Opera | 9.2 | Partial |
| 9.5 | Partial |
As mentioned in the
description above, browsers do not offer great support for the
lang attribute but it offers benefits beyond the
browser itself (search, assistive technology) and language should be
indicated for these reasons, as well as for forwards compatibility.
User-contributed notes
There are no comments yet.
Add a note
To post a note on this topic, please log in with your SitePoint username and password. If you don't have an account yet, you can create a new account for free.

