Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have always wondered but can't really find an answer anywhere if there are any set standards regarding indenting the body or head tag.

Is this version correct?

<html>
<head>
</head>
<body>
</body>
</html>

Or this one?

<html>
    <head>
    </head>
    <body>
    </body>
</html>

Whilst I appreciate that it probably doesn't make the slightest bit of difference as far as the functionality of the final website goes, we are all human beings, and all blessed with the gift / burden that is curiosity.

Are there any set standards or does it not matter?

like image 839
Toby Cannon Avatar asked Apr 27 '15 15:04

Toby Cannon


People also ask

Should head and body be indented HTML?

As someone above said, indentation makes for clear readable code but not just, <head> and <body> are both children of <html> and thus should be indented to show this fact.

Does indentation matter for HTML?

No. HTML code does not need to be indented, and all browsers and search engines ignore indentation and extra spacing. However, for any human reader, it's a good idea to indent your text because it makes the code easier to scan and read.

What is proper indentation in HTML?

Don't use tabs to indent text; use spaces only. Different text editors interpret tabs differently, and some Markdown features expect spaces and not tabs. Indent by two spaces per indentation level. Use all-lowercase for elements and attributes.

Does body need to be indented?

Formatting the Main Body There should be no extra spaces between the title and the beginning of your paper. The beginning of every paragraph should be indented by half an inch (just hit the "tab" key on your keyboard). There should be no extra spaces between paragraphs.


2 Answers

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>
like image 129
Bergi Avatar answered Sep 30 '22 23:09

Bergi


It does not matter in functionality, but for the purpose of clean, readable and managable code you should always indent childs.

like image 21
Eun Avatar answered Sep 30 '22 23:09

Eun