Step by Step

The HTML lang attribute: which value to use for each language, and which foreign words to mark

7 min read
Mikhail ShaymardanovWeb Quality and Accessibility Expert

A site created with create-next-app or Vite's React template gets lang="en" on its html tag, whatever its language. That attribute tells screen readers which language to read in: on a German page, en asks NVDA to read German as English. Checkers like axe and Lighthouse let it pass: they only check that it's valid. Set it to the page's language, then add a lang to each passage in another language.

Declaring a page's language


#What does the lang attribute on the html tag do?

The lang attribute declares the language of the text, and a screen reader uses it to pick its pronunciation rules. Its first users are people who listen to the page, or who read it with their fingertips on a braille display. The W3C, which writes web standards, lists other users: translation tools, spell checkers and automatic hyphenation.

With NVDA, the free screen reader for Windows, three rules decide which voice speaks:

  • the <html> tag carries the page's main language, and every element inherits it;
  • NVDA switches languages when a piece of text declares its own, if its “Automatic language switching” checkbox is checked, as it is by default;
  • it only switches if the current speech synthesizer, the engine that speaks the text, supports that language.

eSpeak NG, the synthesizer that comes with NVDA, speaks English and German out of the box.

Our demo, the Wavebreak Folk Festival, publishes its homepage in English and in German. The site was started with create-next-app, and both versions share one layout file, app/layout.tsx, which still says lang="en". We had eSpeak NG, run on its own outside NVDA, read the German welcome sentence with the English voice that en calls for. The “Avoid” line below plays the result. The dates “3”, “5” and “30” come out as “three”, “five” and “thirty”, and “Sie” sounds like “sigh”. Numbers have no language of their own, so the voice reads them as words of its own language.

Screenshot: the top of the Wavebreak Folk Festival's German homepage, with the title “Drei Abende Folk am Hafen” in big hand-drawn letters, the welcome sentence and a red “Tickets buchen” button.Marker 1. The German welcome sentence, with its three dates written as numbers.
On the Wavebreak festival's German homepage, the welcome sentencemarker 1 looks the same whatever the lang value; with “en”, eSpeak NG reads its dates as “three”, “five” and “thirty”.

Avoid

// app/layout.tsx, simplified from the template, shared by /en and /de
export default function RootLayout({ children }: LayoutProps<"/">) {
  return (
    <html lang="en">
      <body>{children}</body>
    </html>
  );
}

Screen reader: « Drei Abende Konzerte am Hafen, vom 3. bis 5. Juli. Buchen Sie Ihre Tickets bis zum 30. Juni. »

Correct

// app/[lang]/layout.tsx, simplified: the language comes from the URL
export default async function RootLayout({ children, params }: LayoutProps<"/[lang]">) {
  const { lang } = await params; // "en" or "de"
  return (
    <html lang={lang}>
      <body>{children}</body>
    </html>
  );
}

#Why don't axe and Lighthouse flag lang="en" on a German page?

Both tools only check that lang exists and that its value is a valid code: no axe rule compares it with the language of the text. On the Wavebreak festival's German homepage, the axe-core 4.14 testing library finds no error, though it does flag a misspelled code like de_DE. Lighthouse, Google's audit tool, runs those same axe rules.

WCAG, the international accessibility guidelines, ask for more: their Language of Page criterion requires that software can tell the page's actual language. The W3C's test for the html element, technique H57, checks that the value reflects the language the page is mainly written in. en on German text fails that test. In Europe, the accessibility standard EN 301 549 requires that criterion on every web page. It was written as one way to meet the European Accessibility Act.

Google Lighthouse accessibility score: what a 100 counts, and what to test by hand

#Which value should you use, en or en-US, and where does it go?

The value goes on the <html> tag, and the shortest one does the job: en for an English page, de for a German one.

#Should lang go on the html or the body tag?

On <html>: every element on the page inherits it. The W3C recommends it because <body> leaves out the <head>, which holds the page title shown in the browser tab, “Wavebreak Folk Festival: three nights of folk on the harbor”.

With Next.js, the tag lives in app/layout.tsx, the root layout that wraps every page and must define <html> and <body>. With Vite's React and Vue templates, it's written in index.html. A site with several language versions needs one value per version: the Next.js internationalization guide moves the root layout into app/[lang]/layout.tsx and takes the value from the URL.

#Which values are valid: en, en-US, en-GB or en_US?

The value is a BCP 47 tag, the web's standard for language codes: the language, then an optional region after a hyphen. The W3C asks for the shortest tag possible, with a region only when it helps, such as British spelling for a spell checker.

Valid values for a page in English
ValueWhen to use it
enBy default: the shortest
en-US
en-us
EN
Valid too: upper or lower case makes no difference
en-GBWhen the region helps: British spelling, the W3C's example
Values axe-core 4.14 rejects on a page in English
ValueWhat's wrong
en_USAn underscore where the hyphen goes
englishThe name of the language, which isn't a code

A German page works the same way: de, never de_DE or german.

#How do you check a page's language, in the console and with NVDA?

  1. In the browser console, type document.documentElement.lang: the answer is the value the framework actually rendered. On the Wavebreak festival's German homepage, we get 'en', then 'de' after the fix.
  2. In NVDA (setup guide), NVDA+Ctrl+S opens the synthesizer choice: pick eSpeak NG. If you stay with Windows OneCore, NVDA's default voices, first add a Windows voice for English and for each language your pages use, then restart NVDA.
  3. NVDA+Ctrl+V opens the speech settings: leave the “Automatic language switching” checkbox checked.
  4. Have NVDA read a sentence with a number in it: on a page in any language but English, a “three” means the page is still on en.
