HTML <body> tagS2C Home   « HTML <body> tag

Definition

The <body> tag defines the document body of a HTML file.

Example


<!DOCTYPE html>   <!-- The DOCTYPE declaration always comes first in a HTML document --> 
<html lang="en">
  <!-- Everything else goes between the start and end HTML tags. --> 
  <head>
    <!-- The header contains meta information pertaining to the HTML document as well as a title and other stuff we will discuss in HTML Intermediate/Advanced lessons. External files such as CSS stylesheets and JavaScript files are also imported in the header section of our HTML file. -->
    <title>Appears in browser toolbar, favourites and search engine results.</title>
  </head>
  <body>
    <!-- The content of the web page appears in the body section. -->
  </body>
  <!-- We complete the HTML file with the closing HTML tag. -->
</html>

Attributes

The following attributes can be used with the <body> tag.


<body> Specific Attributes
Attribute Value Description
The following global attributes are specific to the <body> tag.
Window
onafterprintThe script to be run after the document is printed.

 <!-- Executes go() function -->
<body onafterprint="go()">HTML content here</body>
onbeforeprintThe script to be run before the document is printed.

 <!-- Executes go() function -->
<body onbeforeprint="go()">HTML content here</body>
onbeforeunloadThe script to be run when the document is about to be unloaded.

 <!-- Executes go() function -->
