HTML lang Attribute

Beginner
⏱️ 6 min read
📚 Updated: Jun 2026
🎯 3 Examples
Accessibility & i18n

Introduction

The lang attribute in HTML is used to specify the language of the content within an element. It plays a crucial role in making web content more accessible by telling browsers and assistive technologies which language to use for pronunciation, hyphenation, and translation. The attribute is most commonly applied to the <html> tag to define the default language for the entire document.

What You’ll Learn

01

Global Attribute

Works on any element.

02

BCP 47 Codes

en, es, en-US.

03

<html lang>

Document default.

04

Mixed Content

Nested overrides.

05

lang vs hreflang

Content vs links.

06

JS lang

Switch at runtime.

Purpose of lang

The primary purpose of the lang attribute is to assist browsers and other user agents in correctly rendering and interpreting text. Screen readers use it to pick the right voice and pronunciation rules. Search engines use it to understand page language for ranking and regional results. Spell-checkers and hyphenation tools also rely on lang.

Without a document-level lang, assistive technology may guess the language incorrectly—which leads to poor pronunciation and a frustrating experience for users.

💡
Always set lang on <html>

Every page should declare its primary language on the root element, for example <html lang="en">. Add lang on inner elements only when a section uses a different language.

📝 Syntax

Set the default document language on the root <html> element:

lang.html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>My Web Page</title>
</head>
<body>
  <!-- page content -->
</body>
</html>

Syntax Rules

  • lang is a global attribute—valid on every HTML element.
  • Value is a BCP 47 language tag: primary language subtag, optionally followed by region or script (e.g. en-US, zh-Hans).
  • Use lowercase language codes (en, not EN) and proper region casing (en-GB).
  • Set once on <html> for the document default; override on nested elements for mixed-language passages.
  • Empty string lang="" means unknown language—avoid unless intentional.
  • In JavaScript, use the lang property: document.documentElement.lang = "es".

💎 Values

The lang attribute uses BCP 47 language tags. Common examples:

  • en — English (generic).
  • en-US — English as used in the United States.
  • en-GB — English as used in the United Kingdom.
  • es — Spanish.
  • fr — French.
  • de — German.
  • ja — Japanese.
  • zh-Hans — Chinese, simplified script.
  • pt-BR — Portuguese (Brazil).

Read or set the property in JavaScript:

lang-js.html
document.documentElement.lang = "es";
document.querySelector("blockquote").lang = "fr";

⚡ Quick Reference

Use CaseMarkupNotes
Document language<html lang="en">Required best practice
Regional variantlang="en-US"Language + region
Foreign quote<q lang="es">...</q>Nested override
Unknown languagelang=""Rare; disables language hint
JavaScript (page)document.documentElement.langUpdates <html>
JavaScript (element)element.langAny element

Applicable Elements

Element / ContextSupported?Notes
<html>YesDocument default—always set
<p>, <span>, headingsYesMixed-language paragraphs
<a>, <li>, <td>YesAny element with text
<link hreflang>Different attributehreflang for alternate URLs
<track srclang>Different attributesrclang on media text tracks

lang vs hreflang

Featurelanghreflang
PurposeLanguage of content on this page/elementLanguage of an alternate page URL
Used onAny element (especially <html>)<link>, <a> (alternate links)
AudienceBrowsers, screen readers, spell-checkSearch engines (international SEO)
Example<html lang="en"><link rel="alternate" hreflang="es" href="...">
Both needed?Often yes on multilingual sites—they solve different problems

Examples Gallery

Document-level English, mixed-language content with nested lang, and switching language with JavaScript.

👀 Live Preview

Mixed-language paragraph: English page with a Spanish phrase marked inline:

Welcome to our site. The motto is “Aprender cada día” — learn every day.

The outer block is lang="en"; the quote uses lang="es" so screen readers pronounce Spanish correctly.

Example — Document Default Language

Here’s how to use the lang attribute on the <html> tag:

lang.html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>My Web Page</title>
</head>
<body>
  <!-- Your content goes here -->
</body>
</html>
Try It Yourself

How It Works

The lang attribute on <html> sets the default language for the whole page. It is inherited by every descendant element until another lang value appears.

Example — Mixed-Language Content

When part of a page is in another language, mark that section with its own lang:

