Skip to content

04. html and body Elements

The <html> and <body> elements are fundamental to every HTML document. The <html> element serves as the root container for the entire document, while the <body> element contains all visible content. Understanding these elements is essential for creating well-structured HTML documents.

The <html> element is the root element of an HTML document. It wraps all other HTML elements and represents the entire document. Every HTML document must have exactly one <html> element.

<!DOCTYPE html>
<html lang="en">
<!-- All other elements go here -->
</html>
  • Root element: Contains all other HTML elements
  • Required: Must be present in every HTML document
  • Single instance: Only one <html> element per document
  • Wraps everything: Everything except the DOCTYPE goes inside it

The lang attribute specifies the language of the document’s content:

<html lang="en"> <!-- English -->
<html lang="es"> <!-- Spanish -->
<html lang="fr"> <!-- French -->
<html lang="de"> <!-- German -->
<html lang="ja"> <!-- Japanese -->

Why it matters:

  • Helps screen readers pronounce content correctly
  • Assists search engines in understanding content
  • Enables browser translation features
  • Improves accessibility

While <html> can have various attributes, the most important ones are:

  • lang: Specifies the document language (highly recommended)
  • xmlns: Used in XHTML documents (not needed in HTML5)
  • class/id: Can be used for styling or JavaScript targeting

The <body> element contains all visible content of the HTML document. It represents the content that users see and interact with in the browser.

<!DOCTYPE html>
<html lang="en">
<head>
<title>My Page</title>
</head>
<body>
<!-- All visible content goes here -->
<h1>Welcome</h1>
<p>This is visible content.</p>
</body>
</html>
  • Visible content: Contains everything users see
  • Required: Must be present in every HTML document
  • Single instance: Only one <body> element per document
  • After head: Always comes after the <head> element

The proper order of elements in an HTML document is:

<!DOCTYPE html>
<html lang="en">
<head>
<!-- Metadata goes here -->
</head>
<body>
<!-- Visible content goes here -->
</body>
</html>
  1. DOCTYPE: Declares HTML version
  2. html: Root element wrapping everything
  3. head: Contains metadata (not visible)
  4. body: Contains visible content

The <html> and <body> elements have a parent-child relationship:

<html> <!-- Parent -->
<head> <!-- Sibling of body -->
</head>
<body> <!-- Child of html -->
<!-- Content -->
</body>
</html>

The <html> element is the parent, and <head> and <body> are its direct children. Both <head> and <body> are siblings.

Both elements can be styled with CSS, and they serve different purposes:

The <html> element is often used for:

  • Setting base font size (for rem units)
  • Setting background color for the entire viewport
  • Setting default text color
html {
font-size: 16px; /* Base for rem units */
background-color: #f5f5f5;
color: #333;
}

The <body> element is commonly styled for:

  • Page background
  • Default font family
  • Margins and padding
  • Text alignment
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 20px;
background-color: white;
color: #333;
}

Understanding how html and body interact with CSS is important:

/* html takes full viewport height */
html {
height: 100%;
}
/* body can fill html or have its own height */
body {
min-height: 100%;
margin: 0;
}

For layouts that fill the viewport:

<!DOCTYPE html>
<html lang="en">
<head>
<style>
html, body {
height: 100%;
margin: 0;
padding: 0;
}
</style>
</head>
<body>
<!-- Full-height content -->
</body>
</html>

Setting up responsive typography:

<!DOCTYPE html>
<html lang="en">
<head>
<style>
html {
font-size: 16px; /* Base for rem */
}
body {
font-family: system-ui, sans-serif;
line-height: 1.6;
}
</style>
</head>
<body>
<!-- Content -->
</body>
</html>

Always include the lang attribute on the <html> element:

<html lang="en">

This helps:

  • Screen readers pronounce words correctly
  • Search engines understand content language
  • Browser translation tools work properly

Ensure proper nesting:

<!-- Correct -->
<html>
<body>
<main>
<!-- Content -->
</main>
</body>
</html>
<!-- Incorrect - body cannot be outside html -->
<body>
<html>
<!-- This is wrong -->
</html>
</body>

Problem: Forgetting the <html> element.

<!DOCTYPE html>
<head>
<!-- Missing html wrapper -->
</head>

Solution: Always wrap content in <html>.

Problem: Having more than one <body> element.

<body>
<!-- First body -->
</body>
<body>
<!-- Second body - WRONG -->
</body>

Solution: Use only one <body> element.

Problem: Placing visible content outside the <body> element.

<html>
<head>
<h1>This shouldn't be here</h1>
</head>
<body>
<!-- Content should be here -->
</body>
</html>

Solution: All visible content must be inside <body>.

  1. Always include lang attribute: <html lang="en"> improves accessibility
  2. Single html element: Only one per document
  3. Single body element: Only one per document
  4. Proper nesting: Ensure correct parent-child relationships
  5. Content in body: All visible content belongs in <body>
  6. Metadata in head: Non-visible information goes in <head>

Here’s a complete, well-structured HTML document:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My Web Page</title>
<style>
html {
font-size: 16px;
}
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 20px;
line-height: 1.6;
}
</style>
</head>
<body>
<header>
<h1>Welcome to My Website</h1>
</header>
<main>
<p>This is the main content of the page.</p>
</main>
<footer>
<p>&copy; 2024 My Website</p>
</footer>
</body>
</html>