Skip to content
Tutorials Center
My learning

Topics

HTML Reference ยท Document and Metadata

The <body> element

Reference for the HTML <body> element: what belongs inside it, window event handler attributes, obsolete styling attributes, examples and technical details.

The <body> element contains everything a visitor actually sees and interacts with: headings, text, images, forms, navigation and so on. It is the second child of <html>, right after <head>, and every document has exactly one.

HTML
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Trail report</title>
  </head>
  <body>
    <h1>Trail report</h1>
    <p>All paths are open this weekend.</p>
  </body>
</html>

Try it

Styling the body and reacting to window events

Browsers give <body> a default margin of 8px. Most style sheets reset it to 0, as above, and add their own padding.

Attributes

<body> accepts the global attributes. It also accepts a set of window event handler attributes, because events fired on the window can be handled from the body tag:

Attribute Fires when
onafterprint, onbeforeprint Printing starts or has finished
onbeforeunload The page is about to be unloaded
onhashchange The fragment (#...) of the URL changes
onlanguagechange The user's preferred languages change
onmessage, onmessageerror The window receives a message from another window or worker
onoffline, ononline The browser loses or regains its network connection
onpagehide, onpageshow The page is hidden or shown, including from the back/forward cache
onpopstate The user moves through the session history
onstorage Web storage is changed by another page of the same origin
onunhandledrejection, onrejectionhandled A promise rejection is not handled, or is handled late

Old presentational attributes such as bgcolor, background, text, link, vlink, alink and the margin attributes are obsolete. Use CSS instead.

Usage notes

  • One per document. If you leave out the tags, the parser still creates a <body> for you; writing it out simply makes the structure obvious. See Your First Web Page.
  • Metadata belongs in the head. <title>, <meta> and <base> must go in <head>. A few elements, like <script> and some kinds of <link>, are allowed in both.
  • Prefer addEventListener. The on... attributes work, but registering handlers on window in a script keeps HTML and behavior separate and allows more than one handler per event. See Event Attributes and Event Listeners.
  • Structure the content inside. Use landmarks such as <header>, <main> and <footer> directly inside the body, so assistive technology can jump between regions. Page Layout shows a typical skeleton.
  • document.body gives scripts direct access to this element.

Technical summary

Content categories None
Permitted content Flow content
Tag omission The start tag may be omitted if the body is empty, or if its first content is not a space, a comment, or a <meta>, <noscript>, <link>, <script>, <style> or <template> element. The end tag may be omitted if it is not immediately followed by a comment.
Permitted parents An <html> element, as its second child
Implicit ARIA role generic
DOM interface HTMLBodyElement
Default display block (with an 8px margin)

Browser support

Supported in every browser.