← Back to HTML & CSS, page 7

html-css-0132 · Easy fresher

What do the lang and dir attributes do, and why do they matter beyond looks?

lang names the language so screen readers, spell checkers, hyphenation, translation and search work correctly, and it can be set on a single phrase. dir sets the text direction, and rtl mirrors the flow for languages such as Arabic and Hebrew.

Full answer

The lang attribute on the html element names the main language of the page, such as lang="en" or lang="hi". It looks small, but many tools use it.

  • Screen readers choose the right voice and pronunciation. Without it, English text can be read with the wrong accent, or in a voice that makes no sense.
  • Browsers choose the right quotation marks, hyphenation rules and fonts for the script. hyphens: auto works only when the language is known.
  • Translation tools and search engines use it to detect the language. Spell checkers use it too.
  • CSS can select by language with :lang(fr).

For a short phrase in another language, put lang on that element only: <span lang="fr">bonjour</span>. Then a screen reader switches voice for those words.

The dir attribute sets the text direction: ltr, rtl or auto. Languages such as Arabic and Hebrew run right to left. Set dir="rtl" on the html element (or on a part of the page) for such pages, and the browser mirrors the text flow, the alignment, the scrollbar side and the order of flex and grid items. dir="auto" lets the browser look at the first strong character, which is useful for user-generated text. If you use logical CSS properties such as margin-inline-start, your styles follow the direction without extra rules.

Use language codes from the standard, such as en, en-GB, pt-BR, zh-Hans. Keep the code short and correct.

The French word is italic because of the :lang(fr) rule, and a screen reader can read it with a French voice. The Arabic paragraph is aligned to the right. The last paragraph has dir="auto", so Hebrew text is treated as right to left without more code.

Set the language on every page, because it is a basic accessibility requirement.

html
<!DOCTYPE html>
<html lang="en" id="root">
<head><meta charset="utf-8"><title>Language</title></head>
<body>
  <p id="p1">The word <span lang="fr" id="fr">bonjour</span> is French.</p>
  <p lang="ar" dir="rtl" id="ar" style="text-align:start;width:300px">مرحبا</p>
  <p id="p3" dir="auto" style="width:300px;text-align:start">שלום</p>
  <style>:lang(fr) { font-style: italic; }</style>
</body>
</html>

This is a complete page, because the lang attribute sits on the html element. Without the doctype the page runs in quirks mode.

Key points

  • Put lang on the html element on every page.
  • Use lang on a phrase to switch language.
  • dir sets ltr, rtl or auto.
  • Logical properties follow the direction.
  • Use correct standard language codes.

Common mistakes

  • Leaving out lang on the html element.
  • Using a wrong code, such as lang="english".
  • Flipping a layout with transforms for right-to-left languages instead of using dir and logical properties.

Follow-ups

  • What does dir="auto" do?
  • How do logical properties help with dir?

Tips

  • Name screen readers, hyphenation and translation as the practical effects.
  • Mention lang on a single phrase.