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?
(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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With