HTML <body> Tag

Beginner
6 min read
Updated: Oct 2026
3 examples
Live preview

Definition and Usage

The <body> element represents the content of an HTML document — everything users see and interact with. A document may have only one <body>.

Place it inside <html>, usually after <head>. Put page structure such as <header>, <main>, and <footer> inside the body. Style the page with CSS — not deprecated presentational attributes.

Baseline · Widely available.

Not deprecated as an element. Stable across modern browsers since about July 2015 (and long before as HTML’s content root).

Deprecated attributes.

Do not use bgcolor, background, text, link, vlink, alink, or old margin attributes. Prefer CSS background-color, background-image, color, and margin.

HTML Syntax

syntax.html
<body> page content</body>

Write both tags for clarity. Content is flow content (paragraphs, sections, media, and so on). In a normal page, <body> is the second child of <html>.

Attributes

Modern pages rely on global attributes and CSS. Window-level event attributes (such as onload) also exist; prefer addEventListener in apps.

AttributeDescriptionTry it
Global attributesid, class, lang, and other globals on bodyTry it
onloadRuns script when the document finishes loading (prefer listeners in modern code)Try it
bgcolorDeprecated — use CSS background-color insteadTry it
CSS on bodyModern way to set page background, text color, and marginsTry it

Global Attributes

The <body> tag also supports the Global Attributes in HTML.

Live Preview

Preview common <body> content patterns in a mini document outline.

Simulated page body. Apply a pattern to see typical content structure.

Document body

Hello from the document body.

Simple content inside body.

HTML Examples

Short, copy-ready snippets. Use Try It Yourself for a live editor.

Example 1 — Minimal Document Structure

Every page needs html, head, and one body.

HTML
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <title>Document title</title>
  </head>
  <body>
    <p>
      The <code>&lt;body&gt;</code> element holds the content of the page.
    </p>
  </body>
</html>
Try It Yourself

How It Works

  1. Start with <!DOCTYPE html> and the <html> root.
  2. Put metadata in <head> (charset, title).
  3. Put visible content in <body> — only one per document.
  4. Close both tags so the structure stays clear for humans and tools.

Example 2 — Header, Main, and Footer

Organize body content with landmark elements.

HTML
<body>
  <header>
    <h1>My Site</h1>
  </header>

  <main>
    <p>Welcome to the main content.</p>
  </main>

  <footer>
    <p>Contact: [email protected]</p>
  </footer>
</body>
Try It Yourself

How It Works

  1. Use <header> for site or page chrome at the top.
  2. Put primary content in <main> (usually one per page).
  3. Use <footer> for contact, copyright, or related links.
  4. Keep them inside <body> — not in <head>.

Example 3 — Style with CSS (Not bgcolor)

Replace deprecated body attributes with stylesheet rules.

HTML
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Styled body</title>
  <style>
    body {
      margin: 1rem;
      background-color: #f5f5f5;
      color: #222;
      font-family: system-ui, sans-serif;
    }
  </style>
</head>
<body>
  <p>Page background and text color come from CSS.</p>
</body>
</html>
Try It Yourself

How It Works

  1. Skip bgcolor / text on the body tag.
  2. Write a body { … } rule in CSS.
  3. Set background-color and color for the page look.
  4. Adjust margin instead of old margin attributes.

Edge Cases & Pitfalls

Most body mistakes are structural (two bodies) or presentational (old attributes).

structure

Only one body

Extra body tags are invalid. Keep a single content root after head.

deprecated

Avoid presentational attributes

bgcolor, background, text, link, vlink, and alink belong in CSS now.

head vs body

Do not put visible UI in head

Titles and meta go in <head>; headings and paragraphs go in body.

events

Prefer listeners over many on* attributes

Body supports window events like onload, but modern scripts usually use addEventListener.

a11y

Use landmarks inside body

Helpful structure: header, main, nav, footer — clearer for assistive tech and SEO.

Browser Support

The <body> element is core HTML in every browser. It is not a Node.js, Deno, or Bun API.

✓ Baseline · Widely available

<body>

Universally supported as the document content root. Deprecated presentational attributes still render in places but must not be used in new pages.

99% Global support (approx.)
Google Chrome All
Full support
Mozilla Firefox All
Full support
Apple Safari All
Full support
Microsoft Edge All
Full support
Opera All
Full support
Samsung Internet All
Full support
Internet Explorer All
Supported
<body> Widely supported

Bottom line: Always safe for structure. Style with CSS; treat old body color attributes as legacy only.

Key Takeaways

  • Role: <body> holds all visible page content — one per document.
  • Order: html → head then body.
  • Structure: use header, main, and footer inside body.
  • Styling: CSS instead of deprecated bgcolor and friends.

One line: one <body> for visible content; style it with CSS.

Default CSS Settings

Browsers typically render <body> as a block with a small default margin:

default.css
body {
  display: block;
  margin: 8px;
}

Reset or customize margin, background-color, and color in your stylesheet as needed.

Frequently Asked Questions

The body element holds the visible content of an HTML document — headings, paragraphs, images, links, and the rest of the page UI. Metadata stays in head.
Only one. A document must not contain more than one body element.
Inside html, typically after head. It must be the second child of the html element in a normal document.
No. bgcolor, background, text, link, vlink, and alink are deprecated. Use CSS properties such as background-color and color instead.
onload runs script when the document finishes loading. Prefer addEventListener("load", …) on window in modern code, but the attribute still works for simple demos.
The HTML parser can imply body in some cases, but beginners should always write clear <body>…</body> tags for readable, maintainable markup.

Next: br

Insert a line break without starting a new paragraph.

br tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful