{"id":37513,"date":"2019-07-25T23:34:28","date_gmt":"2019-07-25T15:34:28","guid":{"rendered":"https:\/\/www.hongkiat.com\/blog\/?p=37513"},"modified":"2023-04-06T21:57:38","modified_gmt":"2023-04-06T13:57:38","slug":"html-template-slow-tag-shadow-dom","status":"publish","type":"post","link":"https:\/\/www.hongkiat.com\/blog\/html-template-slow-tag-shadow-dom\/","title":{"rendered":"How to Use HTML &lt;template&gt; &amp; &lt;slot&gt; With Shadow DOM"},"content":{"rendered":"<p><strong><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/HTML\/Element\/slot\" target=\"_blank\" rel=\"noopener noreferrer\">HTML Slot<\/a><\/strong> is one of the most remarkable standards made by W3C. Combine that with another impressive W3C standard called <strong><a href=\"https:\/\/developer.mozilla.org\/en\/docs\/Web\/HTML\/Element\/template\" target=\"_blank\" rel=\"noopener noreferrer\">templates<\/a><\/strong>, and you have a fabulous concoction to work with. Being able to <strong>create and add HTML elements<\/strong> to a page <strong>using JavaScript<\/strong> is a necessary and important task.<\/p>\n<p>It\u2019s useful when a code snippet has to <strong>appear only at certain times<\/strong>, or when you don\u2019t want to type out hundreds of similarly structured HTML elements but want to <strong>automize the process<\/strong>.<\/p>\n<p class=\"recommended_top\">\n\t\t\t\t\t<strong>Read Also:<\/strong>\u00a0\n\t\t\t\t\t<a target=\"_blank\" href=\"https:\/\/www.hongkiat.com\/blog\/dom-manipulation-javascript-methods\/\">Master DOM Manipulation with 15 Essential JavaScript Methods<\/a>\n\t\t\t\t<\/p>\n<p>Creating HTML elements in JavaScript is <strong>not so desirable<\/strong>. It\u2019s a hassle having to check and recheck if  you have covered all the tags, placed them in the right order, all in all, there is just <em>too<\/em> much to type and keep track of. This turmoil, however, <strong>got a solution<\/strong> when the <a href=\"https:\/\/developer.mozilla.org\/en\/docs\/Web\/HTML\/Element\/template\" target=\"_blank\" rel=\"noopener noreferrer\"><code>&lt;template&gt;<\/code><\/a> tag appeared. If something needs to be <strong>added to the page dynamically<\/strong>, you can put it inside of the <code>&lt;template&gt;<\/code> element.<\/p>\n<p>In this post, I will show you how you can use the <code>&lt;slot&gt;<\/code> and <code>&lt;template&gt;<\/code> tags together with JavaScript to <strong>create a mini HTML table factory<\/strong> that can create and populate hundreds of similar tables.<\/p>\n<h2>The <code>&lt;slot&gt;<\/code> and <code>&lt;template&gt;<\/code> tags<\/h2>\n<p>The <a href=\"https:\/\/developer.mozilla.org\/en\/docs\/Web\/HTML\/Element\/template\" target=\"_blank\" rel=\"noopener noreferrer\"><code>&lt;template&gt;<\/code><\/a> tag holds HTML code that <strong>won\u2019t be rendered by browsers<\/strong> until it\u2019s properly added to the document, using JavaScript. The <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/HTML\/Element\/slot\" target=\"_blank\" rel=\"noopener noreferrer\"><code>&lt;slot&gt;<\/code><\/a> tag is a <strong>placeholder you add to a Shadow DOM<\/strong> which can be made of the content of the <code>&lt;template&gt;<\/code> element.<\/p>\n<p>A <strong><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/Web_Components\/Shadow_DOM\" target=\"_blank\" rel=\"noopener noreferrer\">Shadow DOM<\/a><\/strong> is similar to a regular DOM (the document model parsed from HTML). It <strong>creates a scoped tree<\/strong> (a Shadow DOM tree), that has a <strong>root of its own<\/strong> and can also have a <strong>style of its own<\/strong>.<\/p>\n<p>When you insert the Shadow DOM tree into an element in the main document \u2014 the element will then be called the <strong>shadow host<\/strong> \u2014, all the child elements of the shadow host that are marked with the <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/HTML\/Global_attributes\/slot\"><code>slot<\/code> attribute<\/a> (not the same as the aforementioned <code>&lt;slot&gt;<\/code> tag) will <strong>take their place in the newly inserted subtree<\/strong>.<\/p>\n<figure><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/assets.hongkiat.com\/uploads\/html-template-slow-tag-shadow-dom\/slot-diagram.jpg\" alt=\"Addition of the Shadow DOM\" width=\"857\" height=\"772\"><figcaption><a href=\"http:\/\/w3c.github.io\/webcomponents\/publish\/shadow\/WD-shadow-dom-20151008\/#fig-a-distribution\" target=\"_blank\" rel=\"noopener noreferrer\">html-template-slow-tag-shadow-dom: W3C<\/a><\/figcaption><\/figure>\n<p>The Shadow DOM, as of writing this article (July 2017), are <strong>supported only in WebKit- and Blink-based browsers<\/strong> but you can check the actual state of browser support <a href=\"https:\/\/caniuse.com\/#search=shadow%20dom\" target=\"_blank\" rel=\"noopener noreferrer\">on CanIUse<\/a> at any time.<\/p>\n<h2>Setting up the HTML <code>&lt;template&gt;<\/code><\/h2>\n<p>Still confusing? Let\u2019s see some code, starting with the <code>&lt;template&gt;<\/code> element.<\/p>\n<p>Inside <code>&lt;template&gt;<\/code>, there\u2019s a <code>&lt;table&gt;<\/code> we\u2019ll <strong>use as a blueprint<\/strong> for <strong><a href=\"https:\/\/www.hongkiat.com\/blog\/create-auto-generated-table-of-contents-html-slot\/\">creating some tables<\/a><\/strong> that\u2019ll be added to a document. There are <code>&lt;slot&gt;<\/code> elements inside the table cells (<code>&lt;th&gt;<\/code> and <code>&lt;td&gt;<\/code>) <strong>acting as placeholders<\/strong> for the column titles and cell values. Each slot has a unique <code>name<\/code> attribute that <strong>identifies it<\/strong>.<\/p>\n<pre>\r\n&lt;template&gt;\r\n  &lt;table&gt;\r\n    &lt;tr&gt;\r\n      &lt;th&gt;&lt;slot name='title-1'&gt;&lt;\/slot&gt;&lt;\/th&gt;\r\n      &lt;th&gt;&lt;slot name='title-2'&gt;&lt;\/slot&gt;&lt;\/th&gt;\r\n    &lt;\/tr&gt;\r\n    &lt;tr&gt;\r\n      &lt;td&gt;&lt;slot name='value-1.1'&gt;&lt;\/slot&gt;&lt;\/td&gt;\r\n      &lt;td&gt;&lt;slot name='value-1.2'&gt;&lt;\/slot&gt;&lt;\/td&gt;\r\n    &lt;\/tr&gt;\r\n    &lt;tr&gt;\r\n      &lt;td&gt;&lt;slot name='value-2.1'&gt;&lt;\/slot&gt;&lt;\/td&gt;\r\n      &lt;td&gt;&lt;slot name='value-2.2'&gt;&lt;\/slot&gt;&lt;\/td&gt;\r\n      &lt;\/tr&gt;\r\n  &lt;\/table&gt;\r\n  &lt;style&gt;\r\n    table {\r\n        table-layout: fixed;\r\n        border-collapse: collapse;\r\n        margin-bottom: 10px;\r\n    }\r\n    th {\r\n        width: 300px;\r\n    }\r\n    th,\r\n    td {\r\n        border: 1px solid;\r\n    }\r\n  &lt;\/style&gt;\r\n&lt;\/template&gt;\r\n<\/pre>\n<p><strong>Inside the template<\/strong>, I\u2019ve also added <strong>some basic styles<\/strong> for the table, using the <code>&lt;style&gt;<\/code> tag.<\/p>\n<p><strong>Outside the template<\/strong>, there are two <code>&lt;div&gt;<\/code> elements <strong>carrying the column titles and cell values inside <code>&lt;span&gt;<\/code><\/strong>, for two separate tables we want to add to the page.<\/p>\n<pre>\r\n&lt;div&gt;\r\n  &lt;span slot='title-1'&gt;Title A&lt;\/span&gt;\r\n  &lt;span slot='title-2'&gt;Title B&lt;\/span&gt;\r\n  &lt;span slot='value-1.1'&gt;Value A.1&lt;\/span&gt;\r\n  &lt;span slot='value-1.2'&gt;Value A.2&lt;\/span&gt;\r\n  &lt;span slot='value-2.1'&gt;Value B.1&lt;\/span&gt;\r\n  &lt;span slot='value-2.2'&gt;Value B.2&lt;\/span&gt;\r\n&lt;\/div&gt;\r\n\r\n&lt;div&gt;\r\n  &lt;span slot='title-1'&gt;Title C&lt;\/span&gt;\r\n  &lt;span slot='title-2'&gt;Title D&lt;\/span&gt;\r\n  &lt;span slot='value-1.1'&gt;Value C.1&lt;\/span&gt;\r\n  &lt;span slot='value-1.2'&gt;Value C.2&lt;\/span&gt;\r\n  &lt;span slot='value-2.1'&gt;Value D.1&lt;\/span&gt;\r\n  &lt;span slot='value-2.2'&gt;Value D.2&lt;\/span&gt;\r\n&lt;\/div&gt;\r\n<\/pre>\n<p>Each <code>&lt;span&gt;<\/code> element has a <code>slot<\/code> attribute of which value is <strong>equal to the <code>name<\/code> value<\/strong> of their corresponding <code>&lt;slot&gt;<\/code> tag inside <code>&lt;template&gt;<\/code>.<\/p>\n<p>Right now, all you can see on the page are the text strings contained in the spans, so we need to add some JavaScript as well.<\/p>\n<h2>Attaching the Shadow DOM tree<\/h2>\n<p>Using Javascript, we insert the table from inside the template into both divs <strong>as a Shadow DOM tree<\/strong>. After the insertion, the spans get placed into their respective slots inside the table and display the desired column titles or cell values. The result will be <strong>two auto-generated tables<\/strong> that use the same template.<\/p>\n<p>First, we need to check if the Shadow DOM is supported in the user\u2019s browser. The <strong><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/Element\/attachShadow\" target=\"_blank\" rel=\"noopener noreferrer\"><code>attachShadow()<\/code><\/a><\/strong> method <strong>attaches a Shadow DOM tree to an element<\/strong> and returns the root node of that Shadow DOM tree. The <code>if<\/code> condition in the code below checks if the browser supports this method by testing if the divs on the page have the <code>attachShadow<\/code> method.<\/p>\n<pre>\r\n\/\/ check if Shadow DOM is supported\r\nif ('attachShadow' in document.createElement('div')){\r\n\r\n}\r\nelse\r\n  console.warn('attachShadow not supported');\r\n<\/pre>\n<p>We create a custom variable called <code>templateContent<\/code> that <strong>serves as a reference<\/strong> to the content of the template.<\/p>\n<pre>\r\nif('attachShadow' in document.createElement('div')) {\r\n  let templateContent = document.querySelector('template').content;\r\n  let divs = document.querySelectorAll('div');\r\n\r\n  divs.forEach(function(div) {\r\n      \/\/ inside loop\r\n  });\r\n}\r\nelse\r\n  console.warn('attachShadow not supported');\r\n<\/pre>\n<p>Inside the <code>forEach<\/code> loop, a Shadow DOM tree is <strong>attached to each div<\/strong> (<code>div.attachShadow({  mode: 'open' })<\/code>).<\/p>\n<p>There are <strong>two <code>mode<\/code> options<\/strong> for <code>attachShadow<\/code>: <code>open<\/code> and <code>closed<\/code>. If <code>closed<\/code> was chosen the root node of the Shadow DOM tree <strong>would become inaccessible<\/strong> to outside DOM elements and objects.<\/p>\n<p>Then, we <strong>add a copy of the template content<\/strong> to the Shadow DOM tree using the <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/Node\/cloneNode\" target=\"_blank\" rel=\"noopener noreferrer\"><code>templateContent.cloneNode(true)<\/code><\/a> method.<\/p>\n<pre>\r\nif('attachShadow' in document.createElement('div')) {\r\n  let templateContent = document.querySelector('template').content;\r\n  let divs = document.querySelectorAll('div');\r\n\r\n  divs.forEach(function(div){\r\n    div.attachShadow({  mode: 'open' }).appendChild(\r\n        templateContent.cloneNode(true))\r\n  });\r\n}\r\nelse\r\n  console.warn('attachShadow not supported');\r\n<\/pre>\n<p>And, our dynamic HTML tables are ready, here\u2019s how the output looks like in Chrome:<\/p>\n<figure><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/assets.hongkiat.com\/uploads\/html-template-slow-tag-shadow-dom\/slot-output.jpg\" alt=\"Dynamic tables generated using HTML slots\" width=\"688\" height=\"188\"><\/figure>\n<p class=\"recommended_top\">\n\t\t\t\t\t<strong>Read Also:<\/strong>\u00a0\n\t\t\t\t\t<a target=\"_blank\" href=\"https:\/\/www.hongkiat.com\/blog\/mutationobserver-api\/\">How to Use the MutationObserver API for Tracking DOM Changes<\/a>\n\t\t\t\t<\/p>","protected":false},"excerpt":{"rendered":"<p>Speed up your HTML templates with the slow tag and Shadow DOM. Learn how to optimize your website for faster loading times.<\/p>\n","protected":false},"author":145,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[3392],"tags":[3554,506],"topic":[4520],"class_list":["entry-content","is-maxi"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v22.8 (Yoast SEO v27.8) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>How to Use HTML &lt;template&gt; &amp; &lt;slot&gt; With Shadow DOM - Hongkiat<\/title>\n<meta name=\"description\" content=\"Speed up your HTML templates with the slow tag and Shadow DOM. Learn how to optimize your website for faster loading times.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.hongkiat.com\/blog\/html-template-slow-tag-shadow-dom\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Use HTML &lt;template&gt; &amp; &lt;slot&gt; With Shadow DOM\" \/>\n<meta property=\"og:description\" content=\"Speed up your HTML templates with the slow tag and Shadow DOM. Learn how to optimize your website for faster loading times.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.hongkiat.com\/blog\/html-template-slow-tag-shadow-dom\/\" \/>\n<meta property=\"og:site_name\" content=\"Hongkiat\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/hongkiatcom\" \/>\n<meta property=\"article:published_time\" content=\"2019-07-25T15:34:28+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-04-06T13:57:38+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/assets.hongkiat.com\/uploads\/html-template-slow-tag-shadow-dom\/slot-diagram.jpg\" \/>\n<meta name=\"author\" content=\"Preethi Ranjit\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@hongkiat\" \/>\n<meta name=\"twitter:site\" content=\"@hongkiat\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Preethi Ranjit\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/html-template-slow-tag-shadow-dom\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/html-template-slow-tag-shadow-dom\\\/\"},\"author\":{\"name\":\"Preethi Ranjit\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#\\\/schema\\\/person\\\/e981676afae36d1ff5feb75094950ab3\"},\"headline\":\"How to Use HTML &lt;template&gt; &amp; &lt;slot&gt; With Shadow DOM\",\"datePublished\":\"2019-07-25T15:34:28+00:00\",\"dateModified\":\"2023-04-06T13:57:38+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/html-template-slow-tag-shadow-dom\\\/\"},\"wordCount\":743,\"publisher\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/html-template-slow-tag-shadow-dom\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/assets.hongkiat.com\\\/uploads\\\/html-template-slow-tag-shadow-dom\\\/slot-diagram.jpg\",\"keywords\":[\"DOM\",\"HTML\"],\"articleSection\":[\"Coding\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/html-template-slow-tag-shadow-dom\\\/\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/html-template-slow-tag-shadow-dom\\\/\",\"name\":\"How to Use HTML &lt;template&gt; &amp; &lt;slot&gt; With Shadow DOM - Hongkiat\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/html-template-slow-tag-shadow-dom\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/html-template-slow-tag-shadow-dom\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/assets.hongkiat.com\\\/uploads\\\/html-template-slow-tag-shadow-dom\\\/slot-diagram.jpg\",\"datePublished\":\"2019-07-25T15:34:28+00:00\",\"dateModified\":\"2023-04-06T13:57:38+00:00\",\"description\":\"Speed up your HTML templates with the slow tag and Shadow DOM. Learn how to optimize your website for faster loading times.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/html-template-slow-tag-shadow-dom\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/html-template-slow-tag-shadow-dom\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/html-template-slow-tag-shadow-dom\\\/#primaryimage\",\"url\":\"https:\\\/\\\/assets.hongkiat.com\\\/uploads\\\/html-template-slow-tag-shadow-dom\\\/slot-diagram.jpg\",\"contentUrl\":\"https:\\\/\\\/assets.hongkiat.com\\\/uploads\\\/html-template-slow-tag-shadow-dom\\\/slot-diagram.jpg\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/html-template-slow-tag-shadow-dom\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Use HTML &lt;template&gt; &amp; &lt;slot&gt; With Shadow DOM\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/\",\"name\":\"Hongkiat\",\"description\":\"Tech and Design Tips\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#organization\",\"name\":\"Hongkiat.com\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/wp-content\\\/uploads\\\/hkdc-logo-rect-yoast.jpg\",\"contentUrl\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/wp-content\\\/uploads\\\/hkdc-logo-rect-yoast.jpg\",\"width\":1200,\"height\":799,\"caption\":\"Hongkiat.com\"},\"image\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/hongkiatcom\",\"https:\\\/\\\/x.com\\\/hongkiat\",\"https:\\\/\\\/www.pinterest.com\\\/hongkiat\\\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#\\\/schema\\\/person\\\/e981676afae36d1ff5feb75094950ab3\",\"name\":\"Preethi Ranjit\",\"description\":\"A .NET developer with a JavaScript background, Preethi is expert in front-end coding, JavaScript, HTML, and CSS.\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/author\\\/preethi\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"How to Use HTML &lt;template&gt; &amp; &lt;slot&gt; With Shadow DOM - Hongkiat","description":"Speed up your HTML templates with the slow tag and Shadow DOM. Learn how to optimize your website for faster loading times.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.hongkiat.com\/blog\/html-template-slow-tag-shadow-dom\/","og_locale":"en_US","og_type":"article","og_title":"How to Use HTML &lt;template&gt; &amp; &lt;slot&gt; With Shadow DOM","og_description":"Speed up your HTML templates with the slow tag and Shadow DOM. Learn how to optimize your website for faster loading times.","og_url":"https:\/\/www.hongkiat.com\/blog\/html-template-slow-tag-shadow-dom\/","og_site_name":"Hongkiat","article_publisher":"https:\/\/www.facebook.com\/hongkiatcom","article_published_time":"2019-07-25T15:34:28+00:00","article_modified_time":"2023-04-06T13:57:38+00:00","og_image":[{"url":"https:\/\/assets.hongkiat.com\/uploads\/html-template-slow-tag-shadow-dom\/slot-diagram.jpg","type":"","width":"","height":""}],"author":"Preethi Ranjit","twitter_card":"summary_large_image","twitter_creator":"@hongkiat","twitter_site":"@hongkiat","twitter_misc":{"Written by":"Preethi Ranjit","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.hongkiat.com\/blog\/html-template-slow-tag-shadow-dom\/#article","isPartOf":{"@id":"https:\/\/www.hongkiat.com\/blog\/html-template-slow-tag-shadow-dom\/"},"author":{"name":"Preethi Ranjit","@id":"https:\/\/www.hongkiat.com\/blog\/#\/schema\/person\/e981676afae36d1ff5feb75094950ab3"},"headline":"How to Use HTML &lt;template&gt; &amp; &lt;slot&gt; With Shadow DOM","datePublished":"2019-07-25T15:34:28+00:00","dateModified":"2023-04-06T13:57:38+00:00","mainEntityOfPage":{"@id":"https:\/\/www.hongkiat.com\/blog\/html-template-slow-tag-shadow-dom\/"},"wordCount":743,"publisher":{"@id":"https:\/\/www.hongkiat.com\/blog\/#organization"},"image":{"@id":"https:\/\/www.hongkiat.com\/blog\/html-template-slow-tag-shadow-dom\/#primaryimage"},"thumbnailUrl":"https:\/\/assets.hongkiat.com\/uploads\/html-template-slow-tag-shadow-dom\/slot-diagram.jpg","keywords":["DOM","HTML"],"articleSection":["Coding"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.hongkiat.com\/blog\/html-template-slow-tag-shadow-dom\/","url":"https:\/\/www.hongkiat.com\/blog\/html-template-slow-tag-shadow-dom\/","name":"How to Use HTML &lt;template&gt; &amp; &lt;slot&gt; With Shadow DOM - Hongkiat","isPartOf":{"@id":"https:\/\/www.hongkiat.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.hongkiat.com\/blog\/html-template-slow-tag-shadow-dom\/#primaryimage"},"image":{"@id":"https:\/\/www.hongkiat.com\/blog\/html-template-slow-tag-shadow-dom\/#primaryimage"},"thumbnailUrl":"https:\/\/assets.hongkiat.com\/uploads\/html-template-slow-tag-shadow-dom\/slot-diagram.jpg","datePublished":"2019-07-25T15:34:28+00:00","dateModified":"2023-04-06T13:57:38+00:00","description":"Speed up your HTML templates with the slow tag and Shadow DOM. Learn how to optimize your website for faster loading times.","breadcrumb":{"@id":"https:\/\/www.hongkiat.com\/blog\/html-template-slow-tag-shadow-dom\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.hongkiat.com\/blog\/html-template-slow-tag-shadow-dom\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.hongkiat.com\/blog\/html-template-slow-tag-shadow-dom\/#primaryimage","url":"https:\/\/assets.hongkiat.com\/uploads\/html-template-slow-tag-shadow-dom\/slot-diagram.jpg","contentUrl":"https:\/\/assets.hongkiat.com\/uploads\/html-template-slow-tag-shadow-dom\/slot-diagram.jpg"},{"@type":"BreadcrumbList","@id":"https:\/\/www.hongkiat.com\/blog\/html-template-slow-tag-shadow-dom\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.hongkiat.com\/blog\/"},{"@type":"ListItem","position":2,"name":"How to Use HTML &lt;template&gt; &amp; &lt;slot&gt; With Shadow DOM"}]},{"@type":"WebSite","@id":"https:\/\/www.hongkiat.com\/blog\/#website","url":"https:\/\/www.hongkiat.com\/blog\/","name":"Hongkiat","description":"Tech and Design Tips","publisher":{"@id":"https:\/\/www.hongkiat.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.hongkiat.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.hongkiat.com\/blog\/#organization","name":"Hongkiat.com","url":"https:\/\/www.hongkiat.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.hongkiat.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.hongkiat.com\/blog\/wp-content\/uploads\/hkdc-logo-rect-yoast.jpg","contentUrl":"https:\/\/www.hongkiat.com\/blog\/wp-content\/uploads\/hkdc-logo-rect-yoast.jpg","width":1200,"height":799,"caption":"Hongkiat.com"},"image":{"@id":"https:\/\/www.hongkiat.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/hongkiatcom","https:\/\/x.com\/hongkiat","https:\/\/www.pinterest.com\/hongkiat\/"]},{"@type":"Person","@id":"https:\/\/www.hongkiat.com\/blog\/#\/schema\/person\/e981676afae36d1ff5feb75094950ab3","name":"Preethi Ranjit","description":"A .NET developer with a JavaScript background, Preethi is expert in front-end coding, JavaScript, HTML, and CSS.","url":"https:\/\/www.hongkiat.com\/blog\/author\/preethi\/"}]}},"jetpack_featured_media_url":"https:\/\/","jetpack_shortlink":"https:\/\/wp.me\/p4uxU-9L3","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/posts\/37513","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/users\/145"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/comments?post=37513"}],"version-history":[{"count":3,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/posts\/37513\/revisions"}],"predecessor-version":[{"id":66301,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/posts\/37513\/revisions\/66301"}],"wp:attachment":[{"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/media?parent=37513"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/categories?post=37513"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/tags?post=37513"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/topic?post=37513"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}