简体   繁体   English

全局样式未应用于Chrome 36中Polymer元素中的Shadow DOM子项

[英]Global styles not applied to Shadow DOM children in Polymer elements in Chrome 36

In Chrome 34, styles defined in the header with a <style> tag will affect elements within the Shadow DOM of a Polymer element. 在Chrome 34中,标题中使用<style>标记定义的<style>将影响Polymer元素的Shadow DOM中的元素。 In Chrome 36, this isn't the case. 在Chrome 36中,情况并非如此。

I can move all of our styles directly into the element's template, but sometimes our css selectors bridge the Shadow DOM gap. 我可以将所有样式直接移动到元素的模板中,但有时我们的css选择器会桥接Shadow DOM间隙。 eg: 例如:

.something-outside .something-inside { ... }
.something-outside.foo .something-inside { ... }

The latter case is more difficult since it needs information about two scopes. 后一种情况更加困难,因为它需要有关两个范围的信息。

What's the correct way to deal with this? 处理这个问题的正确方法是什么? Is there a feature of Polymer that will let global styles through? 聚合物的特性是否会让全球风格通过?

Hilariously, I can't add any images or more than 2 links without 10 stackoverflow reputation points (whee), so the best I can offer is this jsbin: 非常有趣的是,我无法添加任何图像或超过2个链接而没有10个stackoverflow信誉点(whee),所以我能提供的最好的是这个jsbin:

http://jsbin.com/vobowigi/1/edit http://jsbin.com/vobowigi/1/edit

What you're seeing is the difference between the polyfill and native Shadow DOM. 你看到的是polyfill和native Shadow DOM之间的区别。 The selectors that applied before no longer target elements in the SD. 之前应用的选择器不再是SD中的目标元素。

To styles elements from outside the SD, there's the /deep/ combinator and ::shadow pseudo element. 要从SD外部对样式元素进行样式处理,可以使用/deep/ combinator和::shadow伪元素。 For example, to style all h1 s red, no matter what tree they appear in use: 例如,要将所有h1的样式设置为红色,无论它们出现在哪个树中使用:

body /deep / h1 {
  color: red;
}

These two articles contain all the details for SD styling stuff: 这两篇文章包含SD样式的所有细节:

If, like me, you were looking for a way to centrally style common elements throughout your site and not just reach into one shadow DOM, a strategy I figured out is to use a global stylesheet as a separate file and include it in the templates of all your custom elements. 如果像我一样,你正在寻找一种方法来集中设计整个网站中的常见元素而不仅仅是一个影子DOM,我想出的一个策略是使用全局样式表作为单独的文件并将其包含在模板中所有自定义元素。 This way you're not repeating styles or tediously referencing nested levels of elements, but you can still take advantage of style scoping. 这样您就不会重复样式或繁琐地引用嵌套级别的元素,但您仍然可以利用样式范围。

My answer might be a bit late, but i had a similar problem. 我的回答可能有点晚了,但我遇到了类似的问题。 But the issue only arose after i had changed from shady DOM to shadow DOM. 但是这个问题只是在我从shady DOM变为shadow DOM之后才出现的。 If you use shadow DOM, then you need to include the styles as is described here: 如果使用shadow DOM,则需要包含如下所述的样式:

https://www.polymer-project.org/1.0/docs/devguide/styling#style-modules https://www.polymer-project.org/1.0/docs/devguide/styling#style-modules

Eg you would do this: 你会这样做:

<!-- shared-styles.html -->
<dom-module id="shared-styles">
  <template>
    <style>
      .red { color: red; }
    </style>
  </template>
</dom-module>

and then include it in your elements like so: 然后将其包含在您的元素中,如下所示:

<style include="shared-styles">
  :host { display: block; }
</style>

I hope this helps someone. 我希望这可以帮助别人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM