The root element containing head and body. Learn more →
<body> ElementThe <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.
<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:
| Attribute | Description | Modern Alternative |
|---|---|---|
onload | Script to run when page loads | Use addEventListener('load') |
onunload | Script to run when page unloads | Use addEventListener('beforeunload') |
onbeforeunload | Script before page unloads | Use addEventListener('beforeunload') |
onresize | Script when window is resized | Use addEventListener('resize') on window |
onscroll | Script when page is scrolled | Use addEventListener('scroll') |
This element supports all global attributes.
The <body> typically contains semantic structure elements:
A typical webpage layout with header, navigation, main content, sidebar, and footer:
Using the body’s load event (modern approach with JavaScript):
The <body> element is commonly styled to set page-wide defaults:
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:
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>| Browser | Version |
|---|---|
| Chrome | All versions |
| Firefox | All versions |
| Safari | All versions |
| Edge | All versions |
| IE | All versions |
The root element containing head and body. Learn more →
Contains document metadata. Learn more →
Represents the main content of the body. Learn more →
Introductory content or navigation. Learn more →
<!-- 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;">