Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 803411 of <shadow>

  • Revision slug: Web/HTML/Element/Shadow
  • Revision title: <shadow>
  • Revision id: 803411
  • Created:
  • Creator: m32po
  • Is current revision? No
  • Comment

Revision Content

Summary

The HTML <shadow> element is used as a shadow DOM {{glossary("insertion point")}}. You might use it if you have created multiple shadow roots under a shadow host. It is not useful in ordinary HTML. It is used with Web Components.

Attributes

This element includes the global attributes.

Example

Here is a simple example of using the <shadow> element. It is an HTML file with everything needed in it.

Note: This is an experimental technology. For this code to work, the browser you display it in must support Web Components. See Enabling Web Components in Firefox.

<html>
  <head></head>
  <body>

  <!-- This <div> will hold the shadow roots. -->
  <div>
    <!-- This heading will not be displayed -->
    <h4>My Original Heading</h4>
  </div>

  <script>
    // Get the <div> above with its content
    var origContent = document.querySelector('div');
    // Create the first shadow root
    var shadowroot1 = origContent.createShadowRoot();
    // Create the second shadow root
    var shadowroot2 = origContent.createShadowRoot();

    // Insert something into the older shadow root
    shadowroot1.innerHTML =
      '<p>Older shadow root inserted by &lt;shadow&gt;</p>';
    // Insert into younger shadow root, including <shadow>.
    // The previous markup will not be displayed unless
    // <shadow> is used below.
    shadowroot2.innerHTML =
      '<shadow></shadow> <p>Younger shadow root, displayed because it is the youngest.</p>';
  </script>

  </body>
</html>

If you display this in a web browser it should look like the following.

shadow example

Specifications

Specification Status Comment
{{SpecName('Shadow DOM', "#the-shadow-element", "shadow")}} {{Spec2('Shadow DOM')}}  

Browser compatibility

{{CompatibilityTable}}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support 35 {{CompatGeckoDesktop("28")}} [1] {{CompatNo}} 26 {{CompatNo}}
Feature Android Firefox Mobile (Gecko) IE Phone Opera Mobile Safari Mobile
Basic support 37 {{CompatGeckoMobile("28")}} [1] {{CompatNo}} {{CompatUnknown}} {{CompatUnknown}}

[1] If Shadow DOM is not enabled in Firefox, <shadow> elements will behave like {{domxref("HTMLUnknownElement")}}. Shadow DOM was first implemented in Firefox 33 and is behind a preference, dom.webcomponents.enabled, which is disabled by default.

See also

  • Web Components
  • {{HTMLElement("content")}}, {{HTMLElement("template")}}, {{HTMLElement("element")}}
{{HTMLRef}}

Revision Source

<h2 id="Summary" name="Summary">Summary</h2>

<p>The <strong><a href="/en-US/docs/Web/HTML">HTML</a>&nbsp;<code>&lt;shadow&gt;</code></strong>&nbsp;element is used as a shadow DOM {{glossary("insertion point")}}. You might use it if you have created multiple shadow roots under a shadow host. It is not useful in ordinary HTML. It is used with <a href="/en-US/docs/Web/Web_Components">Web Components</a>.</p>

<ul class="htmlelt">
 <li><dfn><a href="/en-US/docs/Web/HTML/Content_categories" title="HTML/Content_categories">Content categories</a></dfn> <a href="/en-US/docs/Web/HTML/Content_categories#Transparent_content_model" title="HTML/Content_categories#Transparent_content_model">Transparent content</a>.</li>
 <li><dfn>Permitted content</dfn> <a href="/en-US/docs/Web/HTML/Content_categories#Flow_content">Flow content</a>.</li>
 <li><dfn>Tag omission</dfn> {{no_tag_omission}}</li>
 <li><dfn>Permitted parent elements</dfn>Any element that accepts flow content.</li>
 <li><dfn>DOM interface</dfn> {{domxref("HTMLShadowElement")}}</li>
</ul>

<h2 id="Attributes" name="Attributes">Attributes</h2>

<p>This element includes the <a href="/en-US/docs/Web/HTML/Global_attributes">global attributes</a>.</p>