Screenshot: the console of Chrome's developer tools, in English. Under the command document.documentElement.lang, the console shows the answer 'en'.Marker 1. The console's answer, 'en'.
In Chrome's console, document.documentElement.lang answers 'en'marker 1 on the Wavebreak festival's German homepage: the page kept the value from the create-next-app template.
NVDA screen reader: how to install it, set it up and test a web page

#Do you need a lang attribute on every foreign word in an English page?

No: lang is only required on passages in another language that no exception covers, whether a whole sentence or a single word. That language change is declared on the element around the passage, or on a span added around it.

WCAG's Language of Parts criterion, 3.1.2, exempts four kinds of words. They are proper names, technical terms, words whose language can't be determined, and words that have become part of the surrounding language. The W3C's example of the last kind is “rendezvous”, a French word that appears in English dictionaries and that English screen readers pronounce properly.

When in doubt, the W3C suggests asking whether the word would be pronounced the same, accent and intonation aside, in the language of the text around it. Marking a proper name or adopted word is still allowed, and even encouraged for accurate pronunciation.

#Which foreign words get a lang on the Wavebreak homepage?

On the festival's English homepage, we picked four items that come from another language. Two need a fix: the press quote and the “Deutsch” link.

Screenshot: the Wavebreak Folk Festival's homepage in English. A menu ending with the link Deutsch, the title “Three nights of folk on the harbor” beside a photo of the pier, a green band with a quote in German, a lineup of three bands, then the late session, the newsletter sign-up and the program to download.Marker 1. The German press quote, “Der schönste Folkabend des Sommers.”. Marker 2. The “Deutsch” link to the German version, at the end of the menu. Marker 3. The line “Rendezvous on the pier after the last set”. Marker 4. The band name “Die Seezeichen”.
On the Wavebreak festival's English homepage, the German quotemarker 1 and the “Deutsch” linkmarker 2 need a fix; “rendezvous”marker 3 and the band Die Seezeichenmarker 4 need no lang.
To mark with lang: two items on the homepage
ItemFix
marker 1 Quote “Der schönste Folkabend des Sommers.”<blockquote lang="de">
marker 2 “Deutsch” link<a lang="de">
Optional lang: two words on the homepage
WordWhy
marker 3 “rendezvous”Adopted in English: the W3C's example
marker 4 “Die Seezeichen”Proper name

The quote, a whole German sentence meaning “the finest folk evening of the summer”, gets lang="de" on its blockquote. The “Deutsch” link gets lang="de" too, as in the W3C's own example: links to other language versions, each named and marked in its own language. Neither fix changes anything on screen. The “Avoid” line below plays the quote in eSpeak NG's English voice.

Screenshot: on a green band, the quote “Der schönste Folkabend des Sommers.” in large cream letters, credited to Kielwasser, German folk magazine.Marker 1. The German quote, “Der schönste Folkabend des Sommers.”.
Without lang, eSpeak NG's English voice reads the quotemarker 1 with English rules, and “Folkabend” turns into “folk-ay-bend”; with “de”, NVDA asks for the German voice for the whole sentence.

Avoid

<blockquote>
  <p>Der schönste Folkabend des Sommers.</p>
</blockquote>

Screen reader: « Der schönste Folkabend des Sommers. »

Correct

<blockquote lang="de">
  <p>Der schönste Folkabend des Sommers.</p>
</blockquote>

#Is listening enough to decide which words to mark?

Not for single words: by ear, the Wavebreak festival's homepage suggests the opposite of WCAG's rule. With no lang at all, eSpeak NG's English voice already says “Deutsch” much as a German speaker would, since its English spelling rules read “eu” after a “d” as “oy”. The band name Die Seezeichen comes out mangled: “Die” sounds like the English word “dye”, and “Seezeichen” turns into “see-zitchen”. An ear alone would mark the band and leave the link.

The rule asks for the reverse: a fix for the link, nothing required for the band. What the ear hears comes from one synthesizer's rules, while the attribute serves every synthesizer, and braille translation too. Use your ears to check the page's value, and the rule to decide about single words.

The fixed English homepage has three lang attributes: en on <html>, then de on the quote and on the “Deutsch” link. The German homepage takes de from its URL and puts lang="en" on its “English” link. There, the console answers 'de', and the dates are read in German.


The Includdy scan flags a missing or invalid lang, en_US included. Its guided fix suggests corrected code, generated by AI, for you to review before it goes into your site. Its manual tests then ask whether the language matches the content, and suggest a verdict that you confirm or change.


Share this article

Frequently asked questions

Not on Google: it works out a page's language from its visible content, and its documentation on multi-regional sites says it doesn't use the lang attribute.

The attribute is there for screen readers first, which pick their pronunciation rules from it. The people listening to the page are the ones who hear the difference.

lang declares the language of the text on the page itself. hreflang goes on a link and names the language of the page that link points to; the HTML specification calls it “purely advisory”, a hint only.

Google reads hreflang to understand that several pages are localized versions of the same content, and it uses neither attribute to detect a page's language. On a site with several language versions, each page keeps its own lang, and hreflang tells Google how the versions connect.

No. The W3C says never to use <meta http-equiv="Content-Language"> to give a page's language, and the HTML specification no longer allows it. The HTTP header of the same name, sent by the server, can signal the language of the audience a page is meant for.

The language of the text is always declared with the lang attribute on the html tag, with or without that header.