HTML5 Reference

HTML <template> tag

Definition

The <template> tag is used for declaring fragments of HTML that can be cloned and inserted into the HTML document by a script.

The content of the <template> element is hidden on page load and actioned via scripting.

The <template> tag was introduced in HTML5.

Example


<template id="pieTemplate">
<img src="../../images/chickenpiesmall.jpg"
     alt="Chicken Pie" width="200" height="200">
</template>
<button onclick="showFood()">Let there be pie!</button>
<script>
  function showFood() {
  var pieTemplate = document.getElementById('pieTemplate'),
      showExample = document.getElementById('showExample'),
      clonedTemplate = pieTemplate.content.cloneNode(true);
     showExample.appendChild(clonedTemplate);
  }
</script>

Attributes

The following attributes can be used with the <template> tag.


<template> Specific Attributes
Attribute Value Description Example
There are no required or optional attributes specific to the <template> tag.
<template> Global Attributes
Attribute Description Example
Common
classSpecifies a classname for the element allowing you to apply the style of the predefined class to the content.

<template class="name">Template content</template>
idSpecifies a unique id for the element allowing you to apply the style of the predefined id to the content.

<template id="name">Template content</template>
styleSpecifies an inline style for the element allowing you to apply the style to the contentt.

<template style="color:red;">Template content</template>
titleSpecifies an inline style for the element allowing you to apply the style to the content.

<template title="Content info">Template content</template>
Keyboard
accesskeySpecifies a keyboard shortcut to associate with the element.

<template accesskey="a">Template content</template>
tabindexSpecifies a tab order for the element.

<template tabindex="1">Template content</template>
Language
dirSpecifies the directional flow of the content.

<!-- The text will flow from left to right -->
<template dir="ltr">Template content</template>
<!-- The text will flow from right to left -->
<template dir="rtl">Template content</template>
langSpecifies a language code for the content of the element.

<template lang="en">Template content</template>
spellcheckSpecifies an inline style for the element allowing you to apply the style to the contentt.

<!-- Valid values true and false. -->
<!-- Default inherited / browser specific. -->
<template spellcheck="true">Template content</template> 
translateSpecifies an inline style for the element allowing you to apply the style to the content.

<!-- Valid values yes and no. -->
<!-- Default yes. -->
<template translate="no">Template content</template> 
Miscellaneous
contenteditableSpecifies whether the content of the element is editable.

<!-- Valid values true and false. -->
<!-- Default inherited. -->
<template contenteditable="true">Template content</template> 
draggableSpecifies whether the element is draggable.

<!-- Valid values true and false. -->
<!-- Default browser specific. -->
<template draggable="true">Template content</template>
hiddenSpecifies whether the element is not yet, or no longer, relevant.

<!-- Valid values an empty string or hidden -->
<template hidden>Template content</template> 
<template hidden="hidden">Template content</template> 
<code> template Attributes
Attribute Description Example
Document Element
oncopyThe script to be run when the user copies the content of an element.

 <!-- Executes go() function -->
<template oncopy="go()">Template content</template> 
oncutThe script to be run when the user cuts the content of an element.

 <!-- Executes go() function -->
<template oncut="go()">Template content</template>
onpasteThe script to be run when the user pastes some content into an element.

 <!-- Executes go() function -->
<template onpaste="go()">Template content</template>
Drag and Drop
ondragThe script to be run when an element is dragged.

 <!-- Executes go() function -->
<template ondrag="go()">Template content</template>
ondragendThe script to be run when an element has stopped being dragged.

 <!-- Executes go() function -->
<template ondragend="go()">Template content</template>
ondragenterThe script to be run when an element has been dragged to a valid drop target.

 <!-- Executes go() function -->
<template ondragenter="go()">Template content</template>
ondragleaveThe script to be run when an element leaves a valid drop target.

 <!-- Executes go() function -->
<template ondragleave="go()">Template content</template>
ondragoverThe script to be run when an element is being dragged over a valid drop target.

 <!-- Executes go() function -->
<template ondragover="go()">Template content</template>
ondragstartThe script to be run at the start of a drag operation.

 <!-- Executes go() function -->
<template ondragstart="go()">Template content</template>
ondropThe script to be run when a dragged element is being dropped.

 <!-- Executes go() function -->
<template ondrop="go()">Template content</template>
Form
onblurThe script to be run when the element loses focus.

 <!-- Executes go() function -->
<template onblur="go()">Template content</template>
onchangeThe script to be run when object changed and attempt to leave field.

 <!-- Executes go() function -->
<template onchange="go()">Template content</template>
oncontextmenuThe script to be run when a context menu is triggered.

 <!-- Executes go() function -->
<template oncontextmenu="go()">Template content</template>
onfocusThe script to be run when the element gets focus.

 <!-- Executes go() function -->
<template onfocus="go()">Template content</template>
oninputThe script to be run when an element gets user input.

 <!-- Executes go() function -->
<template oninput="go()">Template content</template>
oninvalidThe script to be run when an element is invalid.

 <!-- Executes go() function -->
<template oninvalid="go()">Template content</template>
onresetThe script to be run when a dragged element is being dropped.

 <!-- Executes go() function -->
<template onreset="go()">Template content</template>
onselectThe script to be run when some or all of the contents of an object are selected.

 <!-- Executes go() function -->
