The HTML hreflang attribute
On a link, hreflang="de" says the page it points to is in German. Any valid BCP 47 language tag is allowed, such as en, pt-BR or zh-Hans, and browsers treat it only as a hint. Paired with rel="alternate" on an <a> or <link>, it can mark a translation of the current page.
Overview
The HTML hreflang attribute states what language a linked page or file is written in. Links made with <a> or <link> take it when they have an href, while its place on <area> is still unsettled.
The HTML standard bars browsers from treating hreflang as authoritative, so once the linked page arrives, they go by the language information that comes with it. On its own, hreflang has no built-in functionality.
Its value is a BCP 47 language tag made of subtags joined by hyphens. The common ones are a two- or three-character language such as de, an optional four-character script such as Hans in zh-Hans, and an optional region such as BR in pt-BR or 013 in es-013.
In the head, <link rel="alternate" hreflang="de" href="https://example.com/de/"> gives hreflang one more job. When its value differs from the page's language, the linked document counts as a translation, and translations listed together are alternates of each other.
Syntax
<a href="/de/" hreflang="de" lang="de">Deutsch</a>
<a href="/pt-br/" hreflang="pt-BR">Portuguese (Brazil)</a>
Values
| Value |
|---|
A BCP 47 language tag, such as en, fr or pt-BR. |
Example
<div lang="en">
<p>Point at a link, Tab to it or click it to compare lang and hreflang.</p>
<p>
<a href="https://example.com/de/" hreflang="de" lang="de">Deutsch</a> |
<a href="https://example.com/fr/" hreflang="fr" lang="fr">Français</a> |
<a href="https://example.com/pt-br/" hreflang="pt-BR" lang="pt-BR">Português (Brasil)</a>
</p>
<p><a href="https://example.com/de/guide/" hreflang="de">Read this guide in German</a></p>
<p id="msg" role="status">No link chosen yet.</p>
</div>
<script>
const msg = document.getElementById('msg');
function show(event) {
const link = event.target.closest('a');
if (!link) return;
if (event.type === 'click') event.preventDefault();
const textLang = link.closest('[lang]').lang;
msg.textContent = 'The text "' + link.textContent + '" is in ' + textLang + ' (lang). The page it opens is in ' + link.hreflang + ' (hreflang).';
}
for (const type of ['mouseover', 'focusin', 'click']) document.addEventListener(type, show);
</script>
Best practices
- Take the
hreflangvalue from the target page's ownlang, such aspt-BRon both. Then the hint never contradicts the page. - Join subtags with hyphens, as in
en-US. Bothen_USand a full name such asenglishfail validation on validator.nu. - Select links by language with
a[hreflang|="pt"], which matches pt, pt-BR and pt-PT. Attribute selectors treat hreflang values as case-insensitive, so PT-br matches as well. - Don't expect
:lang()to read hreflang, since it matches an element's own language. On an English page,a:lang(de)matches a link withlang="de"but not one with onlyhreflang="de".
Accessibility
Put lang on link text written in another language, because hreflang describes only the linked page. The hreflang attribute isn't mapped to any accessibility API, while lang lets assistive technologies such as screen readers use the correct pronunciation.























