I'm in Shadow DOM. My markup was stamped from a
I'm in Shadow DOM. My markup was stamped from a
` These few lines of code pack a lot of punch. Let's understanding everything that is happening: We've registered a new element in HTML: `` The element's DOM was created from a `` The element's scary details are hidden away using Shadow DOM Shadow DOM gives the element style encapsulation (e.g. `p {color: orange;}` isn't
turning the entire page orange) So good! Styling custom elements As with any HTML tag, users of your custom tag can style it with selectors: `
Do
Re
Mi
` Styling elements that use Shadow DOM The rabbit hole goes much *much* deeper when you bring Shadow DOM into the mix.
Custom elements that use Shadow DOM inherit its great benefits. Shadow DOM infuses an element with style encapsulation. Styles defined in a Shadow Root don't
leak out of the host and don't bleed in from the page. **In the case of a custom element, the element itself is the host.** The properties of style encapsulation also allow custom elements to
define default styles for themselves. Shadow DOM styling is a huge topic! If you want to learn more about it, I recommend a few of my other articles: "[A Guide to Styling Elements](http://www.polymer-project.org/articles/styling-elements.html)" on [Polymer](http://www.polymer-project.org)'s documentation. "[Shadow DOM 201: CSS & Styling](/articles/shadowdom-201)" here. FOUC prevention using :unresolved To mitigate [FOUC](http://en.wikipedia.org/wiki/Flash_of_unstyled_content), custom elements spec
out a new CSS pseudo class, `:unresolved`. Use it to target unresolved elements,
right up until the point where the browser invokes your `createdCallback()` (see lifecycle methods).
Once that happens, the element is no longer an unresolved element. The upgrade process is
complete and the element has transformed into its definition. **Example**: fade in "x-foo" tags when they're registered: `` Keep in mind that `:unresolved` only applies to unresolved elements,
not to elements that inherit from `HTMLUnknownElement` (see How elements are upgraded). `
I'm black because :unresolved doesn't apply to "panel".
It's not a valid custom element name.
I'm red because I match x-panel:unresolved.` History and browser support Feature detection Feature detecting is a matter of checking if `document.registerElement()` exists: `function supportsCustomElements() { return 'registerElement' in document; } if (supportsCustomElements()) { // Good to go! } else { // Use other libraries to create components. }` Browser support `document.registerElement()` first started landing behind a flag in Chrome 27 and Firefox ~23. However, the specification has evolved quite a bit since then. Chrome 31 is the first to have
true support for the updated spec. Until browser support is stellar, there's a [polyfill](http://www.polymer-project.org/platform/custom-elements.html) which is used by Google's [Polymer](http://polymer-project.org) and Mozilla's [X-Tag](http://www.x-tags.org/). What happened to HTMLElementElement? For those that have followed the standardization work, you know there was once ``.
It was the bees knees. You could use it to declaratively register new elements: `
...
` Unfortunately, there were too many timing issues with the upgrade process,
corner cases, and Armageddon-like scenarios to work it all out. `` had to be shelved. In August 2013, Dimitri Glazkov posted to [public-webapps](http://lists.w3.org/Archives/Public/public-webapps/2013JulSep/0287.html) announcing its removal, at least for now. It's worth noting that Polymer implements a declarative form of element registration
with ``. How? It uses `document.registerElement('polymer-element')` and
the techniques I described in Creating elements from a template. Conclusion Custom elements give us the tool to extend HTML's vocabulary, teach it new tricks,
and jump through the wormholes of the web platform. Combine them with the other
new platform primitives like Shadow DOM and ``, and we start to realize
the picture of Web Components. Markup can be sexy again! If you're interested in getting started with web components, I recommend checking
out [Polymer](http://polymer-project.org). It's got more than enough to get you going. Except as otherwise noted, the content of this page is licensed under the [Creative Commons Attribution 4.0 License](https://creativecommons.org/licenses/by/4.0/), and code samples are licensed under the [Apache 2.0 License](https://www.apache.org/licenses/LICENSE-2.0). For details, see the [Google Developers Site Policies](https://developers.google.com/site-policies). Java is a registered trademark of Oracle and/or its affiliates. Last updated 2011-10-14 UTC.