Use of Pseudo-Elements in Polymer.js

巧了我就是萌 提交于 2020-01-15 09:28:50

问题


I'm taking my first steps with Polymer.js, and I'm struggling with creating a pseudo-element.

This is what I tried:

In my host document:

<style type="text/css">
  #host::x-a-cool-test {
    color: green;
  }
</style>

<div id="host">
  <my-custom-element></my-custom-element>
</div>

In my custom element:

<element name="my-custom-element">
  <template>
    <style>
      @host {
        * { display: block; color: blue; }
      }          
     </style>
     <div id="group" pseudo="x-a-cool-test">
        just some text
     </div>
  </template>
  <script>
    Polymer.register(this);
  </script>
</element>

That will show just my text in blue. That is correct, because according to this, rules wrapped in a @host have higher specificity than any selector in the parent page.

My question:

If I delete color: blue from inside the @host block in my template, the text is shown in black and NOT green as I would expect. Why is that???


回答1:


I believe this plunker works how you want it to. Basically, the CSS pseudo-element has to be applied directly to the custom element (in this case the my-custom-element). I switched id="host" to it (instead of its parent div) and the code worked.

<div>
  <my-custom-element id="host"></my-custom-element>
</div>

Note: The overriding nature of @host may change. Some (myself included) think it should be more for providing default, fallback styles. In this case rules in the host document will override @host rules instead of the other way around.



来源:https://stackoverflow.com/questions/17274179/use-of-pseudo-elements-in-polymer-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!