HTML DOM Document body

Examples

Get the HTML content of a document:

const myBody = document.body.innerHTML;
Try it Yourself »

Change the background color of a document:

document.body.style.backgroundColor = "yellow";
Try it Yourself »

Change the <body> of a document (overwrite all existing content):

document.body.innerHTML = "Some new HTML content";
Try it Yourself »

More examples below.


Description

The body property sets or returns a document's <body> element.

Warning

Setting the body property overwrites all elements in the document's <body>.

Note

The difference between document.body and document.documentElement:

document.body returns the <body> element.
document.documentElement returns the <html> element.

See Also:

The Document documentElement Property

The HTML <body> tag

The HTML DOM Body Object


Syntax

Return the body property:

document.body

Set the body property:

document.body = newContent

Property Values

Value Description
newContent The new content for the <body> element.

Return Value

Type Description
ObjectThe body element of the document.

More Examples

Create a <p> element and append it to the document's body:

const para = document.createElement("p");
const node = document.createTextNode("This is a paragraph.");

para.appendChild(node);
document.body.appendChild(para);
Try it Yourself »

Browser Support

document.body is a DOM Level 1 (1998) feature.

It is fully supported in all browsers:

Chrome Edge Firefox Safari Opera IE
Yes Yes Yes Yes Yes 9-11


Copyright 1999-2023 by Refsnes Data. All Rights Reserved.