Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

Is there any consideration that should be done between using the head or the body to add template tags? Ex:

conste template = document.createElement('template');
// add template content etc.

document.body.appendChild(template);
// or
document.head.appendChild(template);

I just stumble upon a code base that dynamically adds templates to head and my gut tells me that maybe it isn't the best idea, but maybe it doesn't matter?

like image 808
givanse Avatar asked Sep 20 '18 21:09

givanse


People also ask

What is the difference between head and body tag in HTML?

(i) The HEAD tag defines the HTML document header. (ii) It contains the information such as page title. (i) The BODY tag defines the body of the document. (ii) It contains the entire contents that will appear in the web browser window.

Is it necessary to write head body and HTML tags?

In HTML 5 it is not mandatory to include a <head> tag inside the HTML document but in previous versions(4.0. 1) it was mandatory to include it.

When should I use template tag?

The <template> tag is used as a container to hold some HTML content hidden from the user when the page loads. The content inside <template> can be rendered later with a JavaScript. You can use the <template> tag if you have some HTML code you want to use over and over again, but not until you ask for it.

Why do we use head tag in HTML?

The <head> tag in HTML is used to define the head portion of the document which contains information related to the document. The <head> tag contains other head elements such as <title>, <meta>, <link>, <style> <link> etc.


1 Answers

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.

like image 116
Alohci Avatar answered Sep 28 '22 19:09

Alohci