The HTML <body> tag
The HTML <body> element contains all the visible content of the page — text, images, links, everything the user sees. There is exactly one per document, and it also hosts window-level event handlers like onload.
Overview
The <body> element holds everything that is displayed on the page — text, images, links, all of it. It is the second and final child of <html>, coming after the <head>, and there is exactly one per document.
It is also the home of the window-level event handler attributes — onload, onresize, onhashchange and similar — which fire for events on the window rather than the body itself. Attaching these with addEventListener on window in JavaScript is generally preferred to the inline attributes, since it keeps behavior out of the markup and allows multiple handlers.
Its old presentational attributes (background color, text color, link colors) are obsolete; style the body with CSS instead.
Syntax
<body>
<h1>Welcome</h1>
<p>Page content goes here.</p>
</body>
Attributes
The <body> element supports the following attributes, in addition to the global attributes available to every HTML element.
| Attribute | Value | Description |
|---|---|---|
onafterprint |
Runs JavaScript after printing. | |
onbeforeprint |
Runs JavaScript before printing. | |
onbeforeunload |
Runs JavaScript before the page unloads, to warn about unsaved changes. | |
onhashchange |
Runs JavaScript when the URL hash changes. | |
onload |
Runs JavaScript when the element or page finishes loading. | |
onmessage |
Runs JavaScript when a message is received. | |
onoffline |
Runs JavaScript when the browser goes offline. | |
ononline |
Runs JavaScript when the browser comes back online. | |
onpopstate |
Runs JavaScript on history navigation. | |
onresize |
Runs JavaScript when the window is resized. | |
onstorage |
Runs JavaScript when storage changes in another tab. |
Example
<body>
<h1>Hello, world</h1>
<p>This is the visible page content.</p>
</body>
More Examples
Best practices
Frequently asked questions
What is the body element?
How many body elements can a document have?
What are the on* attributes on the body element?
onload and onresize. Attaching them with window.addEventListener is generally preferred.What is the difference between the body and the head?
<body> holds the visible page content.