Styling Web Component shadow DOM elements with an inline link element

Picking up on this week’s articles on why styling the shadow DOM sucks, today I wanted to talk about using an inline <link> element.
Earlier this week, I shared an approach using an inline <style> element. My buddy Scott Jehl pointed out that you can also use a <link> and point to an external stylesheet.
// Inject the HTML into the shadow DOM this.root.innerHTML = `<link rel=”stylesheet” type=”text/css” href=”/path/to/buttons.css”> <button>Clicked ${this.


This content originally appeared on Go Make Things and was authored by Go Make Things

Picking up on this week’s articles on why styling the shadow DOM sucks, today I wanted to talk about using an inline <link> element.

Earlier this week, I shared an approach using an inline <style> element. My buddy Scott Jehl pointed out that you can also use a <link> and point to an external stylesheet.

// Inject the HTML into the shadow DOM
this.root.innerHTML = 
	`<link rel="stylesheet" type="text/css" href="/path/to/buttons.css">
	<button>Clicked ${this.count} Times</button>`;

This let’s you avoid repeating the same CSS in a global stylesheet and your inline code.

It probably also works best if you have a modularized CSS setup.


This content originally appeared on Go Make Things and was authored by Go Make Things


Print Share Comment Cite Upload Translate Updates
APA

Go Make Things | Sciencx (2024-11-08T14:30:00+00:00) Styling Web Component shadow DOM elements with an inline link element. Retrieved from https://www.scien.cx/2024/11/08/styling-web-component-shadow-dom-elements-with-an-inline-link-element/

MLA
" » Styling Web Component shadow DOM elements with an inline link element." Go Make Things | Sciencx - Friday November 8, 2024, https://www.scien.cx/2024/11/08/styling-web-component-shadow-dom-elements-with-an-inline-link-element/
HARVARD
Go Make Things | Sciencx Friday November 8, 2024 » Styling Web Component shadow DOM elements with an inline link element., viewed ,<https://www.scien.cx/2024/11/08/styling-web-component-shadow-dom-elements-with-an-inline-link-element/>
VANCOUVER
Go Make Things | Sciencx - » Styling Web Component shadow DOM elements with an inline link element. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/11/08/styling-web-component-shadow-dom-elements-with-an-inline-link-element/
CHICAGO
" » Styling Web Component shadow DOM elements with an inline link element." Go Make Things | Sciencx - Accessed . https://www.scien.cx/2024/11/08/styling-web-component-shadow-dom-elements-with-an-inline-link-element/
IEEE
" » Styling Web Component shadow DOM elements with an inline link element." Go Make Things | Sciencx [Online]. Available: https://www.scien.cx/2024/11/08/styling-web-component-shadow-dom-elements-with-an-inline-link-element/. [Accessed: ]
rf:citation
» Styling Web Component shadow DOM elements with an inline link element | Go Make Things | Sciencx | https://www.scien.cx/2024/11/08/styling-web-component-shadow-dom-elements-with-an-inline-link-element/ |

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.