日本語

‘Lighthouse: fixing the missing lang attribute on the html element’

​​Lighthouse​Accessibility,HTML,Troubleshooting

When the Lighthouse accessibility audit reports "<html> element does not have a [lang] attribute" (the Japanese-language report shows a translated version of this message), the page language is not set in the HTML. For a Japanese page, change the opening tag to <html lang="ja"> and the warning goes away. The attribute tells screen readers which language to use when reading the page aloud.

In my test environment, I confirmed that Lighthouse reports the warning for a page without lang, and that adding lang="ja" clears it. I also tried typos such as lang="jp" and lang="ja_JP", and cases where the language was given with a meta tag or an HTTP header.

Note

The test environment was Ubuntu 24.04, Node.js 22.23.4, and Lighthouse 13.6.0 (the CLI installed with npm; the axe-core it uses internally is 4.14.0). The browser was headless Google Chrome for Testing 153. I ran only the accessibility category (--only-categories=accessibility) with the default mobile settings. I created the pages in the test environment and served them from 127.0.0.1. I did not test the Lighthouse panel in Chrome DevTools, PageSpeed Insights, or actual screen reader output.

Page that triggers the warning

The test page is a Japanese page with one heading and one paragraph. The <html> element has no lang.

html
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>lang の検証(直す前)</title>
</head>
<body>
<main>
<h1>今日の猫</h1>
<p>窓辺で寝ているところです。</p>
</main>
</body>
</html>

I served this page with python3 -m http.server and ran Lighthouse as follows:

bash
npx lighthouse http://127.0.0.1:8000/bad.html \
  --only-categories=accessibility \
  --output=json --output=html --output-path=./out/bad \
  --chrome-flags="--headless=new --no-sandbox" --quiet

In the HTML report, this audit appears under the "INTERNATIONALIZATION AND LOCALIZATION" section. Opening it shows <html> under "Failing Elements".

Lighthouse report opened on the "<html> element does not have a [lang] attribute" audit. One html element is listed under Failing Elements below the description
Report for a page without lang. The html element is flagged

According to the description, if a page has no lang attribute, screen readers assume the page is written in the default language the user has set. If the actual language is different, the text may not be read correctly.

From the JSON report, here are the accessibility score and the results of the four language-related audits:

text
accessibility score: 85
[html-has-lang] weight=7 score=0 (binary)
  title: `<html>` element does not have a `[lang]` attribute
  - html | <html>
      Fix any of the following:
        The <html> element does not have a lang attribute
[html-lang-valid] weight=0 score=null (notApplicable)
  title: `<html>` element has a valid value for its `[lang]` attribute
[html-xml-lang-mismatch] weight=0 score=null (notApplicable)
  title: `<html>` element has an `[xml:lang]` attribute with the same base language as the `[lang]` attribute.
[valid-lang] weight=0 score=null (notApplicable)
  title: `[lang]` attributes have a valid value
failed accessibility audits: html-has-lang

Only one audit failed: html-has-lang. The other three check the value of lang on <html>, whether lang and xml:lang match, and lang on elements other than <html>. This page has nothing for them to check, so they were marked "not applicable" (notApplicable). The pass or fail decision comes from the html-has-lang rule in axe-core, which Lighthouse uses internally.

Adding lang="ja" to html

The fix changes only the opening tag:

html
<html lang="ja">

After I ran Lighthouse again under the same conditions, html-has-lang passed and the score became 100.

text
accessibility score: 100
[html-has-lang] weight=7 score=1 (binary)
  title: `<html>` element has a `[lang]` attribute
[html-lang-valid] weight=7 score=1 (binary)
  title: `<html>` element has a valid value for its `[lang]` attribute
[html-xml-lang-mismatch] weight=0 score=null (notApplicable)
  title: `<html>` element has an `[xml:lang]` attribute with the same base language as the `[lang]` attribute.
[valid-lang] weight=0 score=null (notApplicable)
  title: `[lang]` attributes have a valid value
failed accessibility audits: (none)

