<html>
The <html> element is the root element of an HTML document. It represents the top-level container for all other HTML elements on the page, wrapping both the <head> and <body> sections. Every valid HTML document must have exactly one <html> element.
This page was last updated on 2025-11-27
Syntax
<!DOCTYPE html>
<html lang="en">
<head>...</head>
<body>...</body>
</html>
Attributes
- lang - Specifies the primary language of the document (e.g., "en" for English, "fr" for French, "es" for Spanish). This is important for accessibility and search engines.
- dir - Sets the text direction for the document. Values are "ltr" (left-to-right) or "rtl" (right-to-left).
- xmlns - Declares the XML namespace. Only needed for XHTML documents, not standard HTML5.
Examples
Basic HTML5 document structure:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>My Website</title>
</head>
<body>
<h1>Welcome</h1>
</body>
</html>
Document with right-to-left text direction:
<!DOCTYPE html>
<html lang="ar" dir="rtl">
<head>...</head>
<body>...</body>
</html>
Multi-language document (primary language English):
<!DOCTYPE html>
<html lang="en-US">
<head>
<title>International Site</title>
</head>
<body>
<p>Hello, world!</p>
<p lang="es">Hola, mundo!</p>
</body>
</html>
When to Use
You must use the <html> element in every HTML document. It should immediately follow the DOCTYPE declaration and contain exactly two child elements: <head> and <body>, in that order.
Best practices:
- Always include the
langattribute to specify the document's language. This helps screen readers pronounce content correctly and assists search engines. - Use specific language codes like "en-US" or "en-GB" when targeting specific regions.
- Set the
dirattribute for languages that read right-to-left (Arabic, Hebrew, Persian). - While the closing </html> tag is technically optional in HTML5, always include it for clarity and compatibility.