lang

The lang attribute in HTML specifies the language of an element's content, aiding browsers and search engines in accurately processing and displaying text based on its linguistic context.

Lang Attribute

The lang attribute in HTML defines the language of the content within an element, helping with accessibility and search engine optimization. It is commonly applied to the <html> tag but can be used on any HTML element.

  • Purpose: This attribute allows screen readers and search engines to correctly identify the language of the content, improving accessibility, translation, and content indexing.
  • Usage: While it's most often set on the <html> element, you can also apply it to any HTML element to define a different language for specific sections.

Syntax

index.html
<tag lang="language_code">Content</tag>

Example

Here’s an example demonstrating the use of the lang attribute:

This is a paragraph.

Ceci est un paragraphe.

index.html
<p>This is a paragraph.</p>
<p lang="fr">Ceci est un paragraphe.</p>

Key Points

  • Inheritance: When set on the <html> element, the lang attribute is inherited by all child elements, unless it is explicitly overridden.
  • Accessibility: The lang attribute assists screen readers by enabling them to pronounce the text accurately based on its language.
  • Search Engines: It helps search engines identify the language of the content, enhancing regional indexing and content relevancy.

See Also

Conclusion

The lang attribute in HTML defines the language of an element's content, helping browsers and search engines process and display text appropriately. It enhances accessibility by aiding screen readers and improves SEO by providing accurate linguistic context. The attribute can be applied to any HTML element, with inheritance from the <html> element for consistent language identification.