References

Beginner-friendly references for web development, with live, editable examples.

The HTML lang attribute

Global attribute Works on every element All modern browsers Updated
Quick answer

The HTML lang attribute declares the language of an element's text using a BCP 47 language tag, e.g. lang="en" or lang="pt-BR". Set it on <html> for the whole page and on any element whose language differs. It is a global attribute that helps screen readers, translation tools and search engines.

Overview

The lang attribute tells browsers and tools what language an element's content is written in, using a standard BCP 47 tag such as en, en-GB, fr or ar.

Set it once on the <html> element to declare the page's primary language, then add it to any passage written in another language. Screen readers use it to switch to the correct pronunciation and voice, browser and machine translation use it to translate accurately, search engines use it as a language signal, and CSS can target it with the :lang() selector. It pairs naturally with the dir attribute for right-to-left languages.

Syntax

<html lang="en">
  …
  <p>She whispered <span lang="fr">au revoir</span> and left.</p>
</html>

Values

Value
A BCP 47 language tag, e.g. en, en-US, fr, pt-BR, ar, zh-Hans.

Example

Live example
<p>The French word <span lang="fr">château</span> means castle.</p>

Best practices

  • Always set lang on the <html> element — it is one of the cheapest accessibility wins available.
  • Mark up individual phrases in other languages with their own lang so they are pronounced correctly.
  • Use valid BCP 47 codes and add a region when it matters (en-GB vs en-US).
  • Keep lang in sync with the actual content language, including on dynamically swapped content.

Accessibility

The lang attribute is critical for screen-reader users: it tells the screen reader which language rules and voice to use, so a French phrase is read with French pronunciation instead of being mangled by an English voice. A page with no lang on <html> is a common, easily-fixed accessibility failure.

Frequently asked questions

What is the HTML lang attribute for?
It declares the language of the content so screen readers, translators and search engines handle it correctly.
Where should I set the lang attribute?
On the <html> element for the whole document, and on any element containing text in a different language.
What values does lang accept?
BCP 47 language tags — a primary subtag plus optional region or script, e.g. en, en-US, pt-BR, zh-Hant.
Does the lang attribute help SEO?
Yes. It is a clear language signal that helps search engines serve your pages to the right audience, alongside hreflang.