Once lang is present, html-lang-valid, which checks whether the value is correct, is also included in the score, and it passed. As described on MDN's lang attribute page, the value of lang is a BCP 47 language tag. The tag for Japanese is ja.

A wrong value triggers a different warning

Even when lang is present, an incorrect value triggers the html-lang-valid audit with the message "<html> element does not have a valid value for its [lang] attribute." I changed only the value and got these results:

<html> markup Failed audit Score
no lang html-has-lang 85
lang="" (empty) html-has-lang 85
lang="jp" html-lang-valid 87
lang="japanese" html-lang-valid 87
lang="ja_JP" html-lang-valid 87
lang="ja" none 100
lang="ja-JP" none 100
Lighthouse report opened on the "<html> element does not have a valid value for its [lang] attribute." audit. html lang="jp" is listed under Failing Elements
Report for a page with lang="jp". An incorrect value is flagged

For lang="jp", the reason was "Value of lang attribute not included in the list of valid languages". axe-core takes the first part of the lang value (the part before -) and checks it against the list of language codes that axe-core holds. This list is based on ISO 639-1/2 language codes, and the code for Japanese is ja. Neither jp nor japanese is on the list, so both failed.

ja_JP, with an underscore, also failed. axe-core splits the language part at -. With an underscore, the whole value is lowercased to ja_jp and treated as one language code, which is not on the list. To include a region, use a hyphen, as in ja-JP.

lang="" was treated the same as having no lang at all. axe-core's has-lang check considers lang missing even when the value contains only whitespace. MDN also says that an empty value makes the language "unknown."

On the other hand, a Japanese page with lang="en" still scored 100. Lighthouse checks whether the value is a valid language code. It does not check whether the text on the page is actually written in that language. For a Japanese page, use ja.

A meta tag or HTTP header does not clear it

MDN's lang attribute page says that if no ancestor element specifies a language, the language can also be given with <meta http-equiv="content-language"> or the HTTP Content-Language header. I tried both on a page without lang:

  • Put <meta http-equiv="content-language" content="ja"> in <head>
  • Have the server return a Content-Language: ja header

In both cases, html-has-lang still failed, and the score was 85. The has-lang check in axe-core looks only at the lang and xml:lang attributes on <html>. To clear the Lighthouse warning, you need to write lang on <html>.

I also tried a page whose opening tag had no lang, where a <script> in the page added document.documentElement.lang = "ja" afterward. This passed, and the score was 100. The attribute does not have to be in the opening tag. If <html> has a lang at the time Lighthouse checks it, the audit passes.

When only part of the page is in another language

If you put an English paragraph inside a Japanese page, add lang to that paragraph. A wrong value there is flagged by the valid-lang audit, which is separate from the one for <html>. On a page with <p lang="english">The cat is sleeping.</p>, the score was 89.

text
[valid-lang] weight=7 score=0 (binary)
  title: `[lang]` attributes do not have a valid value
  - body > main > p | <p lang="english">
      Fix all of the following:
        Value of lang attribute not included in the list of valid languages

Changing it to <p lang="en"> passed, and the score was 100.

If <html> also has xml:lang, and the two values differ, as in <html lang="ja" xml:lang="en">, the html-xml-lang-mismatch audit fails and the score was 95. Remove xml:lang or make it match lang.

How much the score drops

In the default configuration file (default-config.js) for Lighthouse 13.6.0, the accessibility score weights are 7 for html-has-lang, html-lang-valid, and valid-lang, and 3 for html-xml-lang-mismatch.

The accessibility score is the share of the total weight of the audits included in the calculation that passed. The test page has little content, so the total weight of the audits in the calculation was 47. Without lang, the page loses the 7 from html-has-lang, leaving 40, and the score is 85 (40 ÷ 47). Because the score is a ratio of weights, a page with a larger total weight drops less when it loses the same 7 points. The score of 87 for lang="jp" is slightly higher because html-lang-valid joins the calculation. That brings the total weight to 54 and the passed weight to 47.

​Posted by 86