Definition
langnames the language of an element's text and of its text attributes, with a BCP 47 tag:arfor Arabic,enfor English, plus a region when it matters, such asar-SA.- Elements inherit it, so the page's language goes on the root element,
<html lang="ar">, and a passage in another language takes its ownlang. An empty value means the language is unknown. langdoes not set the direction: an Arabic page needsdir="rtl"as well.
Why it matters
- Screen readers choose voice and pronunciation by it: an Arabic page declared English, or with no language, may be read with English pronunciation nobody can follow.
- WCAG 2.2 asks that software can determine the page's default language (success criterion 3.1.1, level A) and the language of each passage (3.1.2, level AA), except proper names, technical terms and words that have become part of the surrounding text.
- Browsers use it for choices such as fonts and word breaking, and CSS can select by it with
:lang(ar). - Google does not use it: it reads a page's language from its visible content, and learns about its other language versions from
hreflang.
Example
The page's language on <html>, and an English name marked where it appears:
html
<html lang="ar" dir="rtl">
<body>
<p>ادفع عبر <span lang="en">Apple Pay</span> أو بطاقة مدى.</p>
</body>
</html>Common mistakes
- Leaving the
lang="en"of an English template on the Arabic version of the site. - Writing a language's name instead of its code:
lang="arabic"orlang="english"names no language, so screen readers do not switch. - Using
<meta http-equiv="content-language">instead of the attribute: the W3C advises never to use it for that. - Putting
langon<body>only: it does not cover<head>, where<title>is.