HTML Languages and Direction
Why the Language Matters
Web pages are written in many languages.
When you tell the browser which language a text is in, many things work better:
- Screen readers read the text with the right pronunciation
- Translation tools know which language to translate from
- Search engines show the page to people who speak that language
- The browser uses the right quotation marks and fonts
The lang Attribute
The lang attribute sets the language of an element.
Always set it on the <html> element, so it covers the whole page:
Example
<!DOCTYPE html>
<html lang="en">
<body>
...
</body>
</html>
The value is a language code, like en (English), es (Spanish), fr (French), or ar (Arabic).
You can add a country code after a dash, like en-GB (English in Great Britain) or pt-BR (Portuguese in Brazil).
Tip: See all codes in our Language Code Reference and Country Code Reference.
Language on Parts of a Page
If a part of the page is in another language, set lang on that element too.
Here, the browser picks the correct quotation marks for each language:
Example
<p>In English: <q>Hello</q></p>
<p lang="fr">In French: <q>Bonjour</q></p>
<p lang="de">In German: <q>Hallo</q></p>
Try it Yourself »
The result shows three different quotation marks:
In English: “Hello”
In French: «Bonjour»
In German: „Hallo“
A screen reader can also switch to a French or German voice for these paragraphs.
Right to Left Languages
Some languages, like Arabic, Hebrew, and Persian, are written from right to left.
Use the dir attribute with the value rtl for these languages:
The Arabic text means "Hello World!".
The paragraph starts at the right side of the page, and the exclamation mark ends up on the left, where the sentence ends.
For a whole page in a right to left language, set both attributes on the <html> element:
Example
<html lang="ar" dir="rtl">
Let the Browser Decide
Sometimes you do not know the direction of a text, for example a comment that a user wrote.
The value auto lets the browser look at the text and pick the direction:
The English paragraph goes from left to right, and the Arabic paragraph goes from right to left.
Links to Other Languages
If your web page is translated to other languages, the hreflang attribute tells search engines about the other versions:
Example
<head>
<link rel="alternate" hreflang="en" href="https://example.com/en/">
<link rel="alternate" hreflang="es" href="https://example.com/es/">
</head>
Search engines can then show the Spanish page to people who search in Spanish.
Tip: Learn more about search engines in the HTML SEO chapter.
Summary
- Set
langon the<html>element, and on parts in other languages - Use
dir="rtl"for languages written from right to left - Use
dir="auto"when you do not know the direction - Use
hreflangto link to translated pages
For a complete list of all available HTML tags, visit our HTML Tag Reference.