HTML <body> Tag: Usage, Attributes and Real-World Examples

By Cristian G. Guasch •  Updated: 09/25/23 •  10 min read

When it comes to web design and development, understanding HTML tags is crucial. One such important tag is the HTML <body> tag. It’s the container for all your website’s content, including text, images, hyperlinks, tables— you name it. If your webpage was a house, then the <body> tag would be its foundation.

Diving into its usage and attributes will reveal its true importance in HTML structure. The <body> tag holds all the elements that render on your web browser when you visit a site. It’s where the magic happens! From headers to footers, navigation bars to forms — everything that you see displayed on a webpage lives within this tag.

Let me walk you through some practical examples of how to use this central pillar of HTML coding effectively. After reading this article, I’m confident you’ll have a solid grasp of what makes the <body> tag tick and how best to employ it in your coding journey.

Understanding the HTML <body> Tag

Diving right into the heart of any webpage, we find ourselves in the domain of the HTML <body> tag. This is really where all the magic happens. When I talk about the ‘heart’ or ‘magic’, it’s not an overstatement. The <body> tag is where you put all your website’s content that’ll be visible to users.

Let me clarify a bit more. Imagine you’re building a house; your <html> tag lays down the foundation and structure, while your <head> tag takes care of unseen elements like metadata (think electrical wiring hidden behind walls). Now, coming back to our beloved <body> tag – this is essentially painting those walls, adding furniture and decor – everything that makes a house feel like home!

Just like how every element within a house has its place, all tags coded within an HTML document must nestle comfortably within the <body> tags. From paragraphs (<p>) and headings (<h1>-<h6>) to images (<img>) and links (<a>), they’ve all got their space here.

Here’s what an example might look like:

<!DOCTYPE html>
<html>
<head>
<title>This is my cool website!</title>
</head>
<body>

<h1>Welcome to my page!</h1>

<p>I'm excited for you to learn more about me.</p>

</body>
</html>

You’ll notice there aren’t many attributes tied directly to this workhorse of a tag- it’s because most styling these days gets handled by CSS instead. That doesn’t mean there are no attributes at all! A couple still exist: onload and onunload, which handle events when loading or unloading a page respectively.

One common mistake beginners make? Forgetting to close their <body> tag properly. It’s like leaving the front door of your house wide open! So always remember- for every opening <body> tag, there must be a corresponding closing </body> tag.

Attributes of the HTML <body> Tag

Diving right into it, let’s talk about the different attributes we can use with an HTML <body> tag. It’s important to note that in HTML5, many attributes have been deprecated and are no longer advised for use. However, they’re still functional in older versions of HTML.

The most common attribute you’ll see is ‘bgcolor’. This sets the background color of the webpage. For instance:

<body bgcolor="lightblue">

This will give your website a light blue background color. But remember, since it’s not supported in HTML5 anymore, it’s better to use CSS for styling purposes.

Next up we’ve got ‘background’. Now this one allows you to set an image as the background of your webpage. A line of code might look like this:

<body background="imageurl.jpg">

Just replace ‘imageurl.jpg’ with your desired image link and voila! You’ve got yourself a snazzy new backdrop.

Then there’s ‘text’, which changes the default text color on your webpage. Similarly though, it’s outdated in terms of HTML5 so resorting to CSS would be more appropriate here too.

We also have ‘link’, ‘vlink’, and ‘alink’. These three allow us to modify unvisited links (‘link’), visited links (‘vlink’), and active links (‘alink’) respectively. An example could be:

<body link="red" vlink="green" alink="blue">

In this case, all unclicked links would appear red; ones already clicked would turn green; while any currently selected link becomes blue.

Lastly, we’ve got ‘marginwidth’ and ‘marginheight’, which control space around body content by defining margins horizontally or vertically respectively.
However these aren’t recommended either since they’re unsupported in current versions of HTML5.

In conclusion, while these attributes may still work in older versions of HTML, it’s more advisable to use CSS for styling your webpage nowadays. This way, you’ll keep up with modern standards and have a wider range of customization options at your disposal.

Practical Examples Using the HTML <body> Tag

With my experience in web design, I’ve come to appreciate the versatility and importance of the HTML <body> tag. It’s like a blank canvas, waiting to be filled with vibrant colors and intricate designs. Let’s jump right into some practical examples.

