{"id":383422,"date":"2025-03-14T06:51:59","date_gmt":"2025-03-14T12:51:59","guid":{"rendered":"https:\/\/css-tricks.com\/?p=383422"},"modified":"2025-03-14T06:52:02","modified_gmt":"2025-03-14T12:52:02","slug":"web-components-demystified","status":"publish","type":"post","link":"https:\/\/css-tricks.com\/web-components-demystified\/","title":{"rendered":"Web Components Demystified"},"content":{"rendered":"\n

Scott Jehl released a course called Web Components Demystified<\/a><\/em>. I love that name because it says what the course is about right on the tin: you’re going to learn about web components and clear up any confusion you may already have about them.<\/p>\n\n\n\n

And there’s plenty of confusion to go around! “Components” is already a loaded term that’s come to mean everything from a piece of UI, like a search component, to an element you can drop in and reuse anywhere, such as a React component. The web is chock-full of components, tell you what.<\/p>\n\n\n\n

But what we’re talking about here is a set of standards where HTML, CSS, and JavaScript rally together so that we can create custom elements that behave exactly how we want them to. It’s how we can make an element called <tasty-pizza><\/code> and the browser knows what to do with it.<\/p>\n\n\n\n

This is my full set of notes from Scott’s course. I wouldn’t say they’re complete or even a direct one-to-one replacement for watching the course. You’ll still want to do that on your own, and I encourage you to because Scott is an excellent teacher who makes all of this stuff extremely accessible, even to noobs like me.<\/p>\n\n\n\n\n\n\n\n

\n \n

Chapter 1: What Web Components Are… and Aren’t<\/h2>\n <\/summary>\n \n\n

Web components are not built-in elements, even though that’s what they might look like at first glance. Rather, they are a set of technologies that allow us to instruct what the element is and how it behaves. Think of it the same way that “responsive web design” is not a thing but rather a set of strategies for adapting design to different web contexts. So, just as responsive web design is a set of ingredients<\/a> \u2014 including media fluid grids, flexible images, and media queries \u2014 web components are a concoction involving:<\/p>\n\n\n\n

Custom elements<\/summary>\n

These are HTML elements that are not built into the browser. We make them up. They include a letter and a dash.<\/p>\n\n\n\n

<my-fancy-heading>\n  Hey, I'm Fancy\n<\/my-fancy-heading><\/code><\/pre>\n\n\n\n

We’ll go over these in greater detail in the next module.<\/p>\n<\/details>\n\n\n\n

HTML templates<\/summary>\n

Templates are bits of reusable markup that generate more markup. We can hide something until we make use of it. <\/p>\n\n\n\n

<template>\n  <li class=\"user\">\n    <h2 class=\"name\"><\/h2>\n    <p class=\"bio\"><\/p>\n  <\/li>\n<\/template><\/code><\/pre>\n\n\n\n

Much more on this in the third module.<\/p>\n<\/details>\n\n\n\n

Shadow DOM<\/summary>\n

The DOM is queryable.<\/p>\n\n\n\n

document.querySelector(\"h1\");\n\/\/ <h1>Hello, World<\/h1><\/code><\/pre>\n\n\n\n

The Shadow DOM is a fragment of the DOM where markup, scripts, and styles are encapsulated from other DOM elements. We’ll cover this in the fourth module, including how to <slot><\/code> content.<\/p>\n<\/details>\n\n\n\n

There used to be a fourth “ingredient” called HTML Imports, but those have been nixed.<\/p>\n\n\n\n

In short, web components might be called “components” but they aren\u2019t really components more than technologies. In React, components sort of work like partials. It defines a snippet of HTML that you drop into your code and it outputs in the DOM. Web Components are built off of HTML Elements. They are not replaced when rendered the way they are in JavaScript component frameworks. Web components are quite literally HTML elements and have to obey HTML rules. For example:<\/p>\n\n\n\n

<!-- Nope -->\n<ul>\n  <my-list-item><\/my-list-item>\n  <!-- etc. -->\n<\/ul>\n\n<!-- Yep -->\n<ul>\n  <li>\n    <my-list-item><\/my-list-item>\n  <\/li>\n<\/ul><\/code><\/pre>\n\n\n\n

We\u2019re generating meaningful HTML up-front rather than rendering it in the browser through the client after the fact. Provide the markup and enhance it! Web components have been around a while now, even if it seems we\u2019re only starting to talk about them now.<\/p>\n\n\n<\/details>\n\n\n

\n \n

Chapter 2: Custom Elements<\/h2>\n <\/summary>\n \n\n

First off, custom elements are not built-in HTML elements. We instruct what they are and how they behave. They are named with a dash and at must contain least one letter. All of the following are valid names for custom elements:<\/p>\n\n\n\n