Development & testing

Missing page language: setting the lang attribute correctly

“html-has-lang” and “html-lang-valid” explained: why screen readers need the page language, which codes are valid and how to set them in HTML and WordPress.

5 min readUpdated: Accessonaut editorial team Published on

In short

The axe messages “<html> element must have a lang attribute” (html-has-lang) and “<html> element must have a valid value for the lang attribute” (html-lang-valid) concern the same spot: the main language of the page is missing or given with an invalid code. Screen readers may then choose the wrong pronunciation. The fix is usually a single line such as <html lang="en">. Mark up passages in other languages with their own lang attribute as well.

What the two messages mean

html-has-lang reports an <html> element without a lang attribute or with an empty value. html-lang-valid reports a value that is not a valid language code, such as lang="english", lang="german" or lang="en_GB" with an underscore. Both rules belong to WCAG success criterion 3.1.1 “Language of Page” (Level A).

The related rule valid-lang checks lang attributes within the page, for example on a quotation in another language; it belongs to 3.1.2 “Language of Parts” (Level AA). html-xml-lang-mismatch reports XHTML pages where lang and xml:lang state different languages.

Who a wrong language setting affects

Screen readers use the language setting to choose the voice and pronunciation rules. If it is missing, they fall back to the system language. German text read with English pronunciation, or the other way round, is barely understandable, and the problem affects every page of your website at once. Braille displays may use different translation tables depending on the language.

The browser uses the setting too: automatic hyphenation with the CSS property hyphens: auto needs the correct language, as do spell checking in form fields and translation features.

Valid language codes

The lang attribute contains a BCP 47 language tag. For English, "en" is enough; with a region, "en-GB" or "en-US" are possible, for German "de", "de-DE", "de-AT" or "de-CH", for Spanish "es". The region is optional and only useful when it makes a difference.

The underscore form such as de_DE is a locale identifier from programming languages and content management systems, not a valid HTML language tag. An empty lang="" sets the language to “unknown” and triggers the message as well.

How to set the language

The main language goes on the root element: <!DOCTYPE html> <html lang="en">. Mark up passages in another language on the surrounding element, for example <p>Our motto: <span lang="de">Einfach machen</span>.</p>. According to the Understanding document for 3.1.2, proper names, technical terms and words that have become part of the surrounding language do not need their own markup.

On multilingual websites, every language version gets its own lang attribute. Entries in the language switcher are written in their own language and carry the matching attribute, for example <a href="/de/" lang="de">Deutsch</a>.

WordPress, shop systems and frameworks

In WordPress, the theme should output the opening tag in its template as <html <?php language_attributes(); ?>>. The function sets the language from “Settings > General > Site Language”, for example lang="en-GB". If the attribute is missing, the theme usually has a fixed <html> without this function; the fix belongs in a child theme or with the theme vendor. Multilingual plugins set the language per version.

In frameworks, the attribute sits in the central layout, in Next.js for example in the root layout. In single-page applications that switch language without reloading, the script must also update document.documentElement.lang. In shop systems, the <html> tag lives in the theme’s main template.

Common pitfalls

  • Wrong but valid language: a German website with lang="en" from an English template passes the automated test but is read out incorrectly. You can only spot this by looking.
  • Embedded content: iframes are separate documents with their own language setting, which you may have to configure with the provider.
  • Overridden templates: landing pages, maintenance pages and error pages often use their own templates without a lang attribute.
  • Documents: PDFs and other files have their own language settings and do not inherit the website’s lang attribute.

Checking the fix

Type document.documentElement.lang into the browser console; the result should match the language of the page. Run the website check again for every language version and for different templates such as the home page, blog posts and shop pages. Finally, listen to a paragraph with a screen reader and check that pronunciation and hyphenation are right.

Sources & review status

Here you’ll find the original sources for this article. Last checked on .

Legal requirements depend on the country, the provider and the specific offer. This article explains the basics and practical steps. Test results apply to their documented scope and date.

More on Development & testing

WCAG 2.2 AA: The practical checklist

Law & standards5 min read

The most important WCAG topics for your website: clear text, contrast, keyboard, forms and the new requirements in WCAG 2.2.

Your next step

What does your website need?

Check a public page for automatically detectable barriers. The results show specific findings and help you prioritise.

Free website check

A sensible testing process

  • Find automatically detectable issues
  • Test the keyboard and real journeys manually
  • Fix, re-check and document
Guide to testing your website