Back to HTML Notes
Topic #90

HTML lang Attribute

By the end of this lesson, you will be able to correctly apply the lang attribute to HTML elements to improve screen reader pronunciation and search engine indexing.

What it is

The lang attribute specifies the language of an element’s content. It uses ISO 639-1 two-letter codes (e.g., en for English, fr for French) optionally followed by a hyphen and an ISO 3166-1 alpha-2 country code (e.g., en-US). This attribute helps assistive technologies determine which voice profile or dictionary to use when reading text aloud.

Why it matters

  • Accessibility: Screen readers rely on the lang attribute to pronounce words correctly. Without it, foreign text may be read with the wrong accent or phonetic rules, making it unintelligible.
  • SEO: Search engines use language metadata to serve appropriate results to users based on their locale preferences.
  • Translation Tools: Browser translation features and automated tools detect language boundaries more accurately when lang is present.
  • CSS Styling: Developers can target specific languages using CSS attribute selectors (e.g., [lang="fr"]) to adjust typography or spacing.

Syntax or steps

The most common usage is setting the primary document language on the root <html> tag. For mixed-language content within a page, apply the attribute to specific block-level or inline elements containing the different language.

  1. Add lang="xx" to the opening <html> tag.
  2. If a section contains text in a different language, wrap that text in an element (like <p> or <span>) and add lang="yy".

Example

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Multilingual Example</title>
</head>
<body>
    <h1>Welcome</h1>
    <p>This paragraph is in English.</p>
    
    <p lang="es">Este párrafo está en español.</p>
    
    <p>Back to English text here.</p>
</body>
</html>

Explanation: The <html> tag declares the default language as English (en). The second paragraph explicitly overrides this with lang="es", signaling to a screen reader that it should switch to Spanish pronunciation rules for that specific sentence only. Subsequent paragraphs revert to the inherited English setting.

Common mistakes

  • Omitting the root language: Failing to set lang on <html> forces screen readers to guess, often resulting in poor pronunciation for all content.
  • Using invalid codes: Ensure you use standard ISO codes. For example, use pt-BR for Brazilian Portuguese, not brazilian-portuguese.
  • Forgetting nested overrides: If you have a long article in French but include an English quote, you must mark the quote with lang="en". Otherwise, the French screen reader voice will attempt to read the English words.
  • Confusing lang with xml:lang: In modern HTML5, lang is sufficient. xml:lang is primarily for XHTML or XML documents.

When to use it

ScenarioAction
Single-language websiteSet lang on <html> only.
Mixed-language contentSet lang on <html> AND override on specific elements.
Dynamic user-generated contentEnsure backend logic detects language and applies lang attributes dynamically.

Practice

Guided Exercise: Create a simple HTML page with a heading in German and a paragraph in Japanese. Apply the correct lang attributes.

Challenge: Write a CSS rule that changes the font family to "Arial" for any element where lang="zh-CN".

Hint: Use the attribute selector [lang="zh-CN"] { font-family: Arial; }.

Quick check

Q: Does the lang attribute affect how search engines index your page?

A: Yes, it helps search engines understand the content's language, which influences localization and relevance rankings for users in those regions.

Summary

The lang attribute is a critical accessibility feature that ensures assistive technologies interpret text correctly. Always define the primary language at the document root and override it locally for mixed-content sections to maintain clarity for both users and machines.

Want to go beyond the notes?

Join Coding Now Tech Institute's HTML course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

HTML lang Attribute – FAQs

Quick answers about learning HTML lang Attribute in HTML.

This free note from Coding Now Tech Institute explains HTML lang Attribute in HTML — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every HTML topic on Coding Now Tech Institute, including HTML lang Attribute, is 100% free with no signup required.
With focused practice, most students grasp HTML lang Attribute in 1–3 days from these notes; pairing it with Coding Now Tech Institute's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the Coding Now Tech Institute Community (/community) — expert instructors answer within 24 hours.
Call NowEnroll Now