阿里云主机折上折
  • 微信号
Current Site:Index > <html> - the root element of an HTML document

<html> - the root element of an HTML document

Author:Chuan Chen 阅读数:48144人阅读 分类: HTML

<html> is the root element of an HTML document, and all other elements must be nested within it. It defines the beginning and end of the document and carries metadata (such as <head>) and content (such as <body>). Understanding the attributes and usage of <html> is fundamental to building valid web pages.

Basic Structure of <html>

A standard HTML document must start with the <html> tag and end with the </html> tag. Its basic structure is as follows:

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

The <!DOCTYPE html> declaration specifies the document type as HTML5, while <html> wraps the entire document content. The lang attribute specifies the primary language of the document (e.g., en for English).

Core Attributes of <html>

<html> supports several global attributes, but the following are particularly important:

  1. lang
    Defines the language of the document, affecting search engines and screen readers. For example:

    <html lang="zh-CN"> <!-- Simplified Chinese -->
    
  2. dir
    Controls text direction, with possible values of ltr (left-to-right) or rtl (right-to-left):

    <html dir="rtl"> <!-- Right-to-left languages like Arabic -->
    
  3. xmlns
    Declares the XML namespace in XHTML (usually omitted in HTML5):

    <html xmlns="http://www.w3.org/1999/xhtml">
    

<html> and the Document Type Declaration

The <!DOCTYPE> must precede <html> and cannot be omitted. It ensures the browser renders the page in standards mode. For example:

<!DOCTYPE html> <!-- HTML5 document type -->
<html>
  <!-- Content -->
</html>

Omitting <!DOCTYPE> may cause the browser to enter quirks mode, leading to styling and layout issues.

Child Elements of <html>

<html> can only contain two direct child elements:

  1. <head>
    Contains metadata (e.g., title, charset, CSS/JS links):

    <head>
      <meta charset="UTF-8">
      <title>Example Page</title>
    </head>
    
  2. <body>
    Contains the visible page content:

    <body>
      <h1>Welcome</h1>
      <p>This is a paragraph.</p>
    </body>
    

Practical Examples

Multilingual Site

Dynamically modify the lang attribute of <html> for language switching:

<html lang="ja"> <!-- Japanese -->
<body>
  <button onclick="document.documentElement.lang='zh'">Switch to Chinese</button>
</body>
</html>

Responsive Design

Use CSS's :root pseudo-class (which targets <html>) to define global variables:

:root {
  --primary-color: #3498db;
}
body {
  color: var(--primary-color);
}

Common Errors and Validation

  1. Missing Closing Tag
    The following code will cause a parsing error:

    <html>
      <body>
        <!-- Content -->
      </body>
    <!-- Missing </html> -->
    
  2. Incorrect Nesting
    <html> must be the outermost element of the document and cannot be nested within other tags:

    <!-- Incorrect Example -->
    <div>
      <html></html>
    </div>
    
  3. Duplicate Tags
    Only one <html> tag is allowed per document:

    <!-- Incorrect Example -->
    <html></html>
    <html></html>
    

Extended Uses of <html>

  1. JavaScript Manipulation
    Access the <html> element via document.documentElement:

    // Change background color
    document.documentElement.style.backgroundColor = "#f0f0f0";
    
  2. CSS Scoping
    Use <html> to scope styles:

    html.dark-mode {
      background: #333;
      color: white;
    }
    

    Toggle themes with JS:

    document.documentElement.classList.toggle("dark-mode");
    

本站部分内容来自互联网,一切版权均归源网站或源作者所有。

如果侵犯了你的权益请来信告知我们删除。邮箱:cc@cccx.cn

Front End Chuan

Front End Chuan, Chen Chuan's Code Teahouse 🍵, specializing in exorcising all kinds of stubborn bugs 💻. Daily serving baldness-warning-level development insights 🛠️, with a bonus of one-liners that'll make you laugh for ten years 🐟. Occasionally drops pixel-perfect romance brewed in a coffee cup ☕.