<template onselect="go()">Template content</template>
onsubmitThe script to be run when a form is submitted.

 <!-- Executes go() function -->
<template onsubmit="go()">Template content</template>
Keyboard
onkeydownThe script to be run when an element is in focus and keyboard key is pressed down.

 <!-- Executes go() function -->
<template onkeydown="go()">Template content</template>
onkeypressThe script to be run when an element is in focus and keyboard key is pressed down and released.

 <!-- Executes go() function -->
<template onkeypress="go()">Template content</template>
onkeyupThe script to be run when an element is in focus and keyboard key is released.

 <!-- Executes go() function -->
<template onkeyup="go()">Template content</template>
Media
onabortThe script code to be run on abort.

 <!-- Executes go() function -->
<template onabort="go()">Template content</template>
oncanplayThe script to be run when a file has buffered enough so it is ready to start playing.

 <!-- Executes go() function -->
<template oncanplay="go()">Template content</template>
oncanplaythroughThe script to be run when a file can be played all the way to the end without further need of buffering.

 <!-- Executes go() function -->
<template oncanplaythrough="go()">Template content</template>
oncuechangeThe script to be run when the cue changes when using the track element.

 <!-- Executes go() function -->
<template oncuechange="go()">Template content</template>
ondurationchangeThe script to be run when the length of the media is changed.

 <!-- Executes go() function -->
<template ondurationchange="go()">Template content</template>
onemptiedThe script to be run when a media resource element suddenly becomes empty, usually due to an error.

 <!-- Executes go() function -->
<template onemptied="go()">Template content</template>
onendedThe script to be run when the media has reach the end.

 <!-- Executes go() function -->
<template onended="go()">Template content</template>
onloadeddataThe script to be run when media data is loaded and playback can start.

 <!-- Executes go() function -->
<template onloadeddata="go()">Template content</template>
onloadedmetadataThe script to be run when metadata has been loaded.

 <!-- Executes go() function -->
<template onloadedmetadata="go()">Template content</template>
onloadstartThe script to be run whenthe media resource has started loading.

 <!-- Executes go() function -->
<template onloadstart="go()">Template content</template>
onpauseThe script to be run when the media resource has been paused.

 <!-- Executes go() function -->
<template onpause="go()">Template content</template>
onplayThe script to be run when the media resource starts playback.

 <!-- Executes go() function -->
<template onplay="go()">Template content</template>
onplayingThe script to be run when when playback has already begun.

 <!-- Executes go() function -->
<template onplaying="go()">Template content</template>
onprogressThe script to be run when the browser is fetching the media data.

 <!-- Executes go() function -->
<template onprogress="go()">Template content</template>
onratechangeThe script to be run when the playback rate changes.

 <!-- Executes go() function -->
<template onratechange="go()">Template content</template>
onseekedThe script to be run when the seeking attribute is set to false indicating that seeking has finished.

 <!-- Executes go() function -->
<template onseeked="go()">Template content</template>
onseekingThe script to be run when the seeking attribute is set to true indicating that seeking is currently active.

 <!-- Executes go() function -->
<template onseeking="go()">Template content</template>
onstalledThe script to be run when the browser is unable to continue fetching media data.

 <!-- Executes go() function -->
<template onstalled="go()">Template content</template>
onsuspendThe script to be run when media data has stopped before being completely loaded.

 <!-- Executes go() function -->
<template onsuspend="go()">Template content</template>
ontimeupdateThe script to be run when the media resources current playback position has changed.

 <!-- Executes go() function -->
<template ontimeupdate="go()">Template content</template>
onvolumechangeThe script to be run when the volume has changed or been muted.

 <!-- Executes go() function -->
<template onvolumechange="go()">Template content</template>
onwaitingThe script to be run when the media resource has paused but is expected to resume.

 <!-- Executes go() function -->
<template onwaiting="go()">Template content</template>
Mouse
onclickThe script to be run when when a mouse is clicked on an element.

 <!-- Executes go() function -->
<template onclick="go()">Template content</template>
ondblclickThe script to be run when a mouse is double clicked on an element.

 <!-- Executes go() function -->
<template ondblclick="go()">Template content</template>
onmousedownThe script to be run when he mouse button is pressed down while the cursor is over an element.

 <!-- Executes go() function -->
<template onmousedown="go()">Template content</template>
onmousemoveThe script to be run when the mouse button is moved.

 <!-- Executes go() function -->
<template onmousemove="go()">Template content</template>
onmouseoutThe script to be run when the mouse cursor moves off an element.

 <!-- Executes go() function -->
<template onmouseout="go()">Template content</template>>
onmouseoverThe script to be run when the mouse cursor moves over an element.

 <!-- Executes go() function -->
<template onmouseover="go()">Template content</template>
onmouseupThe script to be run when the mouse button is released while the cursor is over the element.

 <!-- Executes go() function -->
<template onmouseup="go()">Template content</template>
onwheelThe script to be run when the mouse wheel rolls up or down over an element.

 <!-- Executes go() function -->
<template onwheel="go()">Template content</template>
Scroll
onscrollThe script code to be run when the scrollbar of an element is being scrolled.

 <!-- Executes go() function -->
<template onscroll="go()">Template content</template> 
Window - NONE

Relevant HTML Tutorials

HTML Advanced - Scripting