HTML lang Attribute: How it Works with Examples

html lang attribute

The HTML lang attribute tells browsers and tools what language the page uses. It helps users and search engines understand the content better.

What Is the HTML lang Attribute?

The lang attribute tells the browser which language the page or element uses. It helps screen readers and search engines know the language in use. You can add it to the <html> tag or to any other tag when needed.

Search engines use it to index the page. Screen readers use it to choose the right voice. Some browsers also pick the right fonts.

You can use it with the <html> tag and set the attribute with the language code. You can also set it on other elements if they use a different language. The code must match a known language code format.

Here are some basic language codes you can use:

  • en means English
  • fr means French
  • es means Spanish
  • ar means Arabic
  • zh means Chinese

Some codes have two parts. en-US means English in the US. fr-CA means French in Canada.

You do not need to set the lang on every element. But you must set it when a section uses a different language from the rest of the page.

For example, a quote in Arabic on an English page must use lang="ar" on that element.

If your page does not use the lang attribute, screen readers may guess the wrong language. That can confuse users. Search engines may also miss the correct region or language of your site. The wrong language code can break voice settings and font styles.

Examples

Set Language on Whole Page

<!DOCTYPE html>
<html lang="en">
  <head><title>My Site</title></head>
  <body><p>This is a full English page.</p></body>
</html>

This example sets English for the full page. The browser and screen readers know that the full page uses English.

Use Arabic in One Element

<p>This is English text. <span lang="ar">هذه جملة عربية.</span></p>

The page uses English, but one part uses Arabic. The lang="ar" tells the browser and tools to switch language just for that part.

Use Regional Language Code:

<html lang="en-GB">
  <body><p>This page follows British English rules and not American spelling.</p></body>
</html>

This code uses en-GB to set British English. The tools now expect British grammar and spelling for the full page.

Wrong Language Code:

<html lang="xx">
  <body><p>This language code is not valid.</p></body>
</html>

The code xx is not a known language. Screen readers cannot pick the right voice, and search engines may skip this page.

Wrapping Up

In this article, you learned what the lang attribute means and how it helps your page. You also understood where to place it with examples.

Here is a quick recap:

  • Use the lang attribute to tell the page language.
  • Set it on the <html> tag or on parts with other languages.
  • Always use correct codes like en, fr, ar, or en-US.
  • Wrong codes can harm the search and the access tools.

FAQs

What is the purpose of the <html lang> attribute in HTML?

The <html lang> attribute tells browsers and tools which language the page uses. This helps with accessibility, SEO, and translation tools. Example:
<html lang="en">
  <head>
    <title>Page Title</title>
  </head>
  <body>Content here</body>
</html>

How do I add the <lang> attribute to an HTML document?

Add the <lang> attribute to the opening <html> tag. Use the correct two-letter language code. Example:
<html lang="fr">

Why is the <lang> attribute important for SEO and screen readers?

The <lang> attribute tells search engines and screen readers what language the content uses. This helps indexing and improves user experience. Example:
<html lang="es">

Can I use <lang> on other tags besides <html>?

Yes, you can add <lang> to any tag like <p>, <div>, or <span> to mark part of the page with a different language. Example:
<p lang="ar">مرحبا بك</p>

Similar Reads

HTML Span Tag: Inline Container Explained

The HTML span tag acts as a simple container for inline text or other inline elements. It groups text and…

The canvas Tag in HTML: How to Draw Graphics with Examples

The HTML canvas tag draws shapes and images. It also helps you draw text with code. It gives control over…

HTML link Tag: Linking Stylesheets

The link tag in HTML connects a web page to external resources like stylesheets or icons. It's placed in the…

HTML Aside Tag: How to Create a Side Content

You use the <aside> tag to mark extra content in HTML. It does not replace the main text and shows…

HTML details Tag: How to Toggle Hidden Content

The <details> tag hides content and shows it when needed. It helps organize long pages. The tag shows extra info…

HTML title Tag: How to Set Web Page Titles

The title tag in HTML shows the page name in the browser tab and helps search engines know the topic.…

HTML Skip Link Guide for Beginners and Web Developers

Skip link in HTML helps users jump to important parts of a web page without delay. What is an HTML…

HTML track Tag: How to Add Subtitles to Videos

The track tag in HTML adds text tracks like subtitles and captions to media. It improves video access and helps…

HTML template Tag: How it Works with Examples

The HTML <template> tag lets you store HTML content that won’t show up right away. JavaScript can pull it in…

HTML pre Tag: How to Display Preformatted Text

The HTML <pre> tag keeps the original format of your text. It shows spaces and line breaks exactly as you…

Previous Article

The hidden Attribute in HTML: How it Works with Examples

Next Article

HTML dir Attribute: How to Set Text Direction

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *


Subscribe to Get Updates

Get the latest updates on Coding, Database, and Algorithms straight to your inbox.
No spam. Unsubscribe anytime.