Is It Necessary to Write Head, Body and HTML Tags

Is it necessary to write HEAD, BODY and HTML tags?

Omitting the html, head, and body tags is certainly allowed by the HTML specifications. The underlying reason is that browsers have always sought to be consistent with existing web pages, and the very early versions of HTML didn't define those elements. When HTML first did, it was done in a way that the tags would be inferred when missing.

I often find it convenient to omit the tags when prototyping and especially when writing test cases as it helps keep the markup focused on the test in question. The inference process should create the elements in exactly the manner that you see in Firebug, and browsers are pretty consistent in doing that.

But...

Internet Explorer has at least one known bug in this area. Even Internet Explorer 9 exhibits this. Suppose the markup is this:

<!DOCTYPE html>
<title>Test case</title>
<form action='#'>
<input name="var1">
</form>

You should (and do in other browsers) get a DOM that looks like this:

HTML
HEAD
TITLE
BODY
FORM action="#"
INPUT name="var1"

But in Internet Explorer you get this:

HTML
HEAD
TITLE
FORM action="#"
BODY
INPUT name="var1"
BODY

See it for yourself.

This bug seems limited to the form start tag preceding any text content and any body start tag.

Is HTML tag such as head, HTML mandatory?

An HTML document has two* main parts:

head. The head element contains title and meta data of a web document.
body. The body element contains the information that you want to display on a web page.
* To make your web pages compatible with HTML 4, you need to add a document type declaration (DTD) before the HTML element. Many web authoring software add DTD and basic tags automatically when you create a new web page.

In a web page, the first tag (specifically, ) indicates the markup language that is being used for the document. The tag contains information about the web page. Lastly, the content appears in the tag. The following illustration provides a summary.
Sample Image

HTML, HEAD, BODY is requried - than all content you should write in BODY section

HERE you can read official documentation https://www.w3.org/TR/html401/struct/global.html

Should I use html, head and body tag?

The only reference I can find is talking about reducing HTML document file size. Other than that it doesn't seem like Google doesn't recommend them, it's just saying that it's valid to exclude them and you can reduce file size and speed up your site. However, I doubt that it will have any effect on you personally.

I think it comes down to personal preference. For example the supposed duplicate answer suggests that omitting the tags makes test cases more focused/clear.

I'm personally used to using these tags for production because it removes all ambiguity for what you intend. You may also want to add attributes to the <html> (such as lang), <head>, and <body> tags that would not otherwise be added automatically.

If you really crave for your HTML to be as small as possible when it's served while at the same time keeping your code clear for developers, I'm sure you can find a preprocessor that will remove unnecessary HTML tags and more for you.

Should head and body tags be on a different level of indentation to html?

HTML does not care about indentation, it only requires proper nesting. It is parsed the same (except for whitespace text nodes of course), it does really not matter for correctness.

While proper indentation does matter for readability, many people choose not to indent <html>, <head> and <body> tags as their structure is trivial, and only shifts the whole document rightwards unnecessarily. The contents of those tags should always be indented for clean markup, so that the nesting structure is clear to the reader.

To answer your question explicitly:

Should <head> and <body> tags be on a different level of indentation to <html>?

There is no need for that, as everybody knows they are nested in <html>. You can do it if you want. Both

<html>
<head>
<title>…</title>

<head>
<body>
<div>
<div>…</div>

</div>

</body>
<html>

and

<html>
<head>
<title>…</title>

<head>
<body>
<div>
<div>…</div>

</div>

</body>
<html>

are fine, while the following is not:

<html>
<head>
<title>…</title>

<head>
<body>
<div>
<div>…</div> <!-- which nesting level ??? -->

</div>

</body>
<html>

Can a HTML document be created without the html tag?

Yes, it can be created and it will work in some browsers like Chrome and Firefox without problems but it will break in others. Generally, it's not recommended to do that because, as I already said, it might break functionality in some browsers.

Also, according to the HTML5 specs the html, body and head tags are completely optional. However, it appears that a non-empty <title> tag is required in both HTML4 and HTML5.

EDIT: if you are looking for more competent answer you should check this answer:

Is it necessary to write HEAD, BODY and HTML tags?

Do we really need to use /body and /html closing tags?

Do we need it? Well that depends on your DTD. If you're using XHTML, then yes, you will need it to conform. For accessibility sake I would include the closing tags, you never know if there's a screen reader (or other piece of software) out there that only parses valid XHTML, you could be hindering partially sighted people for example.

Google will also, apparently, rank your valid documents higher than invalid documents in their listings.

Here's a document by a friend of a friend that answers your question a bit better; granted that it was written in 2008, I think some of the points still apply.

If you ever need to use the same html in an XHTML application you won't need to mess around with it, you can just copy it across and not have to worry about conforming (because you already are).

On a separate note, you are essentially future proofing your markup. Who's to say that the spec won't eventually change to "You must include the closing head and body tags"? You won't need to worry if you already have them. It is, however, highly unlikely that the spec will change to, "You must not include the closing head and body tags".


As a great man once said:

Should I close the lid of the toilet when I'm finished? Yes,
especially if the wife is going to use it after me.
- Darren Gourley (Nov 2015)

Is there any difference between adding template tags to the head or body of an HTML doc?

Templates are among the most flexible of all the elements in where they can be placed. The spec says

Contexts in which this element can be used:

- Where metadata content is expected.

- Where phrasing content is expected.

- Where script-supporting elements are expected.

- As a child of a colgroup element that doesn’t have a span attribute.

"Where metadata content is expected." essentially means in the head.

"Where phrasing content is expected." essentially means anywhere the valid child of a body element can go.

"Where script-supporting elements are expected" means it can go even in places that phrasing content can't, such as the child of ul, ol, table, tbody, tr etc elements.



Related Topics



Leave a reply



Submit