MakeW3web

Defining the Document Language

Defining the document language is essential for accessibility, as it helps assistive technologies, like screen readers, understand and interpret the content correctly. Specifying the language also aids search engines in understanding the content and may improve search rankings for language-specific queries.

To define the document language, add the `lang` attribute to the `<html>` tag in your HTML document. The `lang` attribute should contain the language code for the primary language used in your content.

Here's an example of an HTML document with the language set to English:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Example Page</title>
  <!-- Additional head elements -->
</head>
<body>
  <!-- Page content -->
</body>
</html>

In this example, the `lang="en"` attribute specifies that the primary language of the content is English. Language codes are typically composed of two letters, which represent the language, and sometimes additional characters to represent a specific dialect or region. For example, "en" represents English, "es" represents Spanish, and "en-GB" represents British English.

If your web page includes content in multiple languages, you can specify the language for individual elements using the `lang` attribute. This helps assistive technologies understand the language switch and adapt accordingly.

Example:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Example Page</title>
  <!-- Additional head elements -->
</head>
<body>
  <p>This paragraph is in English.</p>
  <p lang="es">Este párrafo está en español.</p>
</body>
</html>

In this example, the main document language is set to English, but the second paragraph is marked as being in Spanish with the `lang="es"` attribute.

By defining the document language and using the `lang` attribute appropriately, you can improve accessibility and search engine optimization (SEO) for your web page.

Was this page helpful?