Skip to content

<body> - Document Body Element

Content Container HTML 2.0

The <body> element contains all the visible content of an HTML document, including text, images, links, tables, lists, and all interactive elements. Everything users see and interact with on a webpage is contained within the <body> element.

Result
<body>
<!-- All visible page content goes here -->
</body>

The <body> element has several event handler attributes, though most are legacy and better handled with JavaScript:

AttributeDescriptionModern Alternative
onloadScript to run when page loadsUse addEventListener('load')
onunloadScript to run when page unloadsUse addEventListener('beforeunload')
onbeforeunloadScript before page unloadsUse addEventListener('beforeunload')
onresizeScript when window is resizedUse addEventListener('resize') on window
onscrollScript when page is scrolledUse addEventListener('scroll')

This element supports all global attributes.

The <body> typically contains semantic structure elements:

Result

A typical webpage layout with header, navigation, main content, sidebar, and footer:

Result

Using the body’s load event (modern approach with JavaScript):

Result

The <body> element is commonly styled to set page-wide defaults:

Result

Structure your body content with semantic elements rather than generic divs:

<!-- Good: Semantic structure -->
<body>
<header>
<nav><!-- navigation --></nav>
</header>
<main>
<article><!-- content --></article>
<aside><!-- sidebar --></aside>
</main>
<footer><!-- footer --></footer>
</body>
<!-- Avoid: Non-semantic divs -->
<body>
<div class="header">
<div class="nav"><!-- navigation --></div>
</div>
<div class="content"><!-- content --></div>
<div class="footer"><!-- footer --></div>
</body>

Browsers apply default margins to the body. Reset them for better control:

body {
margin: 0;
padding: 0;
}
<!-- Avoid: Inline event handlers -->
<body onload="init()">
<!-- Better: JavaScript addEventListener -->
<body>
<script>
document.addEventListener('DOMContentLoaded', init);
</script>
</body>

An HTML document must have exactly one <body> element:

<!-- Wrong: Multiple body elements -->
<html>
<body>First body</body>
<body>Second body</body>
</html>
<!-- Correct: Single body -->
<html>
<head>...</head>
<body>All content here</body>
</html>

A well-structured body typically follows this pattern:

<body>
<!-- 1. Skip link for accessibility -->
<a href="#main" class="skip-link">Skip to main content</a>
<!-- 2. Site header -->
<header>
<nav><!-- Main navigation --></nav>
</header>
<!-- 3. Main content area -->
<main id="main">
<!-- Page-specific content -->
</main>
<!-- 4. Site footer -->
<footer>
<!-- Footer content -->
</footer>
<!-- 5. Scripts at end for performance -->
<script src="app.js"></script>
</body>

Use proper landmark elements within the body to create an accessible page structure:

<body>
<!-- banner landmark (implicit from <header>) -->
<header>
<h1>Site Title</h1>
<!-- navigation landmark -->
<nav>
<a href="/">Home</a>
</nav>
</header>
<!-- main landmark -->
<main>
<h2>Page Content</h2>
</main>
<!-- contentinfo landmark (implicit from <footer>) -->
<footer>
<p>Copyright info</p>
</footer>
</body>

Provide skip links at the start of the body for keyboard users:

Result

The <body> element has no implicit ARIA role. Use semantic elements within the body instead:

<body>
<!-- Use semantic elements, not ARIA on body -->
<header role="banner"><!-- if needed for older browsers --></header>
<main role="main"><!-- if needed for older browsers --></main>
<footer role="contentinfo"><!-- if needed for older browsers --></footer>
</body>
BrowserVersion
ChromeAll versions
FirefoxAll versions
SafariAll versions
EdgeAll versions
IEAll versions
<!-- Wrong: Content outside body -->
<html>
<head><title>Page</title></head>
<h1>This won't display correctly</h1>
<body>
<p>Body content</p>
</body>
</html>
<!-- Correct: All content inside body -->
<html>
<head><title>Page</title></head>
<body>
<h1>This displays correctly</h1>
<p>Body content</p>
</body>
</html>
<!-- Avoid: Deprecated attributes -->
<body bgcolor="#ffffff" background="bg.jpg" text="#000000">
<!-- Use CSS instead -->
<body style="background-color: #ffffff; background-image: url('bg.jpg'); color: #000000;">