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 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 kinds of 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.
The bdi Element
Mixing left to right and right to left text in one line can go wrong.
Here is a list of users and their points, without and with the <bdi> element:
Example
<ul>
<li>User jdoe: 80 points</li>
<li>User إيان: 90 points</li>
</ul>
<ul>
<li>User <bdi>jdoe</bdi>: 80 points</li>
<li>User <bdi>إيان</bdi>: 90 points</li>
</ul>
Try it Yourself »
In the first list, the browser joins the number 90 with the Arabic name, and it jumps to the front of the line.
In the second list, <bdi> keeps the name apart from the rest of the text, so the line reads correctly.
Tip: Use <bdi> around names and other text that users write, when you do not know the language.
Links to Other Languages
If your page is translated, 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
<bdi>around names and text that users write - Use
hreflangto link to translated pages
HTML Tag Reference
| Tag | Description |
|---|---|
| <bdi> | Keeps a part of the text apart, so its direction does not mix with the text around it |
| <bdo> | Forces a part of the text in one direction |
For a complete list of all available HTML tags, visit our HTML Tag Reference.