lang

Declare the language of content. Screen readers use it to switch pronunciation, and browsers use it for hyphenation, spell-checking, and font selection.

Overview

The lang attribute declares the language of an element's content. It is one of the most impactful accessibility attributes — screen readers use it to switch pronunciation engines, and browsers use it for hyphenation, spell-checking, quote marks, and font selection.

Set lang on the <html> element for the page's primary language, and on inline elements when the language changes mid-content.

Language Codes

The value is a BCP 47 language tag. The most common codes:

CodeLanguage
enEnglish
esSpanish
frFrench
deGerman
jaJapanese
zhChinese
arArabic
hiHindi
koKorean
ptPortuguese
ruRussian
itItalian

Regional Variants

Add a region subtag to distinguish dialects. The browser inherits the base language, so en-GB still matches :lang(en) in CSS.

CodeVariant
en-USAmerican English
en-GBBritish English
pt-BRBrazilian Portuguese
pt-PTEuropean Portuguese
zh-HansSimplified Chinese
zh-HantTraditional Chinese
es-MXMexican Spanish
fr-CACanadian French

Page Language

Every page should declare its primary language on the <html> element. This is the single most important use of lang.

<html lang="en"> <head> <meta charset="utf-8" /> <title>My Page</title> </head> <body> <p>This page is declared as English.</p> </body> </html>

Inline Language Switches

When content in a different language appears inside the page, wrap it in an element with the appropriate lang. This tells screen readers to switch pronunciation — without it, a screen reader using English rules will mangle French, Japanese, or any other language.

<p>The French word <span lang="fr">bonjour</span> means "hello".</p> <p>In Japanese, thank you is <span lang="ja">ありがとう</span>.</p> <blockquote lang="de"> <p>Die Grenzen meiner Sprache bedeuten die Grenzen meiner Welt.</p> <footer>— Ludwig Wittgenstein</footer> </blockquote>

The French word bonjour means "hello".

In Japanese, thank you is ありがとう.

Die Grenzen meiner Sprache bedeuten die Grenzen meiner Welt.

— Ludwig Wittgenstein

Effects of lang

Setting the correct lang value affects more than you might expect:

Hyphenation

The CSS hyphens: auto property requires a correct lang attribute to know where words can be broken. Without it, automatic hyphenation will not work.

<style> .hyphenated { hyphens: auto; max-width: 20ch; } </style> <!-- hyphens: auto requires the correct lang to know where to break --> <p lang="en" class="hyphenated"> This paragraph demonstrates automatic hyphenation. </p> <p lang="de" class="hyphenated"> Donaudampfschifffahrtsgesellschaftskapitän </p>

Screen Reader Pronunciation

Screen readers maintain pronunciation dictionaries for each language. When lang changes, the screen reader switches to the appropriate voice and pronunciation rules. Without this, "bonjour" would be read with English phonetics.

Spell-Checking

Browsers use lang to select the correct spell-check dictionary. A <textarea lang="fr"> will check spelling against French, not the page's default language.

Font Selection

When the primary font lacks glyphs for a language's characters, the browser uses lang to select the best fallback. This is especially important for CJK (Chinese, Japanese, Korean) text, where the same Unicode codepoint can render differently depending on language.

Quote Marks

The <q> element generates language-appropriate quotation marks based on the inherited lang value.

<p lang="en">She said, <q>Hello!</q></p> <p lang="fr">Elle a dit, <q lang="fr">Bonjour!</q></p> <p lang="de">Sie sagte, <q lang="de">Hallo!</q></p>

She said, Hello!

Elle a dit, Bonjour!

Sie sagte, Hallo!

CSS :lang() Pseudo-Class

The :lang() pseudo-class matches elements by their computed language, including inherited values. It is more reliable than [lang="..."] attribute selectors because it accounts for inheritance.

<style> /* Style quotes differently by language */ :lang(en) > q { quotes: "\201C" "\201D" "\2018" "\2019"; } :lang(fr) > q { quotes: "\00AB\00A0" "\00A0\00BB"; } :lang(de) > q { quotes: "\201E" "\201C"; } /* Different font for CJK languages */ :lang(ja), :lang(zh), :lang(ko) { font-family: "Noto Sans CJK", sans-serif; } /* Increase line height for scripts that need more vertical space */ :lang(ar), :lang(hi), :lang(th) { line-height: 1.8; } </style>

Code and Non-Language Content

Programming code is not natural language. Setting lang="" (empty string) signals that the content has no linguistic meaning, preventing screen readers from attempting to pronounce variable names as words.

<!-- Code should not be read as natural language --> <pre><code lang="">const greeting = "Hello, world!";</code></pre> <!-- Or use translate="no" alongside lang --> <p>Run <code lang="" translate="no">npm install</code> to install dependencies.</p>

See Also

  • dir — set text direction for RTL languages
  • translate — control whether content should be translated
  • <html> — where to set the page language
  • <q> — inline quotations with language-aware quote marks