PolymerJS: Styling a custom element's injected DOM

We've learnt how to inject DOM into a custom element. And we know how to style the overall host element.

If we want to style the injected nodes in that custom tag, we can use the ::content CSS selector in the custom element.

For example, if we want to style the <desc> tag below:

  <desc>So.... hi....</desc>

Then in the style tag in the custom element, we'd use the ::content selector

:host ::content desc {
    color: yellow;

Note we're selecting the desc tag within the content of the custom element.

We also specify the :host tag -- or a wrapper element around the <content> tag in the custom element's local dom -- otherwise the style will apply to the whole document.

This is because in ShadyDOM -- which is used per default for performance reasons -- the ::content specifier is removed. So by specify :host or a wrapper class, it'll be encapsulated to the custom element.

Full source: index.html

        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <script src="bower_components/webcomponentsjs/webcomponents-lite.min.js"></script>
        <link rel="import" href="bower_components/polymer/polymer.html">
        <link rel="import" href="an-ele.html">
            <desc>So.... hi....</desc>

Full source: an-ele.html

<dom-module id="an-ele">

        :host ::content desc {
            color: yellow;
            Name: <content select='name'></content><br>
            Desc: <content select='desc'></content>
            is: "an-ele"

polymer polymer-styles

Edit on github
comments powered by Disqus
Click me