Ensure lang attributes have valid values
The lang
attribute in HTML specifies the language of the document's content. It's crucial to assign a valid language code to this attribute to enhance accessibility and ensure correct pronunciation by screen readers.
About This Rule
This guideline ensures that the lang
attribute has a valid value, enhancing accessibility for users who rely on assistive technologies. Adhering to this practice aligns with the following standards:
- WCAG 2.1 (AA): 3.1.2 Language of Parts
- WCAG 2.0 (AA): 3.1.2 Language of Parts
- WCAG 2.2 (AA): 3.1.2 Language of Parts
- Trusted Tester: 11.B - The human language for any content segment that differs from the default human language of the page can be programmatically determined.
- EN 301 549: 9.3.1.2 Language of Parts
Why It Matters
Screen readers and other assistive technologies rely on the lang
attribute to determine the language of the content, enabling accurate pronunciation and interpretation. An invalid or missing lang
attribute can lead to mispronunciations, confusing users and diminishing their experience. This practice aligns with WCAG Success Criterion 3.1.2: Language of Parts.
How to Fix
To ensure proper language declaration:
-
Specify the Primary Language in the
<html>
Element: Add a valid two-letter language code to thelang
attribute in the opening<html>
tag. - Include Regional Variants if Necessary: For dialects or regional language variations, use extended language codes like "en-US" for American English or "fr-CA" for Canadian French.
-
Indicate Language Changes Within the Document: When a section of the content is in a different language, use the
lang
attribute on the specific element. -
Specify Text Direction for Right-to-Left Languages: Use the
dir
attribute to define text direction for languages like Arabic or Hebrew.
Examples
Incorrect
An HTML document without a lang
attribute:
Correct
An HTML document with a valid lang
attribute:
More Information
Other Rules
Interested in other web accessibility rules? Please see these other rules: