HTML body Tag

HTML <body> tag is used to define the body section of the HTML document. The <body> tag contains the main content of the document. The <body> tag is placed after the closing <head> tag. It's one the default HTML tag which is used while creating an HTML document.

HTML body tag example

  • The <body> tag includes texts, images, hyperlinks, lists, paragraphs, headings, etc., and everything else that can be a part of the HTML webpage.

  • The <body> tag is the second direct child of the HTML element(<html> tag), second to the <head> tag.

  • The <body> tag is used to display the main content of the website that is visible to the visitors visiting on the website in the browser. One important thing to note is that you can not use multiple pairs of <body> tags inside a single HTML document.

Everything you see on a webpage in a browser window is inside the <body> tag.

HTML <body> Tag - Syntax and Usage

The <body> tag requires both opening (start) tag and closing(end) tag.

Following is the syntax for using the <body> tag:

<body>
    <!-- the whole webpage code comes here -->
</body>

HTML <body> Tag Attributes

HTML <body> tag supports Global attributes and Event attributes and some of the common event attributes are given below:

Attribute Description
onafterprint It calls a function after the user has printed a document.
onbeforeprint It calls a function when the user requests for printing a document.
onbeforeunload It calls a function when the document is about to be unloaded.
onhashchange It calls a function when the hash part of the URL address changes for the current webpage. For example, when studytonight.com/code/html/web-beginner#1 changes to studytonight.com/code/html/web-beginner#2
onload It calls a function when the page finishes its loading.
onfocus It is used to call a function when the visitor focuses on the current Page.
onunload It is used to call a function when visitor Leaves the Page.
onblur It is used to call a function when the web page loses its focus.
onerror this attribute is used to call a function when the web page fails to load
onmessage It is used to call a function when the document has received a message.
onresize It is used to call a function when the document has been resized.
onredo It is used to call a function when the user has moved forward in undo transaction history.
onlanguagechange This attribute is used to call a function when the preferred language change.
onstorage This attribute is used to call a function when the storage area has changed.
onundo It is used to call a function when the user has moved backward in the undo transaction history.
onoffline It calls a function when the internet connection is lost.
ononline It calls a function when the internet connection is restored.

In HTML 4 there were some other attributes like rightmargin, topmargin, leftmargin, bottommargin, vlink, link, text, etc. available in <body> tag but these were deprecated in HTML 5.

HTML <body> Tag Basic Example

Any example for HTML webpage has the <body> tag in it, but still, let's take one more example to see it in action, although whenever you write any HTML code the <body> tag is always in action:

Default CSS Style for HTML <body> tag

Following is the default CSS style rule applied on the <body> tag by default. This may change slightly as per browsers.

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

Browser Support for HTML <body> tag

All the browsers support it, still, for the sake of this section, the following browsers support this attribute:

  • Firefox 1+

  • Google Chrome 1+

  • Internet Explorer 2+

  • Apple Safari 1+

  • Opera 2.1+