Techniques for WCAG 2.0

On this page:

-

# H57: Using language attributes on the html element

## Applicability

HTML and XHTML

This technique relates to:

- Success Criterion 3.1.1 (Language of Page)
  - How to Meet 3.1.1 (Language of Page)
  - Understanding Success Criterion 3.1.1 (Language of Page)

## User Agent and Assistive Technology Support Notes

Additional subtags for region, script, variant or other aspects may lead to errors in language switching in older versions of some screenreaders.

JAWS 8.0 can be configured to change language automatically on the basis of the lang attribute. However, it only switches amongst major languages as indicated by the primary code. If a regional language variant is indicated with a language subcode, JAWS will use the default variant for which it is configured.

## Description

The objective of this technique is to identify the default language of a document by providing the `lang` and/or `xml:lang` attribute on the `html` element.

Identifying the language of the document is important for a number of reasons:

- It allows braille translation software to substitute control codes for accented characters, and insert control codes necessary to prevent erroneous creation of Grade 2 braille contractions.

- Speech synthesizers that support multiple languages will be able to orient and adapt to the pronunciation and syntax that are specific to the language of the page, speaking the text in the appropriate accent with proper pronunciation.

- Marking the language can benefit future developments in technology, for example users who are unable to translate between languages themselves will be able to use machines to translate unfamiliar languages.

- Marking the language can also assist user agents in providing definitions using a dictionary.

HTML 4.01 uses the `lang` attribute of the `html` element. XHTML served as text/html uses the `lang` attribute and the `xml:lang` attribute of the `html` element, in order to meet the requirements of XHTML and provide backward compatibility with HTML. XHTML served as application/xhtml+xml uses the `xml:lang` attribute of the `html` element. Both the `lang` and the `xml:lang` attributes can take only one value.

Note 1: HTML only offers the use of the `lang` attribute, while XHTML 1.0 (as a transitional measure) allows both attributes, and XHTML 1.1 allows only `xml:lang`.

Note 2: Allowed values for the lang and xml:lang attributes are indicated in the resources referenced below. Language tags use a primary code to indicate the language, and optional subcodes (separated by hyphen characters) to indicate variants of the language. For instance, English is indicated with the primary code "en"; British English and American English can be distinguished by using "en-GB" and "en-US", respectively. Use of the primary code is important for this technique. Use of subcodes is optional but may be helpful in certain circumstances.

## Examples

### Example 1

This example defines the content of an HTML document to be in the French language.

Example Code:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html lang="fr">
    <head>
      <title>document écrit en français</title>
      <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    </head>
    <body>
    	...document écrit en français...
    </body>
    </html>

### Example 2

This example defines the content of an XHTML 1.0 document with content type of text/html to be in the French language. Both the `lang` and `xml:lang` attributes are specified in order to meet the requirements of XHTML and provide backward compatibility with HTML.

Example Code:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" lang="fr" xml:lang="fr">
    <head>
      <title>document écrit en français</title>
      <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    </head>
    <body>
    ...document écrit en français...
    </body>
    </html>

### Example 3

This example defines the content of an XHTML 1.1 document with content type of application/xhtml+xml to be in the French language. Only the `xml:lang` attribute is specified.

Example Code:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
       "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr">
    <head>
      <title>document écrit en français</title>
    	<meta http-equiv="content-type" content="application/xhtml+xml; charset=utf-8" />
    </head>
    <body>
    	...document écrit en français...
    </body>
    </html>

## Resources

Resources are for information purposes only, no endorsement implied.

- HTML 4.01 "lang" attribute

- BCP 47: Tags for the Identification of Languages

- Inheritance of language codes

- Declaring Language in XHTML and HTML

- Authoring Techniques for XHTML & HTML Internationalization: Specifying the language of content 1.0

- Language tags in HTML and XML

## Related Techniques

- H58: Using language attributes to identify changes in the human language

## Tests

### Procedure

1. Examine the `html` element of the document.

2. Check that the `html` element has a `lang` and/or `xml:lang` attribute.

3. Check that the value of the `lang` attribute conforms to BCP 47: Tags for the Identification of Languages or its successor and reflects thae primary language used by the Web page.

### Expected Results

- The above checks are all true.

- Top
- Contents
- Intro
- Previous: Technique H56
- Next: Technique H58