Skip to main content

How to Get the body Element with document.body in JavaScript

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
document.body
PartWhat it does
documentThe page.
.bodyIts <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:

index.html, a page with a paragraph This page gets a dark background from JavaScript, and a script that sets document.body.style.background to #292929 and document.body.style.color to #ffffff.

index.html
<!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 page in Chromium with a dark grey background and the white text This page gets a dark background from JavaScript.

The screenshot comes from Chromium 159, the open-source browser that Chrome is built on.

What Happened​

  • document.body was 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​

MistakeWhat happensDo 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 whitePut 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 trueUse document.body
Setting many colors from JavaScriptThe look is split between CSS and scriptsSet one attribute, such as document.body.dataset.theme = 'dark', and keep the colors in CSS rules for body[data-theme="dark"]

Troubleshooting​

SymptomLikely causeFix
Cannot read properties of null (reading 'style')The script ran before the <body> tagMove the script into the body, after the content
Some elements keep their own colorsThose elements have colors set in CSSChange 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​