One way I often use the <body> tag is for setting a website’s background color or image. Here’s how you do it:

<body style="background-color:lightgrey">
  <h1>This is a Heading</h1>
  <p>This is a paragraph.</p>
</body>

In this example, everything contained within <body> will have a light grey background color. Pretty neat, right?

However, it’s crucial to remember that every opening tag needs its closing pair. One common mistake beginners make is forgetting to close their tags properly:

<body style="background-color:lightgrey">
  <h1>This is a Heading
  <p>This is a paragraph.

Without those closing </h1> and </p> tags, your browser won’t know where each element ends – causing potential chaos on your webpage.

But let’s move on from mistakes and get back to exploring more possibilities with our trusty <body>. Another exciting attribute we can add within this tag is onload. This instructs the browser to execute specific JavaScript once the page has fully loaded:

<body onload="myFunction()">
...
</body>

By harnessing these attributes effectively inside our body tags, we can create dynamic and responsive webpages that keep users engaged and interested.

And finally – for all you accessibility champions out there (and let’s be honest – we should ALL be striving for accessibility), here’s an example of using ARIA roles within your body tag:

<body role=”document”>
...
</body>

This tells assistive technologies, like screen readers, that the <body> contains content meant to be read as a document. It’s just one way we can make the web a more inclusive place for everyone.

So there you have it – the HTML <body> tag is no longer a mystery! Whether you’re setting background colors, triggering JavaScript functions on load, or improving webpage accessibility with ARIA roles – it all happens here in this hardworking tag.

Common Mistakes and How to Avoid Them

Embarking on the journey of web development, you’re bound to stumble upon a few roadblocks. But don’t fret! I’m here to help navigate some common mistakes with HTML’s <body> tag and how you can avoid them.

Firstly, let’s tackle one mistake that even seasoned developers sometimes make: forgetting the body tag altogether. Now, I know what you’re thinking – “How could someone forget that?” Well, it’s easier than you think, especially when you’re knee-deep in complex coding tasks. So here’s my advice – Always start your HTML document by including the <body> tag right after your <head> section ends. It should look something like this:

<!DOCTYPE html>
<html>
<head>
   <!-- Your head content goes here -->
</head>

<body>
  <!-- Your main content goes here -->
</body>

</html>

Now onto our second common misstep: Inserting elements outside the body tag. The browser might still render your page correctly but this is not good practice and may lead to unpredictable results in different browsers or versions. Ensure all visible contents are wrapped within the <body> tags.

The last pitfall we’ll address today involves misuse of attributes within the <body> tag. While there aren’t many attributes associated with this specific element (only class, id, style, etc.), it’s critical to use them correctly for optimal website functionality. For instance, applying inline styles using the style attribute might seem convenient initially but as your project expands it could lead into a messy codebase with inconsistent styling.

In conclusion, while dealing with HTML’s <body> tag isn’t necessarily rocket science, vigilance against these common errors will ensure a smoother ride on your web development journey.

Conclusion: Mastering the Use of HTML <body> Tag

I’ve shared a fair bit about the HTML <body> tag, from its usage, attributes to some key examples. It’s essential to grasp how this tag works as it forms the skeleton of your webpage.

Let’s not forget that everything visible on your website lives within this <body> tag. So understanding and mastering its use can significantly enhance your web development skills.

A typical example of using the <body> tag is:

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

<h1>This is a Heading</h1>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>

</body>
</html>

In this snippet, you’ll notice that all content intended for display – heading and paragraphs, are wrapped inside the <body> tags.

As with any coding endeavor, there are pitfalls to avoid when using the <body> tag:

Remember these points while designing your webpage and you’re on the path of creating efficient codes!

HTML offers several attributes for customization like bgcolor, background etc., but they’re outdated now due to advances in CSS. I’d recommend sticking with CSS for styling purposes.

Ultimately, practice makes perfect! The more you experiment with different elements inside the <body> tag and observe their behavior, better will be your understanding and command over them. Happy coding!

Cristian G. Guasch

Hey! I'm Cristian Gonzalez, I created HTML Easy to help you learn HTML easily and fast.

Related articles