Shadow Dom ::内容css选择器优先级?

时间:2015-01-05 14:14:32

标签: html css html5 css-selectors shadow-dom

我有像这样的影子dom元素:

<style>
   ::content div{
      padding-left:130px;
   }
</style>

<div class="shadow">
   foo
   <content select="div"></content>
</div>

从页面上加载的样式尝试使用这样的选择器覆盖它

.shadow /deep/ div{
   padding-left:50px;
}

但影子dom中的选择器具有更高的优先级。我发现给第二选择器更高优先级的唯一方法是使用重要的。还有另一种方式吗?

1 个答案:

答案 0 :(得分:1)

如果您的/deep/来自轻型DOM,那么您实际上并不需要div

我认为你有两个同等重要的选择器:::content div.shadow div。解决这个问题的一种方法是给你的div一个班级,并让覆盖的样式使用它来提高他们的分数。

ex:jsbin

<!DOCTYPE html>
<html>
  <head>
    <meta charset=utf-8 />
    <title>Polymer</title>
    <script src="http://www.polymer-project.org/components/webcomponentsjs/webcomponents.js"></script>
    <link rel="import" href="http://www.polymer-project.org/components/polymer/polymer.html">
    <style>
      .widget .special {
        color: blue;
      }
    </style>
  </head>
  <body>

    <polymer-element name="x-foo">
      <template>
        <style>
          ::content > div {
            color: red;
          }
        </style>
        <h1>Hello from x-foo</h1>
        <content select="div"></content>
      </template>
      <script>
        Polymer({

        });
      </script>
    </polymer-element>

    <x-foo class="widget">
      <div class="special">Hello World</div>
    </x-foo>

  </body>
</html>
相关问题