What does the <body> element do?
Great question.
The <body> element is the main part of an HTML document, containing everything the user sees in the browser window.
If you compare HTML to a building:
<head>is the blueprint and technical data,- and
<body>is the interior and the contents of the rooms: what we actually use.
The purpose of <body>
It serves as a container for all the content of the page: text, images, links, tables, video, buttons, forms and other elements.
Example:
<body>
<h1>Welcome!</h1>
<p>This is my first site.</p>
<img src="photo.jpg" alt="Photo">
<a href="contact.html">Contact me</a>
</body>Everything between <body> and </body> is displayed on screen.
What <body> can contain
Everything the user sees and interacts with:
| Element | Purpose |
|---|---|
<h1>-<h6> | Headings of different levels |
<p> | Text paragraphs |
<img> | Images |
<a> | Links |
<ul>, <ol> | Lists |
<table> | Tables |
<form> | Forms and input fields |
<button> | Buttons |
<video>, <audio> | Media |
How the browser works with <body>
- When the browser loads HTML, it creates the DOM tree.
The
<body>node becomes the main parent for all visible elements. - CSS controls the appearance of the content inside
<body>. - JavaScript can change or add elements directly inside
<body>in real time.
Example:
document.body.style.background = "lightblue";Changes the background color of the whole page.
Attributes of the <body> tag
Although they are now rarely set directly (it's more common to use CSS), they still exist:
bgcolor- background color (deprecated);text- text color (deprecated);onload- an event fired when the page loads (still relevant in JavaScript).
Example:
<body onload="alert('Page loaded!')">Summary:
| Characteristic | Description |
|---|---|
| Purpose | The main content of the page |
| What it contains | Everything visible to the user |
| Controlled through | CSS and JavaScript |
| Place in the structure | Inside <html>, after <head> |
In simple terms:
<body> is the body of the web page.
Everything between <body> and </body> is what a person sees when they open the site.
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.