如何让导入的css对阴影dom中的元素产生影响? [英] How to let imported css have effects on elements in the shadow dom?
问题描述
假设我想使用阴影dom创建自定义元素。模板中的某些元素具有在链接的css文件中指定的类名。现在我想让css规则对元素产生影响。但由于影子dom样式边界,我无法实现这一点。
Say if I want to create a custom element using shadow dom. Some elements in the template have class names specified in the linked css file. Now I want to let the css rules have effects on the elements. But I can't achieve that because of the shadow dom style boundary.
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css">
<template id="blog-header">
<header>
<h1>DreamLine</h1>
<nav>
<ul>
<li><a href="#0">Tour</a></li>
<li><a href="#0">Blog</a></li>
<li><a href="#0">Contact</a></li>
<li><a href="#0">Error</a></li>
<li><a href="#0"><i class="fa fa-search"></i>Search</a></li>
</ul>
</nav>
</header>
</template>
<script type="text/javascript">
var importDoc = document.currentScript.ownerDocument;
var proto = Object.create(HTMLElement.prototype, {
createdCallback: {
value: function() {
var t = importDoc.querySelector("#blog-header");
var clone = document.importNode(t.content, true);
this.createShadowRoot().appendChild(clone);
}
}
});
document.registerElement("blog-header", {prototype: proto});
</script>
你看, fa-search
是一个在font-awesome css文件中定义的类,我如何设置< i>
元素?
You see, fa-search
is a class defined in the font-awesome css file, how can I style the <i>
element?
推荐答案
使用导入的字体(即 FontAwesome )在Shadow DOM中,您应该:
To use an imported font (i.e. FontAwesome) in a Shadow DOM, you should:
1°声明字体
首先,在主文档中包含< link rel =stylesheet>
元素。它将声明一个 @ font-face
CSS规则,该字体将使该字体可用于文档中的所有文本。
First, include the <link rel="stylesheet">
element in the main document. It will declare a @font-face
CSS rule that will make the font available for all the text in the document.
2°导入样式表
然后,使用 @import url导入相同的文件
< template>
节点中的CSS规则,以使 .fa - *
类选择器可用于Shadow DOM:
Then, import the same file with an @import url
CSS rule in the <template>
node to make the .fa-*
classes selectors available from the Shadow DOM :
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css">
<template id="blog-header">
<style>
@import url("https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css")
</style>
<header>
<h1>DreamLine</h1>
//...
</header>
</template>
var importDoc = document.currentScript.ownerDocument;
var proto = Object.create(HTMLElement.prototype, {
createdCallback: {
value: function() {
var t = importDoc.querySelector("#blog-header");
var clone = document.importNode(t.content, true);
this.createShadowRoot().appendChild(clone);
}
}
});
document.registerElement("blog-header", {prototype: proto});
/*
@font-face {
font-family: "FontAwesome";
src: url("https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome-webfont.woff2?v=4.5.0") format('woff2');
}
*/
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css">
<template id="blog-header">
<style>
@import url("https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css")
</style>
<header>
<h1>DreamLine</h1>
<nav>
<ul>
<li><a href="#0">Tour</a></li>
<li><a href="#0">Blog</a></li>
<li><a href="#0">Contact</a></li>
<li><a href="#0">Error</a></li>
<li><a href="#0"><i class="fa fa-search"></i>Search</a></li>
</ul>
</nav>
</header>
</template>
<blog-header></blog-header>
这篇关于如何让导入的css对阴影dom中的元素产生影响?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!