A purchasing manager at a German distributor opens your product page. The screen reader announces the text in English, and the browser is guessing the language of a page that is entirely in German. (Hypothetical example.) The cause is often a single line at the very top of the document: <html lang="...">. In this article we explain what the HTML lang attribute does, how it differs from the hreflang annotation, and how the two work together.
If you want the bigger picture of multilingual search first, start with our complete guide to hreflang. This article focuses on the lang attribute and its relationship with hreflang.
Key Takeaways
- The lang attribute declares the language of a page or a part of it. hreflang tells search engines which language and region version of the same content should be shown to which audience.
- The two do not replace each other. When both are correct, users and search engines make fewer mistakes.
- Values follow the BCP 47 format, such as
en,en-GBorde-DE. - A template that outputs the same value on every page, usually
lang="en", is one of the most common errors. - For accessibility, lang is the subject of WCAG success criteria 3.1.1 and 3.1.2.
What the lang attribute does
The HTML lang attribute is a global attribute that states the language in which an element's content is written. The WHATWG HTML Living Standard expects the value to be a valid BCP 47 language tag, made of a language code defined by ISO 639 and an optional region code. To declare the primary language of a page, the attribute is set on the <html> element, where it applies to the whole document.
Browsers and assistive technologies use this information for several purposes. Screen readers can select a matching voice and pronunciation rules, and a wrong language can make text sound garbled. Browsers can offer to translate a page when they detect that its language differs from the one the user prefers. Hyphenation and spell checking also depend on the language. The exact behavior varies between browsers and versions, but all of it depends on the lang value being correct.
Choosing the right value
Choosing a value comes down to two questions: what language is the text written in, and does that language have a region-specific form that matters here? If there is no regional difference, a plain language code is enough. Examples:
lang="en": English, when no regional variant is intended.lang="en-GB": British English. For a US-facing version,en-USis used.lang="de"orlang="de-DE": German. Both forms are valid; consistency matters most.lang="pt-BR": Brazilian Portuguese. For European Portuguese,pt-PTis used.
A small but frequent mistake is the separator. BCP 47 tags use a hyphen (-), not an underscore. A value such as en_US fails validators and may not be recognized as a language by some clients. Tags are case-insensitive, but writing the language code in lowercase and the region code in uppercase is a common, readable convention.
lang vs hreflang: the difference
This is the point most often confused. lang tells the reader what language a page is written in. hreflang tells search engines that a page has other language and region versions of the same content. One page can say "I am German", but only hreflang can say "my German version is at this URL and my English version is at that one".
| Aspect | lang | hreflang |
|---|---|---|
| What it declares | The language of the page or a part of it | The language and region of an alternate page |
| Where it goes | An attribute on the HTML element (<html lang>) | A link element in the head, an HTTP header, or an XML sitemap |
| How many values | One value per element | One entry for each alternate version |
| Must it be reciprocal? | No | Yes; every alternate page must link back |
| Main purpose | User experience and accessibility | Showing the right version in the right market |
Google's hreflang documentation describes how to connect alternate pages using link elements, HTTP headers, or sitemaps, and it expects language and region values based on ISO 639-1 and ISO 3166-1 alpha-2 codes. The documentation does not say that the lang attribute replaces hreflang. So even when lang is correct, alternate pages are not connected unless the hreflang annotations exist and link back to each other. We cannot give a reliable figure for how much weight search engines place on language signals, so the only safe conclusion here is that the two tags do different jobs.
Other search engines may read these signals differently. We cover Bing's use of the content-language meta tag in our article on how Bing reads language signals, and the approach of Yandex and Baidu in our article on language targeting signals.
Accessibility: WCAG 3.1.1 and 3.1.2
WCAG 2.2 contains two success criteria that deal directly with language. 3.1.1 Language of Page (Level A) requires that the default human language of a page can be determined programmatically. 3.1.2 Language of Parts (Level AA) requires that passages written in a different language from the rest of the page are also identified. The second criterion means that if a page contains an English product name, a technical term or a quotation, that part should be marked with lang.
In a German product page, the markup looks like this:
<p>For the full technical specifications, see the <span lang="de">Datenblatt</span> in the download section.</p>
This markup changes nothing visually. A screen reader, however, can pronounce the German term with a German voice. Accessibility audit tools such as axe and Lighthouse usually report a missing or invalid lang on the html element. Their output is not enough on its own, but they give a quick first check.
How to output lang in templates
On a multilingual site, the lang value should never be a hard-coded constant. It should be generated from the language context of the page being rendered. If the language is set by a URL segment (for example /en/ and /de/), the root template should read that segment and write it into the <html lang> attribute:
<html lang="{locale}">
The key point is that the value must come from the page's own language, not from the default language. If a template renders German content but outputs lang="en" by default, every German page is labeled with the wrong language. The bug is often invisible in development, because nobody reads the source during local testing.
In single page applications, changing the language updates the visible text but can leave the <html> value unchanged. In that case the language switch should also update the lang attribute of the root element. For server-rendered pages, it is enough that the correct value is present in the HTML response, so the browser receives the right language from the first load.
Using lang and hreflang together
On a multilingual site, a correct setup has three layers. Each page's lang value must describe its own language. Each page's hreflang annotations must point to the other versions of the same content. And those annotations must be reciprocal. For example, if the English home page has lang="en" and points to a German version with hreflang="de", the German page must have lang="de" and send back hreflang="en". If that chain breaks, Google may ignore the alternate page. We explain how to find and fix reciprocity errors in our article on hreflang return tag errors.
For the general error patterns in international SEO, our international SEO guide covers the common cases. When to use the x-default value on alternate links is a separate question, and it is covered in our x-default article.
Common mistakes
- A constant value from the template: every page is marked
lang="en". In multilingual CMS and framework setups this is the most frequent error. - Language and content do not match: Turkish content is served with
lang="en". Translation prompts and screen reader pronunciation then work from the wrong language. - Underscores: values such as
tr_TRoren_US. The separator is always a hyphen. - Unneeded region codes:
lang="en-US"is valid, but if there is no US-specific content,enis simpler and more consistent. What matters is using one form across the site. - Unmarked foreign passages: English blocks sit inside a page that declares only the main language. This goes against criterion 3.1.2.
- Stale language after a switch: in a single page application the
<html>value stays the same after the language changes. - Mismatched lang and hreflang: a page declares
lang="de"but advertisesde-ATin hreflang. If there is no Austria-specific content, both values should bede.
Checklist for multilingual sites
Running these checks on a sample set of pages before and after publishing catches most problems early:
- Does every page have a valid lang value on its
<html>element? - Does the value match the main content language of the page?
- Are foreign-language passages inside the page marked with lang?
- Are alternate pages connected to each other with reciprocal hreflang?
- Does the template avoid producing a fixed lang value?
- Does the lang value change when the language changes?
- If region codes are used, are they applied in the same form across the site?
A practical method is to open the source of a page in the browser and read the <html> tag on the first line. This does not scale to thousands of pages, but checking one example from each language version is a good way to confirm that the template works.
Conclusion and next step
The lang attribute declares the language of a page. hreflang tells search engines which language and region versions of that page exist. The two do different jobs, and when both are correct, accessibility and international SEO both benefit. As a next step, open your main template, confirm that the value on the html element is generated correctly for each language, and run a sample page audit with the checklist above. If you would like to review how this should be set up in your multilingual structure, you are welcome to get in touch with us.




No comments yet
Leave a Comment