<h2 id="Example">Example</h2>

<p>Here is a simple example of using the <code>&lt;shadow&gt;</code> element. It is an HTML file with everything needed in it.</p>

<div class="note">
<p><strong>Note:</strong>&nbsp;This is an experimental technology. For this code to work, the browser you display it in must support Web Components. See <a href="/en-US/docs/Web/Web_Components#Enabling_Web_Components_in_Firefox">Enabling Web Components in Firefox</a>.</p>
</div>

<pre class="brush: html">
&lt;html&gt;
  &lt;head&gt;&lt;/head&gt;
  &lt;body&gt;

&nbsp; &lt;!-- This &lt;div&gt; will hold the shadow roots. --&gt;
&nbsp; &lt;div&gt;
&nbsp; &nbsp; &lt;!-- This heading will not be displayed --&gt;
&nbsp; &nbsp; &lt;h4&gt;My Original Heading&lt;/h4&gt;
&nbsp; &lt;/div&gt;

&nbsp; &lt;script&gt;
&nbsp; &nbsp; // Get the &lt;div&gt; above with its content
&nbsp; &nbsp; var origContent = document.querySelector('div');
&nbsp; &nbsp; // Create the first shadow root
&nbsp; &nbsp; var shadowroot1 = origContent.createShadowRoot();
&nbsp; &nbsp; // Create the second shadow root
&nbsp; &nbsp; var shadowroot2 = origContent.createShadowRoot();

&nbsp; &nbsp; // Insert something into the older shadow root
&nbsp; &nbsp; shadowroot1.innerHTML =
&nbsp; &nbsp; &nbsp; '&lt;p&gt;Older shadow root inserted by &amp;lt;shadow&amp;gt;&lt;/p&gt;';
&nbsp; &nbsp; // Insert into younger shadow root, including &lt;shadow&gt;.
&nbsp; &nbsp; // The previous markup will not be displayed unless
&nbsp; &nbsp; // &lt;shadow&gt; is used below.
&nbsp; &nbsp; shadowroot2.innerHTML =
&nbsp; &nbsp; &nbsp; '&lt;shadow&gt;&lt;/shadow&gt; &lt;p&gt;Younger shadow root, displayed because it is the youngest.&lt;/p&gt;';
&nbsp; &lt;/script&gt;

  &lt;/body&gt;
&lt;/html&gt;
</pre>

<p>If you display this in a web browser it should look like the following.</p>

<p><img alt="shadow example" src="https://mdn.mozillademos.org/files/10083/shadow-example.png" style="height:343px; width:641px" /></p>

<h2 id="Specifications">Specifications</h2>

<table class="spec-table standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('Shadow DOM', "#the-shadow-element", "shadow")}}</td>
   <td>{{Spec2('Shadow DOM')}}</td>
   <td>&nbsp;</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility" name="Browser_compatibility">Browser compatibility</h2>

<p>{{CompatibilityTable}}</p>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari (WebKit)</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>35</td>
   <td><span style="font-size:12px; line-height:18px">{{CompatGeckoDesktop("28")}}</span>&nbsp;[1]</td>
   <td>{{CompatNo}}</td>
   <td>26</td>
   <td><span style="font-size:12px; line-height:18px">{{CompatNo}}</span></td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>IE Phone</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>37</td>
   <td>{{CompatGeckoMobile("28")}} [1]</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<p>[1] If Shadow DOM is not enabled in Firefox, <code>&lt;shadow&gt;</code> elements will behave like {{domxref("HTMLUnknownElement")}}. Shadow DOM was first implemented in Firefox 33 and is behind a preference, <code>dom.webcomponents.enabled</code>, which is disabled by default.</p>

<h2 id="See_also" name="See_also">See also</h2>

<ul>
 <li><a href="/en-US/docs/Web/Web_Components">Web Components</a></li>
 <li>{{HTMLElement("content")}}, {{HTMLElement("template")}},&nbsp;{{HTMLElement("element")}}</li>
</ul>

<div>{{HTMLRef}}</div>
Revert to this revision