HTML5 <body> Tag

The HTML <body> tag is used to represent the content of an HTML document. There can be only one <body> element in a document.

The only content permitted inside this element is Flow content. You can omit the start tag for this element if the first thing inside it is not a space character, comment, <script> or <style> element. The end tag may be omitted if the body element either has some content or has a start tag. Additionaly, the end must not be immediately followed by a comment.

The parent of the <body> tag should be the an <html> element.

The following style is applied to this element by default:

body {
  display: block;
  margin: 8px;
}

body:focus {
  outline: none;
}

Here is an example of using this tag:

<!DOCTYPE HTML>
<html>
  <head>
    <title>Online or offline?</title>
  </head>
  <body ononline="update(true)"
    onoffline="update(false)"
    onload="update(navigator.onLine)">
    <p>You are: <span id="status">(Unknown)</span></p>
    <script>
      function update(online) {
        document.getElementById('status').textContent = online ? 'Online' : 'Offline';
      }
    </script>
  </body>
</html>

Attributes

onafterprint

This attribute is used to specify the function that should be called after the user has printed the document.

onbeforeprint

This attribute is used to specify the function that should be called when the user requests printing of the document.

onbeforeunload

This attribute is used to specify the function that should be called when the document is about to be unloaded.

onblur

This attribute is used to specify the function that should be called when the document loses focus.

onerror

This attribute is used to specify the function that should be called when the document fails to load properly.

onfocus

This attribute is used to specify the function that should be called when the document receives focus.

onhashchange

This attribute is used to specify the function that should be called when the fragment identifier part (starting with the hash (‘#’) character) of the document’s current address has changed.

onlanguagechange

This attribute is used to specify the function that should be called when the preferred languages changed.

onload

This attribute is used to specify the function that should be called when the document has finished loading.

onmessage

This attribute is used to specify the function that should be called when the document has received a message.

onoffline

This attribute is used to specify the function that should be called when network communication has failed.

ononline

This attribute is used to specify the function that should be called when network communication has been restored.

onpopstate

This attribute is used to specify the function that should be called when the user has navigated session history.

onredo

This attribute is used to specify the function that should be called when the user has moved forward in undo transaction history.

onresize

This attribute is used to specify the function that should be called when the document has been resized.

onstorage

This attribute is used to specify the function that should be called when the storage area has changed.

onundo

This attribute is used to specify the function that should be called when the user has moved backward in undo transaction history.

onunload

This attribute is used to specify the function that should be called when the document is going away.

Examples

See the Pen HTML body Tag by Tutorialio (@tutorialio) on CodePen.

Further Reading

  1. Read more about the HTML <body> element on MDN.
  2. You can also read the HTML <body> tag reference on W3C.

Reader Comments

1. You can register or login to post a comment. Asking readers to register improves the quality of discussion.

2. As a registered user, you will also get to access features like choosing between a light and dark theme or anything else that we might implement in future.

Follow Us For Updates

FacebookTwitterGoogle+
Go To Top