# This document appears to be written in X. Consider adding “lang="y"” (or variant) to the “html” start tag.

> Canonical HTML version: https://rocketvalidator.com/html-validation/this-document-appears-to-be-written-in-x-consider-adding-lang-y-or-variant-to-the-html-start-tag
> Attribution: Rocket Validator (https://rocketvalidator.com)
> License: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/)

The `lang` attribute on the `<html>` element declares the primary language of the document's content. When this attribute is missing or set incorrectly, the validator analyzes the text content and attempts to detect the language automatically. If it identifies a likely language, it produces this warning suggesting you add the appropriate `lang` value.

This matters for several important reasons:

- **Accessibility:** Screen readers rely on the `lang` attribute to select the correct pronunciation rules and voice profile. Without it, a screen reader might attempt to read Spanish text using English phonetics, producing unintelligible speech for the user.
- **Browser behavior:** Browsers use the language declaration for hyphenation, quotation mark styling, spell-checking, and font selection. For example, the CSS `hyphens: auto` property depends on the `lang` attribute to apply language-appropriate hyphenation rules.
- **Search engines:** Search engines use the `lang` attribute as a signal to understand the language of your content, which helps serve it to the right audience in search results.
- **Translation tools:** Automatic translation services use the declared language to determine whether (and from which language) to offer translation.

The value of the `lang` attribute must be a valid BCP 47 language tag. Common examples include `en` (English), `es` (Spanish), `fr` (French), `de` (German), `zh` (Chinese), `ja` (Japanese), and `ar` (Arabic). You can also specify regional variants like `en-US`, `en-GB`, `pt-BR`, or `es-MX`.

## Examples

### Missing `lang` attribute

This triggers the warning because the validator detects the content language but finds no `lang` declaration:

```html
<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>Mi Sitio Web</title>
</head>
<body>
  <h1>Bienvenido a mi sitio web</h1>
  <p>Este es un párrafo en español.</p>
</body>
</html>
```

### Fixed with the correct `lang` attribute

Adding `lang="es"` tells browsers, screen readers, and search engines that this document is in Spanish:

```html
<!DOCTYPE html>
<html lang="es">
<head>
  <meta charset="UTF-8">
  <title>Mi Sitio Web</title>
</head>
<body>
  <h1>Bienvenido a mi sitio web</h1>
  <p>Este es un párrafo en español.</p>
</body>
</html>
```

### Mismatched `lang` attribute

This can also trigger the warning when the `lang` value doesn't match the actual content. Here, the content is in French but the language is declared as English:

```html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Mon Site Web</title>
</head>
<body>
  <h1>Bienvenue sur mon site web</h1>
  <p>Ceci est un paragraphe en français.</p>
</body>
</html>
```

The fix is to correct the `lang` value to match the content:

```html
<!DOCTYPE html>
<html lang="fr">
<head>
  <meta charset="UTF-8">
  <title>Mon Site Web</title>
</head>
<body>
  <h1>Bienvenue sur mon site web</h1>
  <p>Ceci est un paragraphe en français.</p>
</body>
</html>
```

### Using a regional variant

If you need to specify a regional variation, append the region subtag. For example, `es-MX` for Mexican Spanish or `pt-BR` for Brazilian Portuguese:

```html
<!DOCTYPE html>
<html lang="es-MX">
<head>
  <meta charset="UTF-8">
  <title>Mi Sitio Web</title>
</head>
<body>
  <h1>Bienvenido a mi sitio web</h1>
  <p>Este es un párrafo en español de México.</p>
</body>
</html>
```

### Sections in a different language

When your document is primarily in one language but contains sections in another, set the main language on `<html>` and use `lang` on individual elements for the exceptions:

```html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>My Website</title>
</head>
<body>
  <h1>Welcome to my website</h1>
  <p>This site is available in multiple languages.</p>
  <blockquote lang="es">
    <p>Bienvenido a mi sitio web.</p>
  </blockquote>
</body>
</html>
```

This approach ensures that assistive technologies switch pronunciation rules appropriately when they encounter the foreign-language section, while browsers and search engines still understand the primary document language.
