The xml:lang
attribute in HTML is used to specify the natural language of the content. This attribute is part of the XML namespace and can be used alongside the lang
attribute. It is particularly useful in documents that are meant to be compliant with both HTML and XML (XHTML).
xml:lang
attribute helps in defining the language of the text content in an element, similar to the lang
attribute.xml:lang
attribute to determine the language.
Here is an example of how to use both xml:lang
and lang
attributes in an HTML document:
<!DOCTYPE html>
<html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>Example Page</title>
</head>
<body>
<p lang="fr" xml:lang="fr">Bonjour tout le monde!</p>
</body>
</html>
In this example:
html
element specifies that the primary language of the document is English (en
).p
element overrides this and specifies that the language of its content is French (fr
).lang
and xml:lang
should have the same value to avoid conflicts and ensure consistency.lang
attribute, while XML parsers recognize xml:lang
. Using both ensures compatibility with a wider range of tools and browsers.lang
and xml:lang
attributes when working with XHTML documents to ensure full compatibility.lang
and xml:lang
consistent within the same element to avoid confusion.