Custom elements with styles

Custom elements can be styled just like standard HTML elements, by applying CSS styles to them. There are a few different ways to apply styles to a custom element, depending on your requirements.

One way to apply styles to a custom element is to define the styles in a <style> tag within the <template> tag of your custom element. This allows you to define the styles that are specific to your custom element and keep them scoped to that element:

<template>
<style>
/* styles for the custom element */
:host {
/* styles for the host element */
}
/* styles for the children element */
</style>
<!-- content of the custom element -->
</template>

Another way to apply styles to a custom element is to link to an external stylesheet file from the <link> tag within the <template> tag of your custom element:

<template>
<link rel="stylesheet" href="path/to/styles.css">
<!-- content of the custom element -->
</template>

You can also add styles to the custom element from the outside.

hello-world {
/* styles for the custom element */
}

Finally, you can use javascript to apply styles to the custom element by manipulating the style property of the element.

const customElement = document.querySelector('hello-world');
customElement.style.background = 'red';
customElement.style.color = 'white';

It’s worth noting that, depending on how you apply the styles, the cascading order of the styles may be different and that would affect the final look of the custom element. You should take this into account while styling your custom elements.