watashiSHUN
watashiSHUN

Reputation: 10484

why html needs <head> and <body>?

I just started learning HTML from codeacademy, the first lesson states that:

every html has a <head> and <body>

I don't deal with markup language very often, and I apologize if I am asking something obvious: why do we need to put these tags specifically?

  1. we know exactly what are the tags that can go into <head>, and assume <body> comes after <head> (can they be in any order?, ie <head> comes after <body>?), but despite the order, as long as <head> to <\head> is still a chunk, the parser can easily tell where <head> start and when it ends? The following is a snippet I took from w3school

    tags describe metadata: <title>, <style>, <meta>, <link>, <script>, and <base>

  2. this is also from w3school

    In the HTML5 standard, the <html> tag, the <body> tag, and the <head> tag can be omitted

    which is kind of implying that they are not needed at the first place?

Upvotes: 0

Views: 92

Answers (3)

unor
unor

Reputation: 96527

Note that w3schools.com is not from the W3C. If you want to know something about the HTML standards, you should check the official specifications.

For W3C’s HTML5, it’s http://www.w3.org/TR/2014/REC-html5-20141028/.

There it says for the head element:

Tag omission in text/html:

A head element's start tag may be omitted if the element is empty, or if the first thing inside the head element is an element.

A head element's end tag may be omitted if the head element is not immediately followed by a space character or a comment.

(The definition of the head element also answers your other question: it says "As the first element in an html element."; bold emphasis mine.)

And for the body element it says:

Tag omission in text/html:

A body element's start tag may be omitted if the element is empty, or if the first thing inside the body element is not a space character or a comment, except if the first thing inside the body element is a meta, link, script, style, or template element.

A body element's end tag may be omitted if the body element is not immediately followed by a comment.

All optional tags are listed in section 8.1.2.4 Optional tags.

Upvotes: 1

William
William

Reputation: 1

Head is generally for meta-data and things like css, while the body is for content. https://en.wikibooks.org/wiki/HyperText_Markup_Language/Head_and_Body

Upvotes: -1

CristianD
CristianD

Reputation: 138

They aren't always needed as usually browsers make assumptions on how to render the page. However, if you don't add specific enough meta information and appropriate tags, the browser might render it differently from another browser.

Basically the results become rather erroneous as opposed to what you really want from browser to browser.

Upvotes: 1

Related Questions