Back to the article · Zurück zum Artikel (Deutsch)
Example 10: Wrapper around the slot (works)
Rule 5: Don't style the slot
The same component, but padding and background sit on a <div class="box"> around the slot.
How to try it
- Look at the live example below the output boxes.
- Compare with example 09.
console.log() and errors on this page, the same text as in the developer toolsPink background, spacing, and all the text is dark red, including the bold word from the light DOM: the color is inherited through the flat tree.
Live example
Source
<my-alert> <strong>Warning:</strong> The build is still running. </my-alert>
<script type="module">
class MyAlert extends HTMLElement {
constructor() {
super();
this.attachShadow({ mode: 'open' }).innerHTML = `
<style>
.box { padding: 1rem; background: #fee; color: darkred; }
</style>
<div class="box"><slot></slot></div>
`;
}
}
customElements.define('my-alert', MyAlert);
</script>