<body onbeforeunload="go()">HTML content here</body>
onhashchangeThe script to be run the fragment identifier part of the a URL (starting with the hash # character) has changed.

 <!-- Executes go() function -->
<body onhashchange="go()">HTML content here</body>
onerrorThe script code to be run when an error occurs when the document is being loaded.

 <!-- Executes go() function -->
<body onerror="go()">HTML content here</bdi>
onloadThe script code to be run when the document is loaded.

 <!-- Executes go() function -->
<body onload="go()">HTML content here</bdi>
onmessageThe script to be run when a message has triggered.

 <!-- Executes go() function -->
<body onmessage="go()">HTML content here</body>
onofflineThe script to be run when the browser starts to work offline.

 <!-- Executes go() function -->
<body onoffline="go()">HTML content here</body>
ononlineThe script to be run when the browser starts to work ononline.

 <!-- Executes go() function -->
<body ononline="go()">HTML content here</body>
onpagehideThe script to be run when a user navigates away from a page.

 <!-- Executes go() function -->
<body onpagehide="go()">HTML content here</body>
onpageshowThe script to be run when a user navigates to a page.

 <!-- Executes go() function -->
<body onpageshow="go()">HTML content here</body>
onpopstateThe script to be run when the window's history changes.

 <!-- Executes go() function -->
<body onpopstate="go()">HTML content here</body>
onresizeThe script to be run when the document is resized.

 <!-- Executes go() function -->
<body onresize="go()">HTML content here</body>
onstorageThe script to be run when a web storage area is updated.

 <!-- Executes go() function -->
<body onstorage="go()">HTML content here</body>
onunloadThe script to be run when the document is unloaded.

 <!-- Executes go() function -->
<body onunload="go()">HTML content here</body>
<body> Global Attributes
Attribute Description Example
Common
classSpecifies a classname for the element allowing you to apply the style of the predefined class to the content.

<body class="name">HTML content here</body>
idSpecifies a unique id for the element allowing you to apply the style of the predefined id to the content.

<body id="name">HTML content here</body>
styleSpecifies an inline style for the element allowing you to apply the style to the content.

<body style="color:red;">HTML content here</body>
titleSpecifies an inline style for the element allowing you to apply the style to the content.

<body title="Content info">HTML content here</body>
Keyboard
accesskeySpecifies a keyboard shortcut to associate with the element.

<body accesskey="a">HTML content here</body>
tabindexSpecifies a tab order for the element.

<body tabindex="1">HTML content here</body>
Language
dirSpecifies the directional flow of the content.

<!-- The text will flow from left to right -->
<body dir="ltr">HTML content here</body>
<!-- The text will flow from right to left -->
<body dir="rtl">HTML content here</body>
langSpecifies a language code for the content of the element.

<body lang="en">HTML content here</body>
spellcheckSpecifies an inline style for the element allowing you to apply the style to the content.

<!-- Valid values true and false. -->
<!-- Default inherited / browser specific. -->
<body spellcheck="true">HTML content here</body> 
translateSpecifies an inline style for the element allowing you to apply the style to the content.

<!-- Valid values yes and no. -->
<!-- Default yes. -->
<body translate="no">HTML content here</body> 
Miscellaneous
contenteditableSpecifies whether the content of the element is editable.

<!-- Valid values true and false. -->
<!-- Default inherited. -->
<body contenteditable="true">HTML content here</body> 
draggableSpecifies whether the element is draggable.

<!-- Valid values true and false. -->
<!-- Default browser specific. -->
<body draggable="true">HTML content here</body>
hiddenSpecifies whether the element is not yet, or no longer, relevant.

<!-- Valid values an empty string or hidden -->
<body hidden>HTML content here</body> 
<body hidden="hidden">HTML content here</body> 
<body> Event Attributes
Attribute Description Example
Document Element
oncopyThe script to be run when the user copies the content of an element.

 <!-- Executes go() function -->
<body oncopy="go()">HTML content here</body> 
oncutThe script to be run when the user cuts the content of an element.

 <!-- Executes go() function -->
<body oncut="go()">HTML content here</body>
onpasteThe script to be run when the user pastes some content into an element.

 <!-- Executes go() function -->
<body onpaste="go()">HTML content here</body>
Drag and Drop
ondragThe script to be run when an element is dragged.

 <!-- Executes go() function -->
<body ondrag="go()">HTML content here</body>
ondragendThe script to be run when an element has stopped being dragged.

 <!-- Executes go() function -->
<body ondragend="go()">HTML content here</body>
ondragenterThe script to be run when an element has been dragged to a valid drop target.

 <!-- Executes go() function -->
<body ondragenter="go()">HTML content here</body>
ondragleaveThe script to be run when an element leaves a valid drop target.

 <!-- Executes go() function -->
<body ondragleave="go()">HTML content here</body>
ondragoverThe script to be run when an element is being dragged over a valid drop target.

 <!-- Executes go() function -->
<body ondragover="go()">HTML content here</body>
ondragstartThe script to be run at the start of a drag operation.

 <!-- Executes go() function -->
<body ondragstart="go()">HTML content here</body>
ondropThe script to be run when a dragged element is being dropped.

 <!-- Executes go() function -->
<body ondrop="go()">HTML content here</body>
Form
onblurThe script to be run when the element loses focus.

 <!-- Executes go() function -->
<body onblur="go()">HTML content here</body>
onchangeThe script to be run when object changed and attempt to leave field.

 <!-- Executes go() function -->
<body onchange="go()">HTML content here</body>
oncontextmenuThe script to be run when a context menu is triggered.

 <!-- Executes go() function -->
<body oncontextmenu="go()">HTML content here</body>
onfocusThe script to be run when the element gets focus.

 <!-- Executes go() function -->
<body onfocus="go()">HTML content here</body>
oninputThe script to be run when an element gets user input.

 <!-- Executes go() function -->
<body oninput="go()">HTML content here</body>
oninvalidThe script to be run when an element is invalid.

 <!-- Executes go() function -->
<body oninvalid="go()">HTML content here</body>
onresetThe script to be run when a dragged element is being dropped.

 <!-- Executes go() function -->
<body onreset="go()">HTML content here</body>
onselectThe script to be run when some or all of the contents of an object are selected.

 <!-- Executes go() function -->
<body onselect="go()">HTML content here</body>
onsubmitThe script to be run when a form is submitted.

 <!-- Executes go() function -->
<body onsubmit="go()">HTML content here</body>
Keyboard
onkeydownThe script to be run when an element is in focus and keyboard key is pressed down.

 <!-- Executes go() function -->
<body onkeydown="go()">HTML content here</body>
onkeypressThe script to be run when an element is in focus and keyboard key is pressed down and released.

 <!-- Executes go() function -->
<body onkeypress="go()">HTML content here</body>
onkeyupThe script to be run when an element is in focus and keyboard key is released.

 <!-- Executes go() function -->
<body onkeyup="go()">HTML content here</body>
Media
onabortThe script code to be run on abort.

 <!-- Executes go() function -->
<body onabort="go()">HTML content here</body>
oncanplayThe script to be run when a file has buffered enough so it is ready to start playing.

 <!-- Executes go() function -->
<body oncanplay="go()">HTML content here</body>
oncanplaythroughThe script to be run when a file can be played all the way to the end without further need of buffering.

 <!-- Executes go() function -->
<body oncanplaythrough="go()">HTML content here</body>
oncuechangeThe script to be run when the cue changes when using the track element.

 <!-- Executes go() function -->
<body oncuechange="go()">HTML content here</body>
ondurationchangeThe script to be run when the length of the media is changed.

 <!-- Executes go() function -->
<body ondurationchange="go()">HTML content here</body>
onemptiedThe script to be run when a media resource element suddenly becomes empty, usually due to an error.

 <!-- Executes go() function -->
<body onemptied="go()">HTML content here</body>
onendedThe script to be run when the media has reach the end.

 <!-- Executes go() function -->
<body onended="go()">HTML content here</body>
onloadeddataThe script to be run when media data is loaded and playback can start.

 <!-- Executes go() function -->
<body onloadeddata="go()">HTML content here</body>
onloadedmetadataThe script to be run when metadata has been loaded.

 <!-- Executes go() function -->
<body onloadedmetadata="go()">HTML content here</body>
onloadstartThe script to be run whenthe media resource has started loading.

 <!-- Executes go() function -->
<body onloadstart="go()">HTML content here</body>
onpauseThe script to be run when the media resource has been paused.

 <!-- Executes go() function -->
<body onpause="go()">HTML content here</body>
onplayThe script to be run when the media resource starts playback.

 <!-- Executes go() function -->
<body onplay="go()">HTML content here</body>
onplayingThe script to be run when when playback has already begun.

 <!-- Executes go() function -->
<body onplaying="go()">HTML content here</body>
onprogressThe script to be run when the browser is fetching the media data.

 <!-- Executes go() function -->
<body onprogress="go()">HTML content here</body>
onratechangeThe script to be run when the playback rate changes.

 <!-- Executes go() function -->
<body onratechange="go()">HTML content here</body>
onseekedThe script to be run when the seeking attribute is set to false indicating that seeking has finished.

 <!-- Executes go() function -->
<body onseeked="go()">HTML content here</body>
onseekingThe script to be run when the seeking attribute is set to true indicating that seeking is currently active.

 <!-- Executes go() function -->
<body onseeking="go()">HTML content here</body>
onstalledThe script to be run when the browser is unable to continue fetching media data.

 <!-- Executes go() function -->
<body onstalled="go()">HTML content here</body>
onsuspendThe script to be run when media data has stopped before being completely loaded.

 <!-- Executes go() function -->
<body onsuspend="go()">HTML content here</body>
ontimeupdateThe script to be run when the media resources current playback position has changed.

 <!-- Executes go() function -->
<body ontimeupdate="go()">HTML content here</body>
onvolumechangeThe script to be run when the volume has changed or been muted.

 <!-- Executes go() function -->
<body onvolumechange="go()">HTML content here</body>
onwaitingThe script to be run when the media resource has paused but is expected to resume.

 <!-- Executes go() function -->
<body onwaiting="go()">HTML content here</body>
Mouse
onclickThe script to be run when when a mouse is clicked on an element.

 <!-- Executes go() function -->
<body onclick="go()">HTML content here</body>
ondblclickThe script to be run when a mouse is double clicked on an element.

 <!-- Executes go() function -->
<body ondblclick="go()">HTML content here</body>
onmousedownThe script to be run when he mouse button is pressed down while the cursor is over an element.

 <!-- Executes go() function -->
<body onmousedown="go()">HTML content here</body>
onmousemoveThe script to be run when the mouse button is moved.

 <!-- Executes go() function -->
<body onmousemove="go()">HTML content here</body>
onmouseoutThe script to be run when the mouse cursor moves off an element.

 <!-- Executes go() function -->
<body onmouseout="go()">HTML content here</body>
onmouseoverThe script to be run when the mouse cursor moves over an element.

 <!-- Executes go() function -->
<body onmouseover="go()">HTML content here</body>
onmouseupThe script to be run when the mouse button is released while the cursor is over the element.

 <!-- Executes go() function -->
<body onmouseup="go()">HTML content here</body>
onwheelThe script to be run when the mouse wheel rolls up or down over an element.

 <!-- Executes go() function -->
<body onwheel="go()">HTML content here</body>
Scroll
onscrollThe script code to be run when the scrollbar of an element is being scrolled.

 <!-- Executes go() function -->
<body onscroll="go()">HTML content here</body> 

Relevant HTML Tutorials

HTML Basics - HTML Structure - Document