<template id="my-template">
<style>
.wrapper {
color: blue;
}
</style>
<span class="wrapper"><slot></slot></span>
</template>
class MyElement extends HTMLElement {
constructor() {
super();
const template = document.getElementById('my-template').content;
const shadow = this.attachShadow({ mode: 'open' });
shadow.appendChild(template.cloneNode(true));
const wrapper = document.createElement('span');
wrapper.setAttribute('class', 'wrapper');
const text = document.createElement('span');
text.textContent = this.getAttribute('text');
wrapper.appendChild(text);
shadow.appendChild(wrapper);
}
}
customElements.define('my-element', MyElement);
<!DOCTYPE html>
<html>
<head>
<title>Web Components Example</title>
</head>
<body>
<my-element text="Hello, Web Components!"></my-element>
</body>
</html>