HTML DOM

HTML DOM body Property

The body property of the document object sets or returns the document's <body> element, giving you direct access to the main content area of the page.


Definition and Usage

document.body returns the <body> element of the current document (or the <frameset> element for frameset documents). You can also assign a new body element to it to replace the existing one.

Syntax

// Return the body element
document.body

// Replace the body element
document.body = newBodyElement

Reading returns the HTMLBodyElement object; writing expects a <body> or <frameset> element.

Example

// Change the page background and add content
document.body.style.backgroundColor = "lightyellow";
document.body.innerHTML += "<p>Added to the body!</p>";

// Read how many child elements the body has
console.log("Children: " + document.body.children.length);

The example styles the whole page by setting the body's background color, appends a paragraph, then logs the number of element children inside <body>. All changes render immediately.

💡

document.body is available only after the <body> tag is parsed. Access it in a script at the end of the page or inside a DOMContentLoaded event to be safe.

Ready to use your HTML skills?

Find web development internships and fresher jobs across India.

Browse Web Dev Internships