JavaScript – Document.body

JavaScript Document.body property gives access to the <body> or <frameset> element of the document.

Syntax

The syntax to read body property of document is

</>
Copy
document.body

Document.body property is both readable and writable. We can either read the element from the property, or set an element to the property.

Examples

In the following example, we get the style property of the <body> element present in this document by using Document.body property.

index.html

</>
Copy
<!DOCTYPE html>
<html lang="en">
<body style="color:red;background:greenyellow">
  <p id="output"></p>
  <script>
    var output = document.getElementById('output');
    var x = document.body;
    output.innerHTML = x.style.cssText;
  </script>
</body>
</html>

Conclusion

In this JavaScript Tutorial, we have learnt about Document.body property, with examples.