当前位置:网站首页>Web components series (VIII) -- custom component style settings
Web components series (VIII) -- custom component style settings
2022-07-01 03:50:00 【Programming samadhi】
Preface
Through the previous study , I also have a certain understanding of the relevant concepts and knowledge points of custom components , Today, let's learn some ways to style custom elements and their children .
Add styles directly to custom labels
index.html:
<style> my-card{
display: block; margin: 20px; width: 200px; height: 200px; border: 3px solid #000; } </style>
<my-card></my-card>
<script src="./index.js"></script>
index.js:
class MyCard extends HTMLElement {
constructor() {
super();
this.shadow = this.attachShadow({
mode: "open" });
}
}
window.customElements.define("my-card", MyCard);
The result style takes effect :
It should be noted that : Inherited from HTMLElement The custom elements of , Its style.display
The default is inline.
From the above results, it can be inferred that :
- Add... To custom elements class, And then through class The name setting style can take effect ;
- Add inline styles to custom elements , It works ;
- Give... In the custom element constructor this Add the style , It works .
Style the child elements inside the custom element
In the main DOM Set... By class name
stay style Add the following style to the label :
<style> my-card {
display: block; margin: 20px; width: 200px; height: 200px; border: 3px solid #000; } .card-header {
padding: 10px; font-weight: bold; background-color: yellow; } </style>
<my-card></my-card>
<script> class MyCard extends HTMLElement {
constructor () {
super(); this.shadow = this.attachShadow({
mode: "open"}); let headerEle = document.createElement("div"); headerEle.className = "card-header"; headerEle.innerText = "My Card"; this.shadow.appendChild(headerEle); } } window.customElements.define("my-card", MyCard); </script>
result : Don't take effect .
Through the previous study , We know : Inside the custom element is actually a Shadow DOM, It and the Lord DOM It's isolated from each other , therefore , Lord DOM The style in does not affect Shadow DOM Of .
Use JS to Shadow DOM increase style label
There are two scenarios : In the main DOM Use JS 、 stay Custom Elements In the constructor JS.
The first one is : In the main DOM Use JS to Shadow DOM increase style label :
<style> my-card {
display: block; margin: 20px; width: 200px; height: 200px; border: 3px solid #000; } </style>
<my-card>
</my-card>
<script> class MyCard extends HTMLElement {
constructor () {
super(); this.shadow = this.attachShadow({
mode: "open"}); let headerEle = document.createElement("div"); headerEle.className = "card-header"; headerEle.innerText = "My Card"; this.shadow.appendChild(headerEle); } } window.customElements.define("my-card", MyCard); // to Shadow DOM increase style label let styleEle = document.createElement("style"); styleEle.textContent = ` .card-header{ padding:10px; background-color: yellow; font-size: 16px; font-weight: bold; } `; document.querySelector("my-card").shadowRoot.appendChild(styleEle); </script>
The effect is as follows :
The second kind : stay Custom Elements In the constructor JS increase style label :
<style> my-card {
display: block; margin: 20px; width: 200px; height: 200px; border: 3px solid #000; } </style>
<my-card>
</my-card>
<script> class MyCard extends HTMLElement {
constructor () {
super(); this.shadow = this.attachShadow({
mode: "open"}); let styleEle = document.createElement("style"); styleEle.textContent = ` .card-header{ padding:10px; background-color: yellow; font-size: 16px; font-weight: bold; } `; this.shadow.appendChild(styleEle); let headerEle = document.createElement("div"); headerEle.className = "card-header"; headerEle.innerText = "My Card"; this.shadow.appendChild(headerEle); } } window.customElements.define("my-card", MyCard); </script>
The effect is as follows :
In terms of the above two ways , The second is more in line with the characteristics of componentization , And when using the first method, pay attention to , If... Will be added style The code of the tag is placed in the definition Custom Elements I would have reported a mistake ( Custom element not found ).
introduce CSS file
Use here JS establish link label , Then introduce the CSS The file sets the style for the child elements inside the custom element , The code is as follows :
<style> my-card {
display: block; margin: 20px; width: 200px; height: 200px; border: 3px solid #000; } </style>
<my-card>
</my-card>
<script> class MyCard extends HTMLElement {
constructor () {
super(); this.shadow = this.attachShadow({
mode: "open"}); let linkEle = document.createElement("link"); linkEle.rel = "stylesheet"; linkEle.href = "./my_card.css"; this.shadow.appendChild(linkEle); let headerEle = document.createElement("div"); headerEle.className = "card-header"; headerEle.innerText = "My Card"; this.shadow.appendChild(headerEle); } } window.customElements.define("my-card", MyCard); </script>
my_card.css The code is as follows :
.card-header{
padding:10px;
background-color: yellow;
font-size: 16px;
font-weight: bold;
}
The effect is as follows :
Of course , It can also be in the Lord DOM Use in JS to Shadow DOM introduce CSS file , however , This is not in line with the characteristics of componentization , So skip .
Conclusion
The above is a summary of the basic methods of styling custom elements and their child elements .
- ~
- The end of this paper , Thank you for reading !
~
Learn interesting knowledge , Make interesting friends , Create interesting souls !
Hello everyone , I am a 〖 The samadhi of programming 〗 The author of Hermit King , My official account is 『 The samadhi of programming 』, Welcome to your attention , I hope you can give me more advice !
边栏推荐
- Idea plug-in backup table
- The method to measure the similarity of two vectors: cosine similarity, pytorch calculate cosine similarity: torch nn. CosineSimilarity(dim=1, eps=1e-08)
- 整合阿里云短信的问题:无法从静态上下文中引用非静态方法
- 171. Excel 表列序号
- 【EI检索】2022年第六届材料工程与先进制造技术国际会议(MEAMT 2022)重要信息会议网址:www.meamt.org会议时间:2022年9月23-25日召开地点:中国南京截稿时间:2
- [ta - Frost Wolf May - 100 people plan] 2.3 Introduction aux fonctions communes
- Leetcode 128 longest continuous sequence (hash set)
- 6. zigzag transformation
- 208. 实现 Trie (前缀树)
- bootsrap中的栅格系统
猜你喜欢
AfxMessageBox和MessageBox的用法
Pytorch training deep learning network settings CUDA specified GPU visible
Online public network security case nanny level tutorial [reaching out for Party welfare]
盘点华为云GaussDB(for Redis)六大秒级能力
The method to measure the similarity of two vectors: cosine similarity, pytorch calculate cosine similarity: torch nn. CosineSimilarity(dim=1, eps=1e-08)
【TA-霜狼_may-《百人计划》】2.1 色彩空间
Addition without addition, subtraction, multiplication and division
【TA-霜狼_may-《百人计划》】2.3 常用函数介绍
访问阿里云存储的图片URL实现在网页直接预览略缩图而不直接下载
The difference between MFC for static libraries and MFC for shared libraries
随机推荐
431. 将 N 叉树编码为二叉树 DFS
242. valid Letter heteronyms
【TA-霜狼_may-《百人计划》】2.1 色彩空间
241. 为运算表达式设计优先级
187. 重复的DNA序列
The method to measure the similarity of two vectors: cosine similarity, pytorch calculate cosine similarity: torch nn. CosineSimilarity(dim=1, eps=1e-08)
Cygwin的下载和安装配置
241. Design priorities for operational expressions
The programmer's girlfriend gave me a fatigue driving test
283.移动零
Deep learning | rnn/lstm of naturallanguageprocessing
Leetcode:剑指 Offer 59 - I. 滑动窗口的最大值
Sort linked list (merge sort)
Libevent Library Learning
AfxMessageBox和MessageBox的用法
[EI conference] the Third International Conference on nanomaterials and nanotechnology in 2022 (nanomt 2022)
[TA frost wolf \u may- hundred people plan] 2.4 traditional empirical lighting model
【TA-霜狼_may-《百人计划》】1.1 渲染流水线
pytorch nn. AdaptiveAvgPool2d(1)
在 C 中声明函数之前调用函数会发生什么?