HTML <body> Tag for Body Section

 

ATTENTION: THIS PAGE IS Valid HTML 5 AND IS BEST VIEWED WITH HTML 5 - Please upgrade your browser or download one of the HTML 5 compatible browsers such as Mozilla Firefox, Chrome, Opera or IE 9 (March 14, 2011 or later). For more information see HTML 5 browsers.


If you find this helpful, please click the Google +1 Button to the left, if it is white, to make it turn blue or red. Thank you! (It also helps find this page again more easily.)


PDF mobile

The <body> Tag in HTML 5

The <body> tag creates the body section of the document, which contains the actual visible content of the document. The body element is the second of the two elements making up the inner HTML of the top html element, following the </head> end tag for the head element, which is the first element in the top html element inner HTML. The <body> tag is one of the structure tags in HTML. It is also one of the sectioning root tags, which means that the sections and headings inside the body element are not included in the outline of any higher level sections.

HTML <body> with fixed background

This

text

scrolls

vertically

but

the

background

stays

fixed.

See the <body> tag example code below.


<body> Tag Syntax

<html xmlns="http://www.w3.org/1999/xhtml">
   <head>
      <title>My Title</title>
      ... metadata content ...
   </head>
   <body>
      ... flow content ...
   </body>
</html>
Rules for coding the HTML body element

Make sure you understand the difference between a tag and element and are familiar with the definitions of namespace and other HTML terms.

  1. Code the body element for the body section after the end of the head element inside the html element.
  2. Begin the body element with a starting <body> tag. The element name uses lower case letters and should be in the HTML namespace, which it will pick up automatically from the xmlns attribute on the <html> tag.
  3. Code the content of the document inside the body element.
  4. End the body element with a matching </body> closing tag.
Content of the body element

The content of the body element can include HTML comments, text content and any tags that can be used in flow content.


<body> Tag Attributes

Attributes of the <body> tag
global attributes In addition to the personal attributes of the <body> tag below, any of the common HTML attributes can also be coded.
onload When the onload attribute is specified on the <body> tag, the event is for loading of the DOM window object rather than the body node.
onscroll When the onscroll attribute is specified on the <body> tag, the event is for scrolling of the DOM window object rather than the body node.
onfocus When the onfocus attribute is specified on the <body> tag, the event is for the focus of the DOM window object rather than the body node.
onblur When the onblur attribute is specified on the <body> tag, the event is for loss of focus of the DOM window object rather than the body node.
onerror When the onerror attribute is specified on the <body> tag, the event is for errors for the DOM window object rather than the body node.

<body> Tag Examples

Examples of the body tag in HTML 5
HTML <body> with fixed background
(see <body> tag demo above)
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="/site-template.xsl"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
   <head>
      <link rel="stylesheet" type="application/xslt+xml" href="/site-template.xsl"/>
      <title>Example Only</title>
   </head>
   <body style="background: #ffffff url('/bg-image.jpg') repeat fixed">
      <h1>Here Is Your Heading</h1>
      <p>This is the first paragraph of your web page.
      </p>
   </body>
</html>

Changes in HTML 5 - <body> Tag

What's new in HTML 5
Differences between HTML 5 and earlier versions of HTML

The following attributes should not be coded on the <body> tag because they either have been deprecated or were never officially supported:

  • alink
  • background
  • bgcolor
  • bgproperties
  • bordercolor
  • bordercolordark
  • bordercolorlight
  • leftmargin
  • link
  • rightmargin
  • text
  • vlink

The 2000-2010 Recommendations from the W3C HTML Working Group defined the HTML namespace for the body element type name along with the names of all HTML element types. In older (pre-2000) versions of HTML, element type names were not associated with a namespace.


Valid HTML 5