Every visible element on a web page lives inside a single container. Headings, paragraphs,
images, links, forms, buttons, and every interactive widget — all of them are children of
the <body> element. If you have ever seen the phrase “put it in the
body,” this is what that means.
In this guide, you will learn what the <body> element does, its exact
syntax, how it differs from the head and the html element, how to structure real content
inside it with semantic landmarks, and the mistakes that cause the most common layout
problems.
<body> element is the container
for all visible content on a web page — everything the visitor reads, sees, clicks, or
types is nested inside it.
What Is the <body> Element?
The <body> element is the second child of <html>,
following the <head>. Everything the visitor actually sees on a webpage
lives inside it — every heading, paragraph, image, link, form, and interactive element.
A minimal document with a body looks like this:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>My Page</title>
</head>
<body>
<h1>Hello, world!</h1>
<p>This content lives in the body.</p>
</body>
</html>
Nothing visible appears unless it is inside <body>. That single rule
determines where every piece of content goes.
What the element represents
The visible page
Everything the visitor sees, reads, or interacts with is inside the body.
The content container
Headings, paragraphs, images, forms, buttons — all nested inside.
The landmark container
Semantic landmarks like header, main, and footer live directly inside the body.
The style target
Common CSS rules like font-family, background, and margin are set on the body.
When you are unsure whether content belongs in the head or the body, ask one question: “Will the visitor see this on the page?” If yes, it goes in the body. If no, it belongs in the head.
Syntax and Anatomy
The <body> element is a normal, non-void element. It has an opening
tag, content (all the page’s visible elements), and a closing tag.
<body id="page" class="theme-light">
<!-- all visible content goes here -->
</body>
Here is the structure broken down:
The <body> element is not void. It always has content (or at least
the potential for content), so it requires both an opening tag and a closing tag.
Writing <body /> is invalid HTML5.
Where the <body> Element Sits
The <body> element is the second child of <html>,
coming right after the <head>. Nothing appears between them.
The head comes first because browsers need metadata (character encoding, viewport, stylesheets) before rendering. The body comes second because it contains the content that the metadata describes.
A document can have exactly one <body> element. If you write a second
one, browsers merge the contents into the first — but the markup becomes invalid, and
the DOM will not match what you wrote.
Body vs Head vs html
These three elements are easy to confuse. Here is the difference in one table.
| Element | Role | Visible? | Number per page |
|---|---|---|---|
<html> |
The root element — contains both head and body. | No | One |
<head> |
Metadata about the page. | No | One |
<body> |
All visible content on the page. | Yes | One |
A simple way to remember it: <html> is the container for everything,
<head> is the invisible part, and <body> is what
the visitor actually sees.
What Goes Inside the Body
The body accepts almost anything. Here is the full list of content types you can put inside it, grouped by purpose.
Text content
Headings (h1–h6), paragraphs (p), quotes (blockquote), preformatted text (pre).
Links and anchors
Hyperlinks (a), image maps (map, area).
Media
Images (img), video (video), audio (audio), canvas (canvas), embedded content (iframe, embed).
Lists
Unordered lists (ul), ordered lists (ol), list items (li), description lists (dl).
Tables
Data tables (table) with rows, cells, and headers.
Forms
Form containers (form), inputs, buttons, labels, select menus, and textareas.
Semantic landmarks
Structural sections: header, nav, main, article, section, aside, footer.
Generic containers
Used when no semantic element fits: div and span.
Scripts
JavaScript (script) placed at the end of the body, right before </body>.
If the visitor can see it, read it, click it, or type into it, it belongs in the body. If it describes the page or connects external resources, it belongs in the head.
Structuring the Body with Semantic Landmarks
A modern body is usually organized into semantic landmarks. These elements describe what each part of the page is, which helps screen readers, search engines, and other developers understand your structure instantly.
<article>, <section>
related links, ads
Here is the same structure in real HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Structured Body Example</title>
</head>
<body>
<header>
<h1>My Website</h1>
<nav>
<a href="index.html">Home</a>
<a href="about.html">About</a>
<a href="contact.html">Contact</a>
</nav>
</header>
<main>
<article>
<h2>A blog post</h2>
<p>The body of the article goes here.</p>
</article>
<aside>
<p>Related links and side notes.</p>
</aside>
</main>
<footer>
<p>© 2025 Inar Learn</p>
</footer>
</body>
</html>
This structure is instantly readable. A screen reader can jump between landmarks, a
search engine can identify the main content, and a developer can find what they need
without scrolling through a wall of <div> tags.
What each landmark means
| Element | Purpose | Number per page |
|---|---|---|
<header> |
Introductory content or navigation at the top of a page or section. | One per page or per section |
<nav> |
Navigation links for moving around the site. | One or more |
<main> |
The primary content of the page. | One visible |
<article> |
Self-contained content that could stand alone. | One or more |
<section> |
A thematic grouping of content, usually with a heading. | One or more |
<aside> |
Content indirectly related to the main content. | Zero or more |
<footer> |
Closing content — copyright, contact, secondary links. | One per page or per section |
A page should have exactly one visible <main> element. It wraps the
primary content — the thing a visitor came to the page to read or do. Content that
repeats across pages (headers, footers, sidebars) goes outside it.
Attributes on the <body> Element
The <body> element accepts every global attribute — id,
class, style, data-*, and so on. It also historically
accepted event handler attributes.
Global attributes you might use
Theme or state classes
Commonly used to toggle layout variants across the page.
<body class="theme-dark layout-wide">Page identifier
Rarely needed, but useful for targeting a specific page in CSS or scripts.
<body id="home-page">Inline styles
Can set background or font defaults, but a stylesheet is almost always better.
<body style="background: #f8fafc;">Custom data
Lets scripts read page-level configuration without a global variable.
<body data-env="production">Event handler attributes (legacy)
Older HTML allowed event handlers like onload and onscroll
directly on the body element. These still work, but they mix behavior with structure and
are discouraged in modern code.
Legacy
Runs when the page finishes loading. Use window.addEventListener('load', ...) instead.
<body onload="init()">Legacy
Runs when the page is being unloaded. Unreliable — avoid it.
<body onunload="cleanup()">The modern approach is to attach event listeners in JavaScript. This keeps behavior separate from structure and makes the code easier to test.
<!-- Instead of <body onload="init()"> -->
<body>
<script>
window.addEventListener("load", function () {
console.log("Page loaded");
});
</script>
</body>
Obsolete presentational attributes
Older HTML had presentational attributes on the body — bgcolor,
background, text, link, vlink,
alink, and marginwidth. All of these are obsolete.
<body bgcolor="#ffffff"
text="#000000"
link="#0000ff">body {
background: #ffffff;
color: #000000;
}
a { color: #0000ff; }All presentational attributes are handled by CSS today. Mixing them into HTML is a sign of code written before about 2010.
Styling the Body with CSS
The <body> element is one of the most common places to set global
defaults for a page. Properties like font family, text color, and background color are
usually defined there so they cascade to every descendant.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Styling the Body</title>
<style>
body {
font-family: system-ui, sans-serif;
font-size: 16px;
line-height: 1.7;
color: #0f172a;
background: #f8fafc;
max-width: 720px;
margin: 0 auto;
padding: 40px 20px;
}
</style>
</head>
<body>
<h1>A styled page</h1>
<p>The font, color, and spacing come from CSS on the body element.</p>
</body>
</html>
Because the body is the root of all visible content, setting a property on it applies to everything inside by inheritance. This is why the body is often called “the global default container.”
Common CSS properties on the body
font-family
Sets the default font for all text. Choose a system font stack or a loaded web font.
background
Sets the page background. Often a solid color, gradient, or image.
color
Sets the default text color inherited by all descendants.
margin
Removes the browser’s default 8px body margin — a very common reset.
line-height
Controls vertical spacing of text. 1.5–1.7 is comfortable for body copy.
max-width
Limits how wide the content stretches, keeping lines readable on large screens.
Browsers add a default 8px margin around the body. Most stylesheets begin by removing
it with body { margin: 0; }. This is one of the most common CSS rules in
existence.
Where Scripts Go in the Body
Scripts are often placed at the very end of the body, right before the closing
</body> tag. This ensures the HTML has been parsed and the DOM is ready
before the script runs.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Script at the End</title>
</head>
<body>
<h1>Page Title</h1>
<button id="myBtn">Click me</button>
<script>
document.getElementById("myBtn").addEventListener("click", function () {
alert("Button clicked!");
});
</script>
</body>
</html>
The <script> element is one of the last things in the body because it
is the last thing the browser needs. The page renders first, then the script runs.
| Location | Behavior | Best for |
|---|---|---|
| Head (no attribute) | Blocks parsing | Avoid |
Head with defer |
Loads in parallel, runs after parsing | Scripts needing the DOM |
Head with async |
Loads in parallel, runs when ready | Analytics, independent scripts |
| End of body | Page renders first | Most scripts |
A Complete Document
Here is a full, well-formed HTML document showing the body in its proper place with a complete structure of landmarks, semantic content, and a script.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>A Complete HTML Document</title>
</head>
<body>
<header>
<h1>My Website</h1>
<nav>
<a href="index.html">Home</a>
<a href="about.html">About</a>
</nav>
</header>
<main>
<h2>Welcome</h2>
<p>
This document uses the body element as the container for
all visible content, following the standard HTML file structure.
</p>
</main>
<footer>
<p>© 2025 Inar Learn</p>
</footer>
</body>
</html>
That is the whole pattern. Every page you build will follow it, with varying content inside the body.
Copy this pattern into a file called template.html and reuse it every time
you start a new page. Once your structure is in place, you can focus on content.
Common <body> Mistakes
A handful of mistakes come up again and again with the body element. Here are the ones to watch for.
1. Putting visible content outside the body
<html lang="en">
<head>...</head>
<p>This is outside the body.</p>
<body>...</body>
</html><html lang="en">
<head>...</head>
<body>
<p>This is inside the body.</p>
</body>
</html>2. Forgetting the closing tag
<body>
<h1>Title</h1>
<p>Content</p>
<!-- Missing </body> -->
</html><body>
<h1>Title</h1>
<p>Content</p>
</body>
</html>3. Adding a second body element
<body>
<p>First body</p>
</body>
<body>
<p>Second body</p>
</body><body>
<p>Only one body</p>
</body>4. Using obsolete presentational attributes
<body bgcolor="#fff"
text="#000"
link="#00f"
marginwidth="0">body {
background: #fff;
color: #000;
margin: 0;
}5. Using onload instead of addEventListener
<body onload="init()">
<!-- Mixes behavior with structure --><body>
...
<script>
window.addEventListener("load", init);
</script>
</body>6. Scripts before the DOM is ready
<body>
<script>
// Fails — button does not exist yet
document.getElementById("btn").onclick = ...
</script>
<button id="btn">Click</button>
</body><body>
<button id="btn">Click</button>
<script>
document.getElementById("btn").onclick = ...
</script>
</body>7. Multiple main elements
<body>
<main>First main</main>
<main>Second main</main>
</body><body>
<main>
The primary content of the page
</main>
</body>8. Forgetting the body entirely
<!DOCTYPE html>
<html lang="en">
<head>
<title>Page</title>
</head>
<h1>Title</h1>
<p>Content</p>
</html>
<!-- Browser inserts empty body, then
moves content into it — invalid --><!DOCTYPE html>
<html lang="en">
<head>
<title>Page</title>
</head>
<body>
<h1>Title</h1>
<p>Content</p>
</body>
</html>The W3C Markup Validation Service will flag content outside the body, missing closing tags, multiple body elements, and obsolete attributes. Running every page through it before publishing is a habit worth building.
Best Practices
A few habits make the body of a page clean, accessible, and easy to maintain.
Use semantic landmarks
Structure content with header, nav, main, and footer, not just div.
One main per page
Wrap the primary content in a single visible main element.
Put scripts at the end
Load JavaScript right before </body> so the DOM is ready.
Set global styles on the body
Font, color, background, and margin defaults belong on the body element.
No presentational attributes
Never use bgcolor, text, or link — use CSS instead.
No inline event handlers
Use addEventListener in a script instead of onload on the body.
Reset the default margin
Add body { margin: 0; } at the top of your stylesheet.
Validate before publishing
Run every page through the W3C validator to catch structural problems early.
One body per page, containing all visible content, structured with semantic landmarks,
and closed with </body> before </html>.
Frequently Asked Questions
1. What is the body element in HTML?
The <body> element is a container for all the visible content on a
web page — headings, paragraphs, images, links, forms, and interactive elements.
Everything the visitor sees is inside the body.
2. Is the body element required?
Yes, effectively. Browsers will insert an empty body automatically if one is missing, but every valid HTML document should include an explicit body element that contains the visible content.
3. What is the difference between head and body?
The head contains metadata about the page — information for browsers, search engines, and other software. The body contains everything the visitor sees and interacts with on screen.
4. Can I have more than one body element?
No. A document must have exactly one <body> element, and it must be
the second child of <html>. Any content that appears after the body
closes is either ignored or moved into the body by the browser.
5. What attributes can the body element have?
The <body> element accepts global attributes like
id, class, and style. It also accepts event
handler attributes like onload, onclick, and
onscroll, though these are better handled by JavaScript. Legacy
presentational attributes like bgcolor are obsolete.
6. What should go inside the body?
All visible content: headings (h1–h6), paragraphs, images,
links, lists, tables, forms, and semantic landmarks like header,
nav, main, article, section,
aside, and footer.
7. Should I use onload or DOMContentLoaded?
Use DOMContentLoaded for scripts that need the DOM ready, or
addEventListener('load', ...) on the window object. The
onload attribute on body is legacy and mixes behavior with structure.
8. What is the difference between body and main?
The <body> is the container for all visible content — the header,
the main content, the footer, and everything else. The <main>
element is a semantic landmark inside the body that wraps only the primary content of
the page.
9. Can I style the body element?
Yes. The body element is styled like any other element. Common patterns include
setting font-family, background-color, color,
and margin on the body to define global defaults for the page.
10. How many main elements can a page have?
A page should have exactly one visible <main> element, though you
can have multiple main elements if some are hidden. The <main>
element identifies the primary content of the page for screen readers and search
engines.
11. What is the difference between body and html?
The <html> element is the root of the document and contains both
the head and the body. The <body> contains only the visible
content. The <html> element also carries the lang
attribute for the document.
12. Where should I put scripts — head or body?
Scripts are best placed at the end of the body, right before the closing tag. This ensures the HTML has been parsed and the DOM is ready before the script runs, avoiding errors from missing elements.
Key Takeaways
- The
<body>element contains all visible content — everything the visitor sees, reads, or interacts with. - It is the second child of
<html>, following the<head>. - Every document has exactly one body, and it must have both an opening and a closing tag.
- The body accepts almost any content: text, links, media, lists, tables, forms, and semantic landmarks.
- Use semantic landmarks like
<header>,<nav>,<main>, and<footer>to structure the body meaningfully. - A page should have exactly one visible
<main>element wrapping its primary content. - Global CSS defaults — font, color, background, margin — are typically set on the body element.
- Scripts belong at the end of the body, right before
</body>. - Obsolete attributes like
bgcolorandonloadshould be replaced with CSS andaddEventListener. - Validate your HTML to catch missing body tags, multiple body elements, or content placed outside the body.
What to Learn Next
Now that you understand the <body> element, the next step is to learn
the parent and sibling elements. Start with
HTML <head> Element: Complete Guide
for the invisible metadata container, then explore
HTML <html> Element: Syntax and Examples
and
HTML File Structure.
For deeper coverage, explore HTML Elements, HTML Attributes, and HTML Nesting Explained. To understand how the browser renders the body, read How Web Browsers Work and How Websites Work.
Practice Challenge
Build a complete, well-structured HTML page that exercises the body element in every way covered in this guide. Your page should include:
- A
<!DOCTYPE html>and an<html lang="en">root element - A head with
<meta charset>, a<meta viewport>tag, and a<title> - A
<body>element containing a<header>with a heading and a<nav> - A single
<main>element with at least one<article>and one<section> - An
<aside>element with a small side note - A
<footer>with copyright text - A
<style>block in the head that setsfont-family,background,color, andmargin: 0on the body - A
<script>at the very end of the body, just before</body>
Once your page is complete, open it in DevTools and inspect the DOM. Confirm that the
<body> element is the second child of <html>, that
the landmarks are nested inside it, and that the script is at the end.
As a final test, run the page through the W3C validator. A clean result means your body structure is correct — and you have mastered the container that holds every visible element on the web.