Dofactory.com
Dofactory.com

HTML <body> Tag

The <body> tag is a container for all content on a web page.

A page can only have one <body> element.

Example

#

A <body> tag as part of an HTML page.

<!DOCTYPE html>
<html>
 
 <head>
  <title>Page Title</title>
 </head>

 <body>
  <h1>This is a page heading</h1>
  <p>This is a paragraph.</p>
 </body>

</html>

More Examples

A styled <body> element that creates a page in dark mode.
Click the 'Try it live' button to see it in action.

<!DOCTYPE html>
<html>
 
 <head>
  <title>Title of your page</title>
 </head>

 <body style="background:black;color:white;">
  <h1>A heading on your page</h1>
  <p>A paragraph on your page.</p>
 </body>

</html>


Attributes for <body>

The <body> element has no attributes, but it does accept global attributes. The following are commonly used.

Attribute Value Description
id   identifier Defines a unique identifier for the body.
class   classnames Sets one or more classnames to the body element.
style   CSS-styles Defines CSS style values for the body element.
data-*   value Defines additional data that can be used by JavaScript.

For other global attributes see our global attributes list.


Obsolete Attributes

Do not use the attributes listed below.  They are no longer valid on the body tag in HTML5.

Attribute Description Alternative
link Specifies color for unvisited links. CSS color
alink Specifies color for active links. CSS color
vlink Specifies color for visited links. CSS color
topmargin Specifies the margin to the top of the page. CSS margin-top
rightmargin Specifies the margin to the right of the body. CSS margin-right
bottommargin Specifies the margin to the bottom of the body. CSS margin-bottom
leftmargin Specifies the margin to the left of the body. CSS margin-left
background URL of image to use as a background for the page. CSS background-image
bgcolor Specifies the background color of the page. CSS background-color
text Specifies text color of the page. CSS color

Did you know?

Did you know?

The <body> tag is not required

Remarkably, in HTML5, the <body> tag is considered optional.

To test this, remove the body tags and confirm it has no impact.

However, the convention is to wrap all content inside a <body> tag.

To create a robust page structure, always include a <body> tag.


Page Tags

The <body> tag is part of a group of tags that define the structure of a web page. This group is referred to as the Page tag group. Together, they allow you to create solid, well-structured web pages.

Here is the complete list.

Element Description
<!DOCTYPE> Must appear on the first line of a page. Specifies the HTML version
<html> Defines the root container for an HTML document
<head> Creates a head container that holds page-level metadata elements
<meta> Provides metadata about a web page
<link> Defines a link to an external source, such as a style sheet
<base> Sets the base URL for all relative URLs on a page
<script> Adds JavaScript to a page. Either client- or server-side
<style> Adds CSS style elements to a page
<title> Specifies the page title that displays in the browser's tab
<body> Specifies a container for the content of the page, with text, links, images, etc.

Browser support

Here is when <body> support started for each browser:

Chrome
1.0 Sep 2008
Firefox
1.0 Sep 2002
IE/Edge
1.0 Aug 1995
Opera
1.0 Jan 2006
Safari
1.0 Jan 2003

You may also like


Last updated on Sep 30, 2023




Guides