How to Get the body Element with document.body in JavaScript
document.body is the page's <body> element, ready to use without searching for it. Change it to affect the whole visible page, for example its colors or a data-* attribute that your CSS reacts to. It's null until the browser has reached the <body> tag, so use it from a script inside the body.
Syntax
document.body
| Part | What it does |
|---|---|
document | The page. |
.body | Its <body> element, the one that holds everything shown on the page. It's null while the browser is still reading the <head>. |
MDN's Document: body property page describes it.
The Example
The example page has one paragraph. A script at the end of the body gives document.body a dark background and white text, so the whole page turns dark.
Step 1: Write the Code
Create a file named index.html in a text editor, such as Visual Studio Code:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>document.body</title>
</head>
<body>
<p>This page gets a dark background from JavaScript.</p>
<script>
document.body.style.background = '#292929';
document.body.style.color = '#ffffff';
</script>
</body>
</html>
style sets a CSS property on the element directly: background for its background, and color for its text. #292929 is a dark grey and #ffffff is white.
Step 2: Open It in a Browser
Double-click index.html. It opens in your browser:

The screenshot comes from Chromium 159, the open-source browser that Chrome is built on.
What Happened
document.bodywas the<body>element, so the colors applied to the whole page.- The paragraph turned white too. It has no color of its own, so it takes the body's.
- The script found the body because it's inside it. When the browser runs that script, the
<body>tag has already been read.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Using document.body in a script in <head> | document.body is still null: the browser's console showed Cannot read properties of null (reading 'style'), and the background stayed white | Put the script at the end of <body> |
Searching for the body, document.querySelector('body') | It works, but it's the same element: document.body === document.querySelector('body') was true | Use document.body |
| Setting many colors from JavaScript | The look is split between CSS and scripts | Set one attribute, such as document.body.dataset.theme = 'dark', and keep the colors in CSS rules for body[data-theme="dark"] |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
Cannot read properties of null (reading 'style') | The script ran before the <body> tag | Move the script into the body, after the content |
| Some elements keep their own colors | Those elements have colors set in CSS | Change them in the CSS, or with a theme attribute on the body |
FAQ
Is there a document.head too?
Yes. document.head is the <head> element, and document.documentElement is the <html> element that holds both. On this page, their tagNames were HEAD and HTML, and document.body.tagName was BODY.
Can I add an attribute to the body?
Yes, like on any element. document.body.dataset.theme = 'dark' adds data-theme="dark" to the body, which CSS can select with body[data-theme="dark"].
Conclusion
document.body is the quickest way to the <body> element. Use it from a script inside the body, because before the browser reaches that tag, it's null.
Additional Resources
- Document: body property, on MDN.
- Document: head property, on MDN.
- HTMLElement: style property, on MDN.
