Features

Ensure that HTML elements with both valid lang and xml:lang attributes agree on the base language of the page

Rule ID: html-xml-lang-mismatch User Impact: moderate Guidelines: WCAG 2.0

To maintain accessibility, the <html> element should have matching lang and xml:lang attributes, both specifying the same primary language of the document.

About This Rule

This rule ensures that the <html> element has consistent lang and xml:lang attributes, both containing valid and identical language codes. Adhering to this practice enhances accessibility for users of assistive technologies by providing clear language declarations.

Why It Matters

Screen readers and other assistive technologies rely on language attributes to determine the correct pronunciation and interpretation of content. Inconsistent or missing language declarations can lead to misinterpretation, making content difficult to understand for users who depend on these technologies.

How to Fix

To ensure consistency:

  • Add Matching Language Attributes: Assign both lang and xml:lang attributes to the <html> element, ensuring they have identical values. For example, for an English document:
    Copy code
    <html lang="en" xml:lang="en">
      <!-- document head and body -->
    </html>
  • Use Valid Language Codes: Utilize standard language codes, such as "en" for English, "fr" for French, or more specific codes like "en-US" for American English. A comprehensive list of language codes is available on the ISO Codes for the Representation of Names of Languages.
  • Specify Languages for Sections: For documents containing multiple languages, apply the lang attribute to relevant elements:
    Copy code
    <p>This is an English paragraph.</p>
    <p lang="es">Este es un párrafo en español.</p>
  • Indicate Text Direction for Right-to-Left Languages: For languages like Arabic or Hebrew, include the dir attribute:
    Copy code
    <p lang="ar" dir="rtl">نص باللغة العربية هنا</p>

Other Rules

Interested in other web accessibility rules? Please see these other rules: