Where do you put styles in web components?
I’m assuming that we’re using the Shadow DOM here as, to me, that’s one of the big draws of a web component: a platform thing that is a uniquely powerful thing that only the platform can do. So this is about defining styles for a web component in a don’t-leak-out way, and less so a way to get global styles to leak in (although that’s very interesting as well, which can be done via custom properties which we’ll look at later in the article).
If you’re building the template inside the JavaScript — which is nice because of template literals and how we can sprinkle our data into the template nicely — you need access to those styles in JavaScript.
const template = ` <style>${styles}</style> <div> <h2 id="title">${title}</h2> ${content} </div> `;
Where does that style variable come from? Maybe also a template literal?
const style = ` :host { background: white; } h2 { font: 900 1.5rem/1.1 -system-ui, sans-serif; } `;
I guess that’s fine, but it makes for a big messy block of code just dunked somewhere in the class where you’re trying to build this web component.
Another way is to the template and make a
<template> <style> :host { background: white; } h2 { font: 900 1.5rem/1.1 -system-ui, sans-serif; } </style> <div> <h2></h2> <p></p> </div> </template>
I can see the appeal with this because it keeps HTML in HTML. What I don’t love about it is that you have to do a bunch of manual shadowRoot.querySelector("#title-hook").innerHTML = myData.title; work in order to flesh out that template. That doesn’t feel like a convenient template. I also don’t love that you need to just chuck this template somewhere in your HTML. Where? I dunno. Just chuck it in there. Chuck it.
The CSS is moved out of the JavaScript too, but it just moved from one awkward location to another.
If we wanted to keep the CSS in a CSS file, we can sorta do that like this:
<template> <style> @import "/css/components/card.css"; </style> <div> <h2></h2> <p></p> </div> </template>
(The use of is deprecated, apparently.)
Now we have @import which is an extra HTTP Request, and notorious for being a performance hit. An article by Steven Lambert says it clocked in at half a second slower. Not ideal. I don’t suppose it would be much better to do this instead:
class MyComponent extends HTMLElement { constructor() { super(); this.attachShadow({ mode: "open" }); fetch('/css/components/card.css') .then(response => response.text()) .then(data => { let node = document.createElement('style'); node.innerHTML = data; document.body.appendChild(node); }); } // ... }
Seems like that would potentially be a Flash-of-Unstyled-Web-Component? I guess I should get off my butt and test it.
Now that I’m digging into this again, it seems like ::part has gotten some steam (explainer). So I can do…
const template = ` <div part="card"> <h2 id="title">${title}</h2> ${content} </div> `;
…then write styles in a global stylesheet that only apply inside that Shadow DOM, like:
my-card::part(card) { background: black; color: white; }
…which has a smidge of browser support, but maybe not enough?
These “part” selectors can only touch the exact element it’s connected to. You’d have to do all your styling by applying a part name to every single DOM node and then styling each entirely on its own. That’s no fun, particularly because the appeal of the Shadow DOM is this isolated styling environment in which we’re supposed to be able to write looser CSS selectors and not be worried our h2 { } style is going to leak all over the place.
Looks like if native CSS modules becomes a thing, that will be the most helpful thing that could happen.
import styles from './styles.css'; class MyElement extends HTMLElement { constructor() { this.attachShadow({mode: open}); this.shadowRoot.adoptedStyleSheets = [styles]; } }
I’m not sure, however, if this is any sort of performance boost. Seems like it would be a wash between this and @import. I have to say I prefer the clarity and syntax with native CSS modules. It’s nice to be writing JavaScript when working with JavaScript.
Constructable Stylesheets also look helpful for sharing a stylesheet across multiple components. But the CSS modules approach looks like it could also do that since the stylesheet has already become a variable at that point.
The above is the detailed content of Thinking Through Styling Options for Web Components. For more information, please follow other related articles on the PHP Chinese website!

For a while, iTunes was the big dog in podcasting, so if you linked "Subscribe to Podcast" to like:

We lost Opera when they went Chrome in 2013. Same deal with Edge when it also went Chrome earlier this year. Mike Taylor called these changes a "Decreasingly

From trashy clickbait sites to the most august of publications, share buttons have long been ubiquitous across the web. And yet it is arguable that these

In this week's roundup, Apple gets into web components, how Instagram is insta-loading scripts, and some food for thought for self-hosting critical resources.

When I was looking through the documentation of git commands, I noticed that many of them had an option for . I initially thought that this was just a

Sounds kind of like a hard problem doesn't it? We often don't have product shots in thousands of colors, such that we can flip out the with . Nor do we

I like when websites have a dark mode option. Dark mode makes web pages easier for me to read and helps my eyes feel more relaxed. Many websites, including

This is me looking at the HTML element for the first time. I've been aware of it for a while, but haven't taken it for a spin yet. It has some pretty cool and


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version
Visual web development tools

Notepad++7.3.1
Easy-to-use and free code editor