正如在另一個答案中所述,要設置陰影DOM的主機樣式,請使用@host
選擇器。在自定義元素的情況下,自定義元素的主機本身。
下面是如何從自定義元素的<style>
標記中對主機元素或自定義元素本身進行設置的示例。
<!DOCTYPE html>
<html>
<head>
<title>index</title>
<script src="packages/polymer/boot.js"></script>
</head>
<body>
<polymer-element name="my-element">
<template>
<style>
@host {
my-element {
display: block;
border: 1px solid black;
}
}
p {
color: red;
}
#message {
color: pink;
}
.important {
color: green;
}
</style>
<p>Inside element, should be red</p>
<div id="message">
The message should be pink
</div>
<div class="important">
Important is green
</div>
<div>
<content></content>
</div>
</template>
<script type="application/dart" src="index.dart"></script>
</polymer-element>
<p>outside of element, should be black</p>
<div id="message">
The outside message should be black
</div>
<div class="important">
Outside important is black
</div>
<my-element>Hello from content</my-element>
<!-- If the script is just an empty main, it's OK to include inline. -->
<!-- Otherwise, put the app into a separate .dart file. -->
<script type="application/dart">main() {}</script>
</body>
</html>
通知的@host
塊樣式:
@host {
my-element {
display: block;
border: 1px solid black;
}
}
由於這種特殊的定製元素不會延伸任何元素,它不會默認爲一個塊。
這裏是它看起來風格像時:
這些日子裏,你不再需要polymer.dart構造屬性。至少,我不需要它。 –