The lang Attribute
The lang Attribute
Section titled “The lang Attribute”The lang attribute specifies the language of an element’s content. It helps search engines, screen readers, translation tools, and browsers provide appropriate language-specific features like spell-checking and hyphenation.
Syntax
Section titled “Syntax”<element lang="language-code">Content</element>Values
Section titled “Values”| Value | Description |
|---|---|
language-code | A valid BCP 47 language tag (e.g., en, es, fr-CA, zh-Hans) |
Examples
Section titled “Examples”Document Language
Section titled “Document Language” Result
Mixed Language Content
Section titled “Mixed Language Content” Result
Quotes in Different Languages
Section titled “Quotes in Different Languages” Result
Regional Variants
Section titled “Regional Variants” Result
Common Language Codes
Section titled “Common Language Codes”Major Languages
Section titled “Major Languages”| Code | Language | Example |
|---|---|---|
en | English | <p lang="en">Hello</p> |
es | Spanish | <p lang="es">Hola</p> |
fr | French | <p lang="fr">Bonjour</p> |
de | German | <p lang="de">Hallo</p> |
it | Italian | <p lang="it">Ciao</p> |
pt | Portuguese | <p lang="pt">Olá</p> |
ru | Russian | <p lang="ru">Привет</p> |
ja | Japanese | <p lang="ja">こんにちは</p> |
zh | Chinese | <p lang="zh">你好</p> |
ar | Arabic | <p lang="ar">مرحبا</p> |
ko | Korean | <p lang="ko">안녕하세요</p> |
hi | Hindi | <p lang="hi">नमस्ते</p> |
Regional Variants
Section titled “Regional Variants”| Code | Language & Region |
|---|---|
en-US | English (United States) |
en-GB | English (United Kingdom) |
en-AU | English (Australia) |
es-ES | Spanish (Spain) |
es-MX | Spanish (Mexico) |
fr-FR | French (France) |
fr-CA | French (Canada) |
pt-BR | Portuguese (Brazil) |
pt-PT | Portuguese (Portugal) |
zh-Hans | Chinese (Simplified) |
zh-Hant | Chinese (Traditional) |
Accessibility Benefits
Section titled “Accessibility Benefits”Screen Reader Pronunciation
Section titled “Screen Reader Pronunciation” Result
Voice Selection
Section titled “Voice Selection”Screen readers use the lang attribute to:
- Select the appropriate voice or pronunciation rules
- Apply correct intonation and stress patterns
- Pronounce special characters properly
Browser Features
Section titled “Browser Features”Spell Checking
Section titled “Spell Checking” Result
Translation Tools
Section titled “Translation Tools”Browsers use the lang attribute to:
- Offer automatic translation when content language differs from browser language
- Improve translation accuracy by knowing source language
- Skip translation for specific elements
<!-- Tell translation tools not to translate this element --><p lang="en" translate="no"> Product code: SKU-12345</p>Font Selection
Section titled “Font Selection”Browsers may select different fonts based on language:
<!-- Japanese text --><p lang="ja">日本語のテキスト</p>
<!-- Chinese text (may use different glyphs) --><p lang="zh">中文文本</p>
<!-- Korean text --><p lang="ko">한국어 텍스트</p>SEO Benefits
Section titled “SEO Benefits”Search Engine Understanding
Section titled “Search Engine Understanding”Search engines use lang to:
- Index content in the appropriate language database
- Show content to users searching in that language
- Understand regional targeting
<!DOCTYPE html><html lang="en-US"><head> <meta charset="UTF-8"> <title>US-focused content</title></head><body> <!-- Content optimized for US English speakers --></body></html>Hreflang and International SEO
Section titled “Hreflang and International SEO”Combined with hreflang for multi-language sites:
<!-- English version --><html lang="en"><head> <link rel="alternate" hreflang="es" href="https://example.com/es/"> <link rel="alternate" hreflang="fr" href="https://example.com/fr/"></head>
<!-- Spanish version --><html lang="es"><head> <link rel="alternate" hreflang="en" href="https://example.com/"> <link rel="alternate" hreflang="fr" href="https://example.com/fr/"></head>Best Practices
Section titled “Best Practices”Always Set on HTML Element
Section titled “Always Set on HTML Element”<!-- Good: Language set on root element --><!DOCTYPE html><html lang="en"> <!-- page content --></html>
<!-- Avoid: No language specified --><!DOCTYPE html><html> <!-- page content --></html>Use Specific Codes When Needed
Section titled “Use Specific Codes When Needed”<!-- Generic when region doesn't matter --><html lang="es">
<!-- Specific when targeting a region --><html lang="es-MX"> <!-- Mexican Spanish --><html lang="es-ES"> <!-- Castilian Spanish -->Mark Language Changes
Section titled “Mark Language Changes”<article lang="en"> <p>In French, hello is <span lang="fr">bonjour</span>.</p> <p>The German word is <span lang="de">hallo</span>.</p></article>Don’t Overuse
Section titled “Don’t Overuse”<!-- Avoid: Unnecessary language tags --><p lang="en"> <span lang="en">This</span> <span lang="en">is</span> <span lang="en">excessive</span>.</p>
<!-- Better: One tag on container --><p lang="en"> This is better.</p>Common Mistakes
Section titled “Common Mistakes”Invalid Language Codes
Section titled “Invalid Language Codes”<!-- Wrong: Not a valid BCP 47 code --><html lang="english"><html lang="EN"><html lang="en_US">
<!-- Correct --><html lang="en"><html lang="en-US">Missing Lang on HTML
Section titled “Missing Lang on HTML”<!-- Problematic: Screen readers won't know the language --><!DOCTYPE html><html><body> <p>Content without language specification</p></body></html>
<!-- Better --><!DOCTYPE html><html lang="en"><body> <p>Content with language specified</p></body></html>Wrong Language Code
Section titled “Wrong Language Code”<!-- Wrong: Using wrong code --><p lang="sp">Spanish text</p> <!-- 'sp' is not Spanish -->
<!-- Correct --><p lang="es">Spanish text</p> <!-- 'es' is Spanish -->Dynamic Language Switching
Section titled “Dynamic Language Switching” Result
Browser Support
Section titled “Browser Support”The lang attribute is supported by all browsers.
| Browser | Support |
|---|---|
| Chrome | All versions |
| Firefox | All versions |
| Safari | All versions |
| Edge | All versions |