Guide
The HTML lang attribute and why it matters
The HTML lang attribute states the language a page, or a part of a page, is written in. It goes on the opening html tag, for example <html lang="en">. Screen readers and browsers read it, so leaving it out is an accessibility failure.
What the HTML lang attribute is
The HTML standard defines lang as the attribute that specifies the primary language of an element’s contents and of its text attributes. You can use the HTML lang attribute on any element, and the elements inside inherit it. As a result, one attribute on the html tag covers the whole page.
<!DOCTYPE html>
<html lang="en">
<head>...</head>
<body>...</body>
</html>
The W3C’s advice is direct: always use a language attribute on the html tag to declare the default language of the text in the page.
HTML lang codes
The value must be a valid BCP 47 language tag. In other words, it is a short language code, and you add a region only when it matters:
| Value | Meaning |
|---|---|
en | English |
en-GB | English as used in the United Kingdom |
es | Spanish |
es-ES | Spanish as used in Spain |
pt-BR | Portuguese as used in Brazil |
The W3C’s guide to choosing a language tag says to add a region only if it tells something needed to set this text apart from another. For example, en-GB can help a spell checker. For most pages, however, the language alone is enough.
Who reads the lang attribute
- Screen readers. They use it to load the right pronunciation rules, because Spanish text read with English rules is very hard to follow.
- Browsers. They use it to display characters and scripts correctly.
- Media players, which use it to show captions correctly.
For this reason, the language of the page is part of the Web Content Accessibility Guidelines. Success Criterion 3.1.1, Language of Page, is Level A, the most basic level: the default language of each page must be programmatically determinable. In short, the HTML lang attribute on the html tag is how a page meets it.
Marking a phrase in another language
When a passage is in a different language from the rest of the page, give that passage its own lang:
<p>The Spanish page is called <span lang="es">Sobre nosotros</span>.</p>
This is Success Criterion 3.1.2, Language of Parts, at Level AA. However, the rule does not apply to proper names and technical terms.
lang and hreflang are different things
People often confuse the two. lang describes the page you are on. hreflang describes a link to another page, and is how a site tells search engines about its other language versions. Therefore, a multilingual site needs both: lang for people and their software, hreflang tags for search engines.
Neither one decides how a search engine classifies your page. Google states that it does not use hreflang or the lang attribute to detect the language of a page. It reads the visible text.
The lang attribute in WordPress
WordPress themes print the attribute with the language_attributes() function, and the value comes from the site language under Settings, then General. A site set to English (United States) prints lang="en-US" on every page.
However, that is the limit on a multilingual site: there is one site language, so every page announces the same language, including the translated ones. Vabelon corrects this per page. A page on a Spanish address announces Spanish, and a page in the default language keeps the site language. See hreflang and page language for what is printed.
How to check the lang attribute of a page
View the source of the page and look at the first lines. The opening html tag should carry a lang value that matches the language of the text.