HTML lang Attribute

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

Definition and Usage

The lang global attribute declares the natural language of an element’s content — or the language users should write in for editable fields. Its value is a single BCP 47 language tag such as en, en-US, or fr.

Always set lang on <html>. Nest another lang when a phrase or block is in a different language so assistive tech can pronounce it correctly (WCAG 3.1.1 / 3.1.2).

Baseline · Widely available.

Not deprecated and not experimental. A core HTML global attribute supported everywhere modern browsers render pages.

Do not leave the page language unknown.

If no ancestor (and no Content-Language cue) sets a language, the language is unknown. That hurts screen readers and SEO — set lang on <html>.

HTML Syntax

syntax.html
<element lang=" bcp47-tag">

Common forms: language only (en), or language + region (en-GB, pt-BR).

Attribute Values

One BCP 47 tag (or an empty string). Examples:

ValueDescriptionTry it
en, fr, es …Primary language (ISO 639)Try it
en-US, en-GB, zh-Hans …Language plus region or script variantTry it
lang=""Language explicitly unknownTry it
Invalid tagTreated as invalid (not a usable language)Try it

Live Preview

Change lang on the sample paragraph. The attribute updates; screen readers would use the new tag for pronunciation.

Sample text below stays the same; only the language tag changes for study.

Sample element

The quick brown fox jumps over the lazy dog.

lang="en" on the paragraph.

HTML Examples

Short, copy-ready snippets. Use Try It Yourself for a live editor.

Example 1 — Set the Page Language

Put lang on <html> so the whole document has a default language.

HTML
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document language</title>
</head>
<body>
  <p>This page is written in English.</p>
</body>
</html>
Try It Yourself

How It Works

  1. One root tag. lang on <html> sets the default for all descendants.
  2. BCP 47 value. Use en, or be more specific with en-US / en-GB.
  3. Inheritance. Children without their own lang inherit this value.
  4. Accessibility + SEO. Assistive tech and crawlers can detect the page language.

Example 2 — Mixed-Language Content

Override lang on spans or blocks that switch language mid-page.

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

  1. Keep a page default. lang="en" on <html> covers most text.
  2. Mark foreign bits. Nested lang="fr" / lang="de" override only that subtree.
  3. Pronunciation. Screen readers can switch voice/rules for those phrases (WCAG 3.1.2).
  4. Return automatically. After the nested element ends, inheritance returns to English.

Example 3 — Update lang with JavaScript

Read and set document.documentElement.lang when the UI language changes without a full reload.

HTML
<html lang="en">
<body>
  <p>Current lang: <strong id="show">en</strong></p>
  <button type="button" id="toggle">Switch to Spanish (es)</button>

  <script>
    document.getElementById("toggle").addEventListener("click", function () {
      var next = document.documentElement.lang === "en" ? "es" : "en";
      document.documentElement.lang = next;
      document.getElementById("show").textContent = next;
      this.textContent = next === "en"
        ? "Switch to Spanish (es)"
        : "Switch to English (en)";
    });
  </script>
</body>
</html>
Try It Yourself

How It Works

  1. Reflects the attribute. document.documentElement.lang maps to <html lang>.
  2. Assign a new tag. Setting the property updates the live attribute.
  3. Keep content in sync. Also swap visible strings/translations when you change language.
  4. Still set HTML first. Prefer a correct initial lang in markup for first paint and no-JS users.

Edge Cases & Pitfalls

Most issues are a missing root lang, wrong tags, or confusing related attributes.

a11y

Always declare the page language

WCAG expects a programmatically determined page language. Start with <html lang="…">.

compare

Not hreflang or srclang

hreflang describes a linked document’s language. srclang is for <track>. lang is for content in this document.

xml

xml:lang must match if both exist

If you also set xml:lang, HTML requires the same value as lang (case-insensitive). xml:lang takes priority when both are present.

empty

Empty vs invalid

lang="" means unknown. A non-BCP-47 string is invalid. Prefer real tags; avoid inventing codes.

legacy

Do not confuse with language on <script>

The old language attribute on scripts is deprecated. Use type / modern module scripts instead — unrelated to page language.

Browser Support

lang is an HTML global attribute in the browser. It is not a Node.js, Deno, or Bun API.

✓ Baseline · Widely available

lang

Supported in all major browsers for decades. Safe and expected on every HTML document for accessibility and internationalization.

100% Global support (approx.)
Google Chrome All
Full support
Mozilla Firefox All
Full support
Apple Safari All
Full support
Microsoft Edge All
Full support
Opera All
Full support
Samsung Internet All
Full support
Internet Explorer All
Full support
lang global attribute Universally supported

Bottom line: Always use lang. Browser support is not the bottleneck — missing markup is.

Key Takeaways

  • Role: declare content language with a BCP 47 tag.
  • Start on <html>: then nest overrides for foreign phrases.
  • Inheritance: children inherit unless they set their own lang.
  • A11y: required for WCAG page language; mark language changes in content.

One line: set lang on <html>, then override nested parts so every language is announced correctly.

Frequently Asked Questions

It declares the natural language of an element’s content (or the language the user should write in for editable content) using a BCP 47 language tag such as en, en-US, or fr.
Always set it on the html element for the whole page. Add lang again on nested elements when a phrase or section is in a different language.
Yes for WCAG: the page language must be programmatically determined (3.1.1), and parts in other languages should be marked too (3.1.2). Screen readers use lang for pronunciation.
lang="" means the language is unknown. A value that is not a valid BCP 47 tag is treated as invalid. Prefer a real tag like en or es.
If an element has no lang, it inherits from its ancestors. If nothing sets a language, Content-Language (HTTP or meta) may apply; otherwise the language is unknown.
lang describes content language in the document. hreflang describes the language of a linked resource. srclang is the language of a media text track.

Next: language

See the deprecated script language attribute (unrelated to page lang).

language tutorial →

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.

8 people found this page helpful