lang-mixed.html
<html lang="en">
<body>
  <p>The French word for hello is
    <span lang="fr">bonjour</span>.
  </p>
  <blockquote lang="de">
    Alle Menschen sind frei und gleich an Würde und Rechten geboren.
  </blockquote>
</body>
</html>
Try It Yourself

How It Works

Child elements inherit lang from ancestors but can override it. Mark only the smallest element that contains the foreign text—not the whole page.

Dynamic Values with JavaScript

Switch the document language when the user picks a locale:

dynamic-lang.html
<script>
  function getUserLanguage() {
    return navigator.language.startsWith("es") ? "es" : "en";
  }
  document.documentElement.lang = getUserLanguage();
</script>
Try It Yourself

How It Works

document.documentElement refers to the <html> element. Setting its lang property updates the content attribute and re-declares the page language at runtime.

♿ Accessibility

  • Always declare document language — WCAG expects a language to be programmatically determined; <html lang> is the standard way.
  • Mark foreign phrases inline — Wrap quotes, names, or terms in lang so screen readers use correct pronunciation.
  • Match lang to actual content — Do not set lang="en" on a page that is mostly Spanish.
  • Update lang when content changes — If JavaScript swaps page text to another language, update lang too.
  • Do not confuse with hreflang — Alternate link tags help SEO; lang helps users of the current page.
  • Right-to-left languages — Pair lang="ar" with dir="rtl" on <html> when the page is Arabic.

🧠 How lang Works

1

Author sets lang on html

BCP 47 tag on root.

Markup
2

Value inherited by children

Unless overridden.

Cascade
3

User agents read lang

Speech, spell-check, SEO.

Processing
=

Correct language handling

Better a11y and global reach.

Browser Support

The lang attribute is supported in all browsers and has been part of HTML for decades. Every modern user agent respects lang for hyphenation, font selection, and accessibility APIs.

HTML · Universal support

Language declaration everywhere

All major browsers honor lang on any element.

99% Browser support
Google Chrome Fully supported
Full support
Mozilla Firefox Fully supported
Full support
Apple Safari Fully supported
Full support
Microsoft Edge Fully supported
Full support
Internet Explorer Fully supported
Full support
Opera Fully supported
Full support
lang attribute 99% supported

Bottom line: Set lang on every page—support is universal and the accessibility benefit is immediate.

💡 Best Practices

✅ Do

  • Set lang on <html> for every document
  • Use valid BCP 47 codes (en, en-US, zh-Hans)
  • Override lang on inline foreign phrases
  • Update lang when JavaScript changes page language
  • Pair RTL languages with dir="rtl"

❌ Don’t

  • Skip lang on the root element
  • Use made-up or non-standard language codes
  • Set the wrong language for most of the page content
  • Confuse lang with hreflang on link tags
  • Wrap entire pages in a foreign lang for one small quote

Conclusion

The lang attribute is a crucial tool for indicating the language of your HTML document, contributing to better accessibility and user experience. It helps screen readers, browsers, and search engines treat your content correctly.

Always declare the document language on <html>, use nested lang for mixed content, and update it dynamically when your site supports language switching. Combined with accurate content, this greatly enhances the global reach of your web pages.

Key Takeaways

Knowledge Unlocked

Five truths every developer should know about lang

Bookmark these before shipping any page.

5
Core concepts
📝 02

BCP 47

en, es, en-US

Syntax
03

Screen readers

Correct pronunciation

A11y
📄 04

Nested lang

Mixed languages

Content
🔗 05

Not hreflang

Content vs alt URLs

SEO

❓ Frequently Asked Questions

It declares the language of an element’s text using a BCP 47 tag. Browsers, screen readers, spell-checkers, and search engines use it to process content correctly.
Always on <html> for the document default. Add it on inner elements when a section or phrase uses a different language.
BCP 47 language tags such as en, es, en-US, or zh-Hans. Use lowercase language subtags and proper region casing.
lang describes content language on the current page. hreflang on <link> or <a> points search engines to alternate language versions at other URLs.
Yes. Set document.documentElement.lang or element.lang when the user switches language or when dynamic content loads in another locale.
HTML does not enforce it, but WCAG and accessibility best practices treat declaring document language as essential. Every production page should include <html lang="...">.

Declare language on every page

Practice setting lang on <html> and marking mixed-language content in the Try It editor.

Try document lang example →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful