Day 18: inhertiable styles and web components

We already know that we can encapsulate styles within a web component by adding elements along with the styles to the shadow DOM. Global style declarations from outside don’t overwrite styles inside the web component.
Shadow DOM doesn’t provide total encapsulation, though.

If you look at the following component, you’ll notice that it uses the same font as the rest of the page, even though I haven’t applied any styles to the web component. If styles were completely encapsulated, I would expect the component to use a default font like Times, but the web component inherits styles from its parent elements.

Demo:

HTML:

<shadow-component></shadow-component>

JS:

class ShadowComponent extends HTMLElement {
constructor() {
super();
this.attachShadow({mode: 'open'});
this.shadowRoot.innerHTML = `<div>Hello World!</div>`
}
}

customElements.define('shadow-component', ShadowComponent);


This content originally appeared on Manuel Matuzović - Web development blog and was authored by Manuel Matuzović

We already know that we can encapsulate styles within a web component by adding elements along with the styles to the shadow DOM. Global style declarations from outside don’t overwrite styles inside the web component.
Shadow DOM doesn't provide total encapsulation, though.

If you look at the following component, you’ll notice that it uses the same font as the rest of the page, even though I haven't applied any styles to the web component. If styles were completely encapsulated, I would expect the component to use a default font like Times, but the web component inherits styles from its parent elements.

Demo:

HTML:

<shadow-component></shadow-component>

JS:

class ShadowComponent extends HTMLElement {
constructor() {
super();
this.attachShadow({mode: 'open'});
this.shadowRoot.innerHTML = `<div>Hello World!</div>`
}
}

customElements.define('shadow-component', ShadowComponent);

If I wrap the component in a <div> and a set a color on the div, the color of the text inside the web component changes as well. That's because top-level elements of a shadow tree inherit from their host element. In other words, inhertiable styles will be passed to the shadow DOM.

.parent {
color: red;
}
<div class="parent">
<shadow-component></shadow-component>
</div>

Demo:


This content originally appeared on Manuel Matuzović - Web development blog and was authored by Manuel Matuzović


Print Share Comment Cite Upload Translate Updates
APA

Manuel Matuzović | Sciencx (2022-10-19T09:38:54+00:00) Day 18: inhertiable styles and web components. Retrieved from https://www.scien.cx/2022/10/19/day-18-inhertiable-styles-and-web-components/

MLA
" » Day 18: inhertiable styles and web components." Manuel Matuzović | Sciencx - Wednesday October 19, 2022, https://www.scien.cx/2022/10/19/day-18-inhertiable-styles-and-web-components/
HARVARD
Manuel Matuzović | Sciencx Wednesday October 19, 2022 » Day 18: inhertiable styles and web components., viewed ,<https://www.scien.cx/2022/10/19/day-18-inhertiable-styles-and-web-components/>
VANCOUVER
Manuel Matuzović | Sciencx - » Day 18: inhertiable styles and web components. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/10/19/day-18-inhertiable-styles-and-web-components/
CHICAGO
" » Day 18: inhertiable styles and web components." Manuel Matuzović | Sciencx - Accessed . https://www.scien.cx/2022/10/19/day-18-inhertiable-styles-and-web-components/
IEEE
" » Day 18: inhertiable styles and web components." Manuel Matuzović | Sciencx [Online]. Available: https://www.scien.cx/2022/10/19/day-18-inhertiable-styles-and-web-components/. [Accessed: ]
rf:citation
» Day 18: inhertiable styles and web components | Manuel Matuzović | Sciencx | https://www.scien.cx/2022/10/19/day-18-inhertiable-styles-and-web-components/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.