<body>
The visible part of the document: everything the user sees on the page and can interact with.
- Display
- block
- Closing tag
- Required
- Permitted content
- Flow content
Example
#<body>
<header>
<strong>CodeWorlds</strong>
</header>
<main>
<h1>Your first lesson</h1>
<p>Everything you see in this preview lives inside the body element.</p>
</main>
<footer>
<small>© 2026 CodeWorlds</small>
</footer>
</body>Definition and usage
#The <body> element contains all the content visible in the browser window: headings, paragraphs, images, forms, and also scripts attached at the end of the page. A document has exactly one <body>, and it always comes right after <head>.
Browsers add a default 8-pixel margin around the content. That keeps the text from sticking to the window edge, but it is exactly why many stylesheets start with body { margin: 0; }. A background set on <body> fills the whole window, as long as the <html> element has no background of its own.
All elements inside inherit the font family, size, and text color set on <body>. It’s a good place for the base typography of the whole page.
In the past, a page’s look was set with attributes such as bgcolor or text. They are deprecated and CSS replaced them. What still works on <body> are window-level event attributes, for example onload or onbeforeunload.
Syntax
#<body>
…
</body>Attributes
#JavaScript code
Code that runs once the page has loaded completely, together with images, styles, and scripts. On<body>it means the same as theloadevent of thewindowobject, so in new code it is usually more convenient to writewindow.addEventListener("load", …)in a script.onbeforeunload
JavaScript code
Runs when the user is about to leave the page: closing the tab, reloading it, or navigating to another address. It lets you ask whether they really want to leave, for example when a form has unsaved changes. You can’t set the text of the question, because the browser shows its own message.onhashchange
JavaScript code
Reacts to a change in the part of the address after the#character, e.g. after clicking a link to a section on the same page. It’s useful in simple tabs and step-by-step guides.ononline / onoffline
JavaScript code
They report that the browser has regained or lost its network connection. In them you can show an “You are offline” message and hide it when the connection returns.bgcolor, text, link, vlink, alink, background
deprecated
Old appearance attributes: the color of the background, text, and links, and a background image. They are no longer part of the standard. You get the same effects in CSS with thebackground-color,color, andbackground-imageproperties and thea:link,a:visited, anda:activeselectors.
The element also supports all global attributes, such as class, id, style and hidden.
Attributes in practice
#onload
JavaScript codeCode that runs once the page has loaded completely, together with images, styles, and scripts. On <body> it means the same as the load event of the window object, so in new code it is usually more convenient to write window.addEventListener("load", …) in a script.
<body onload="document.getElementById('status').textContent = 'Page ready'">
<p id="status">Loading…</p>
</body>Default CSS
#Most browsers display this element with these default styles:
body {
display: block;
margin: 8px;
}Browser support
#Widely available
Works in all modern browsers, phones included. You can use it without worry.
- Chrome
- Edge
- Firefox
- Safari
Good practices
#- If a background or photo should reach the window edges, reset the margin:
body { margin: 0; }. - Set the base font family, size, and text color once on
<body>. The other elements will inherit them. - Instead of event attributes in HTML, use
addEventListenerin scripts. All the code is then in one place and easier to maintain.
Related entries
#- <html>The root of an HTML document. Every other element on the page lives inside it.
- <head>The invisible part of the document holding metadata: the title, character encoding, description, styles, and scripts.
- <main>The page’s main, unique content. Only one should be visible per page.
- <header>An introduction to a page or section: logo, title, navigation, author info.
- <footer>A page or section’s footer: author, copyright, links, contact info.
- <script>Embeds JavaScript code in the page or loads it from a file to give the page behavior.
Practise it in lessons
#Spotted a mistake or missing an example? Write to us.