Multiple Head and Body tags in Webpage

Is it possible to use multiple HTML, Body tag in same webpage?

Last updated:7/20/2021 12:46:29 AM

1 Answers

Ethan Karla
Ethan Karla

An HTML document can have only one html tag and one body tag. If you put multiple HTML documents together, it will be an invalid document, and browsers may have problems displaying it.

You can remove duplicate tags, but it may not be that easy. There can only be one head tag in a document, so you need to concatenate the content of the head tag from different pages. If the pages have style sheets that conflict, this will be hard, then you will have to rewrite the style sheets and use it in the pages so that they don't conflict anymore. Same goes for JavaScript; If you have scripts with conflicting names, you'll need to rewrite them so they don't conflict.

Those pages may contain content that conflicts as well. An ID can only be defined once in a page, so if pages use the same identifiers, you'll have to change them, and use them in style sheets and scripts.

If you make sure that there are no such conflicts, you should be able to combine the pages.

If you have documents where you only have control over the body content, you can circumvent this by adding starting and ending tags for comments, so that the ending of one file and start of the next file are ignored. That way you can keep the start of the first file, the content from each file, and the ending of the last file:

<html>

  <body>
  content...
  <!--
  </body>
</html>
<html>
  <body>
  -->
  content...
  <!--
  </body>
</html>
<html>
  <body>
  -->
  content...
  </body>
</html>

Answer