{"id":23014,"date":"2024-02-21T09:02:55","date_gmt":"2024-02-21T08:02:55","guid":{"rendered":"https:\/\/nelkodev.com\/blog\/shadow-dom-y-light-dom-una-introduccion-a-los-web-components\/"},"modified":"2024-06-03T17:30:46","modified_gmt":"2024-06-03T16:30:46","slug":"shadow-dom-y-light-dom-una-introduccion-a-los-web-components","status":"publish","type":"post","link":"https:\/\/nelkodev.com\/en\/blog\/shadow-dom-and-light-dom-an-introduction-to-web-components\/","title":{"rendered":"Shadow DOM and Light DOM: An introduction to Web Components"},"content":{"rendered":"<p>In the world of web development, Web Components have revolutionized the way we build applications and websites. With the ability to encapsulate code and styles, Web Components provide an efficient way to create reusable and customizable components. In this article, we will explore two key concepts in Web Components: the Shadow DOM and the Light DOM, and how they relate to each other. In addition, we will delve into the topic of the article: What is the Light DOM? and how it is used in Javascript.<\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_80 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #ffffff;color:#ffffff\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewbox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #ffffff;color:#ffffff\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewbox=\"0 0 24 24\" version=\"1.2\" baseprofile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/nelkodev.com\/en\/blog\/shadow-dom-and-light-dom-an-introduction-to-web-components\/#%C2%BFQue_son_los_Web_Components\" >What are Web Components?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/nelkodev.com\/en\/blog\/shadow-dom-and-light-dom-an-introduction-to-web-components\/#Introduccion_al_Shadow_DOM\" >Introduction to Shadow DOM<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/nelkodev.com\/en\/blog\/shadow-dom-and-light-dom-an-introduction-to-web-components\/#%C2%BFQue_es_el_Light_DOM\" >What is Light DOM?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/nelkodev.com\/en\/blog\/shadow-dom-and-light-dom-an-introduction-to-web-components\/#%C2%BFComo_interactuan_el_Shadow_DOM_y_el_Light_DOM\" >How do the Shadow DOM and Light DOM interact?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/nelkodev.com\/en\/blog\/shadow-dom-and-light-dom-an-introduction-to-web-components\/#%C2%BFQue_es_Javascript_y_como_se_utiliza_en_el_Light_DOM\" >What is Javascript and how is it used in the Light DOM?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/nelkodev.com\/en\/blog\/shadow-dom-and-light-dom-an-introduction-to-web-components\/#Conclusion\" >Conclusion<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/nelkodev.com\/en\/blog\/shadow-dom-and-light-dom-an-introduction-to-web-components\/#Preguntas_frecuentes\" >Frequently asked questions<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/nelkodev.com\/en\/blog\/shadow-dom-and-light-dom-an-introduction-to-web-components\/#%C2%BFCual_es_la_diferencia_entre_el_Shadow_DOM_y_el_Light_DOM\" >What is the difference between Shadow DOM and Light DOM?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/nelkodev.com\/en\/blog\/shadow-dom-and-light-dom-an-introduction-to-web-components\/#%C2%BFPor_que_es_importante_el_Shadow_DOM_en_los_Web_Components\" >Why is Shadow DOM important in Web Components?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/nelkodev.com\/en\/blog\/shadow-dom-and-light-dom-an-introduction-to-web-components\/#%C2%BFPuedo_utilizar_Javascript_en_el_Light_DOM\" >Can I use Javascript in the Light DOM?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/nelkodev.com\/en\/blog\/shadow-dom-and-light-dom-an-introduction-to-web-components\/#%C2%BFDonde_puedo_aprender_mas_sobre_los_Web_Components_y_su_uso_en_Javascript\" >Where can I learn more about Web Components and their use in Javascript?<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"%C2%BFQue_son_los_Web_Components\"><\/span>What are Web Components?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Web Components are a set of standard web technologies that allow us to create our own custom, reusable HTML elements. These elements encapsulate both the code (HTML, CSS, JavaScript) and the associated styles, ensuring that they are not affected by other styles or scripts on the page. Web Components are made up of three main technologies: Templates, Custom Elements and Shadow DOM.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Introduccion_al_Shadow_DOM\"><\/span>Introduction to Shadow DOM<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The Shadow DOM is one of the fundamental technologies of Web Components. It is a way to encapsulate the structure of a component and its associated styles, preventing them from leaking out and affecting the rest of the page. You can even use multiple Shadow DOMs on the same page without interfering with each other.<\/p>\n<p>The Shadow DOM is created using the ` element<\/p>\n<pre>yes <shadow-root><\/shadow-root>` inside the custom element or custom element. Within the Shadow DOM, we can define our own HTML structure and apply specific styles just for that component.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"%C2%BFQue_es_el_Light_DOM\"><\/span>What is Light DOM?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The Light DOM, as the name implies, is the &quot;light&quot; DOM that sits outside of the Shadow DOM. It is the global DOM of the page that contains the in-context elements with which we are working and where all the elements that are not encapsulated within the Web Components are located.<\/p>\n<p>The Light DOM is where the elements that interact with the custom component are located. These elements can be passed as children of the component or added dynamically using JavaScript. By using the Light DOM, we can take advantage of the power of Web Components by combining them with other HTML elements and customizing their behavior.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"%C2%BFComo_interactuan_el_Shadow_DOM_y_el_Light_DOM\"><\/span>How do the Shadow DOM and Light DOM interact?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The Shadow DOM and the Light DOM interact in a complementary way. The Shadow DOM provides safe encapsulation for our custom elements, preventing their structure and styles from affecting other elements on the page. Meanwhile, the Light DOM allows us to add elements and customize the behavior of the component using standard HTML elements.<\/p>\n<p>When we use a custom component on a page, the browser creates a separate instance of the Shadow DOM for that component, maintaining the encapsulation of its structure and styles. However, elements and styles defined in the Light DOM can interact with the custom component, allowing for greater flexibility and customization.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"%C2%BFQue_es_Javascript_y_como_se_utiliza_en_el_Light_DOM\"><\/span>What is Javascript and how is it used in the Light DOM?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>In web development, Javascript is a programming language that allows us to add interactivity and functionality to our sites and applications. In the context of the Light DOM, we can use Javascript to perform operations and manipulations on standard HTML elements that interact with our custom components.<\/p>\n<p>For example, we can use Javascript to add and remove elements from the Light DOM, modify attributes, or even apply additional styles to elements inside the Light DOM. This gives us great control and flexibility to customize and adjust the behavior of our Web Components using Javascript.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Web Components are a powerful tool for modern web development, and the Shadow DOM and Light DOM are the two key concepts you need to understand to take full advantage of their potential. The Shadow DOM provides safe encapsulation of component structure and styles, while the Light DOM allows us to interact with other standard HTML elements and customize their behavior.<\/p>\n<p>In short, Shadow DOM and Light DOM work together to provide a flexible and reusable solution in web application development. By understanding how they relate to each other and how they are used together, we can create more powerful, encapsulated custom components.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Preguntas_frecuentes\"><\/span>Frequently asked questions<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><span class=\"ez-toc-section\" id=\"%C2%BFCual_es_la_diferencia_entre_el_Shadow_DOM_y_el_Light_DOM\"><\/span>What is the difference between Shadow DOM and Light DOM?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Shadow DOM is a Web Components technology that allows the structure and styles of a component to be encapsulated, preventing them from affecting the rest of the page. On the other hand, the Light DOM is the &quot;light&quot; DOM that is outside the Shadow DOM and contains the non-encapsulated elements.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"%C2%BFPor_que_es_importante_el_Shadow_DOM_en_los_Web_Components\"><\/span>Why is Shadow DOM important in Web Components?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>The Shadow DOM is important in Web Components because it ensures that the structure and styles of the components are not affected by other elements and scripts on the page. This provides secure encapsulation, making it easy to reuse and customize components in different contexts.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"%C2%BFPuedo_utilizar_Javascript_en_el_Light_DOM\"><\/span>Can I use Javascript in the Light DOM?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Yes, you can use Javascript in the Light DOM. Javascript allows you to perform operations and manipulations on standard HTML elements that interact with your custom components, giving you greater control and flexibility to customize their behavior.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"%C2%BFDonde_puedo_aprender_mas_sobre_los_Web_Components_y_su_uso_en_Javascript\"><\/span>Where can I learn more about Web Components and their use in Javascript?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>You can learn more about Web Components and their use in Javascript on my blog, nelkodev.com. There you will find a variety of educational articles and tutorials that will help you better understand this technology and how to implement it in your projects.<\/p>","protected":false},"excerpt":{"rendered":"<p>In the world of web development, Web Components have revolutionized the way we build applications and websites. With the ability to encapsulate code and styles, Web Components provide an efficient way to create reusable and customizable components. In this article, we will explore two key concepts in Web Components: the Shadow DOM and the Web Components Framework.<\/p>","protected":false},"author":1,"featured_media":23015,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[420,1127],"tags":[205,886,743,545,885,221,722,37,47,742],"class_list":["post-23014","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog","category-webcomponents","tag-blog","tag-components","tag-dom","tag-introduccion","tag-light","tag-los","tag-shadow","tag-una","tag-web","tag-webcomponents"],"_links":{"self":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/posts\/23014","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/comments?post=23014"}],"version-history":[{"count":0,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/posts\/23014\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/media\/23015"}],"wp:attachment":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/media?parent=23014"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/categories?post=23014"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/tags?post=23014"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}