Home  Tech   Difference ...

difference between root, html and body

In HTML, the terms "root", "html", and "body" refer to different parts of the document structure. Understanding the differences between them is crucial for web development, especially when applying styles and manipulating the DOM. Here's an explanation of each:

Root Element

The root element is the topmost element in an HTML document, representing the entire document. In HTML5, the root element is the <html> element. It contains all other elements in the document, including <head>, <body>, and any other child elements.

<html> Element

<body> Element

Differences Between html and body

  1. Scope:

    • <html>: Encompasses the entire HTML document, including the <head> and <body> sections.
    • <body>: Only encompasses the content that is displayed in the browser window.
  2. Styling:

    • <html>: Styles applied here affect the entire document, which can include setting the global font or background.
    • <body>: Styles applied here affect only the visible content, often used for page-specific design adjustments.
  3. JavaScript Access:

    • <html>: Accessed via document.documentElement.
    • <body>: Accessed via document.body.

Example for Clarification

Here’s an example to illustrate the structure and application of styles:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Example Document</title>
    <style>
        html {
            background-color: lightgrey;
            font-family: Arial, sans-serif;
        }
        body {
            background-color: white;
            margin: 20px;
        }
    </style>
</head>
<body>
    <h1>Hello, World!</h1>
    <p>This is an example document.</p>
</body>
</html>

In this example:

Practical Applications

Published on: Jun 11, 2024, 02:30 AM  
 

Comments

Add your comment