::part() and ::slotted()
Throughout this course, your CSS has had full access to every element on the page. You write a selector, it matches, and the styles apply. That model works because everything lives in the same DOM tree and participates in the same cascade. Web Components change the rules. They introduce a boundary called the Shadow DOM that hides a component's internal markup from outside stylesheets. Your global CSS cannot reach inside, and the component's internal CSS cannot leak out.
That boundary is powerful, but it creates a problem: how do you let the outside world customize parts of a component without breaking encapsulation entirely? CSS provides two pseudo-elements for this: ::part(), which lets a component author expose named pieces for external styling, and ::slotted(), which lets the component's own stylesheet style content that a consumer passes in. This page explains both, starting with the Shadow DOM context you need to understand them.
What Is the Shadow DOM?
The regular DOM you have worked with all semester is sometimes called the light DOM. Every element sits in one tree, every stylesheet can reach every element, and specificity is the only thing controlling which rule wins. The Shadow DOM is a separate, encapsulated tree that attaches to a host element. The markup inside a shadow tree is invisible to document.querySelector(), invisible to your global stylesheets, and rendered as part of the host element without mixing into the rest of the page.
You have already used elements that have a shadow tree without realizing it. A native <input type="range"> slider, a <video> player with its controls, and a <details> disclosure widget all use internal shadow trees to render their UI. The browser builds those internal elements for you, and your CSS cannot style them directly (though some browsers expose vendor-prefixed pseudo-elements for specific pieces).
When you create your own Web Component using customElements.define(), you can attach a shadow tree to it. That shadow tree contains the component's internal HTML (usually stamped from a <template>) and optionally its own <style> block. The component becomes a self-contained unit: its styles do not affect the rest of the page, and your page's styles do not affect its internals.
When attaching a shadow root, you choose mode: 'open' or mode: 'closed'. An open shadow root lets JavaScript access the internal tree via element.shadowRoot, which is what you see in DevTools. A closed root hides even that reference. In practice, most components use open mode because it enables debugging and the ::part() styling you are about to learn. Closed mode is rare and primarily used by browser internals.
Why Encapsulation Matters
At first, a boundary that blocks your CSS might sound like a problem. But consider a team building a design system used by dozens of applications. Without encapsulation, any application's stylesheet could accidentally override the button's internal layout, break its focus ring, or change its icon alignment. The component author has no way to protect the parts that must stay consistent.
The Shadow DOM solves this by making encapsulation the default. The component author decides what is stylable from the outside and what is locked down. That decision is made through two mechanisms: named parts and slots.
::part() – Styling Named Pieces from Outside
A component author can mark any element inside the shadow tree with a part attribute. This exposes that element as a named styling surface without revealing the rest of the shadow tree. The consumer (the person using the component) then targets it with the ::part() pseudo-element.
Inside the component's template, the author marks the elements they want to expose:
<!-- Inside the shadow tree -->
<div part="header">
<h2 part="title">Default Title</h2>
</div>
<div part="body">
<slot></slot>
</div>
From outside the component, the consumer styles those parts by combining the component's tag name with ::part():
/* Outside stylesheet */
my-card::part(header) {
background: var(--color-primary);
color: #fff;
padding: 1rem;
}
my-card::part(title) {
font-size: 1.25rem;
font-family: Georgia, serif;
}
Only elements explicitly marked with part are reachable. Everything else inside the shadow tree remains protected. The component author controls the contract: "you can restyle the header and the title, but the internal layout, the icon placement, and the focus behavior are mine."
A few important constraints to know:
-
You cannot use combinators inside
::part(). Writingmy-card::part(header) h2does not work. Each stylable surface must have its ownpartname. -
An element can have multiple part names separated by spaces:
part="btn primary". You can then target it with::part(btn),::part(primary), or::part(btn primary). - Parts are not inherited. A child of a part is not automatically a part itself. The author must explicitly mark each element they want to expose.
::slotted() – Styling Consumer Content from Inside
While ::part() lets the outside world style pieces of the shadow tree, ::slotted() works in the opposite direction. It lets the component's own internal stylesheet style the content that a consumer passes into a <slot>.
A <slot> is a placeholder inside the shadow tree where consumer-provided content (called slotted content or light DOM children) gets projected. The content itself lives in the light DOM, but it renders in the position the slot defines.
Here is a component template with a named slot:
<!-- Inside the shadow tree -->
<div part="header">
<slot name="heading">Default Heading</slot>
</div>
<div part="body">
<slot>Default body content goes here.</slot>
</div>
A consumer passes content into those slots:
<!-- Light DOM usage -->
<my-card>
<h2 slot="heading">Salt Lake Temple</h2>
<p>Dedicated April 6, 1893, after 40 years of construction.</p>
</my-card>
Inside the component's shadow stylesheet, ::slotted() targets those projected elements:
/* Inside the shadow tree's style block */
::slotted(h2) {
color: var(--color-primary);
margin: 0;
}
::slotted(p) {
line-height: 1.6;
}
The selector inside the parentheses must be a simple selector. You can write ::slotted(p) or ::slotted(.highlight), but you cannot write ::slotted(p span). Only the top-level slotted elements are targetable, not their descendants.
One detail that often trips people up: slotted content still belongs to the light DOM, so it still inherits styles from the outer page's stylesheets. ::slotted() rules from inside the shadow tree and rules from the outer page can both apply to the same element. When they conflict, the outer page's styles win by default because ::slotted() rules are treated as lower specificity in the overall cascade.
Seeing It in DevTools
Open DevTools on any page that uses a Web Component with a shadow root. In the Elements panel, you will see the component's tag name, and nested inside it a #shadow-root (open) label. Expand that to see the internal markup, including any part attributes and <slot> elements. Slotted content appears with a reveal link that jumps to its actual location in the light DOM.
This is essential for your teaching video. Showing your teammates the shadow root in DevTools makes the encapsulation boundary visible and concrete. They can see which elements have part names, which slots exist, and exactly where the light DOM content gets projected.
How This Compares to the Course Approach
The portfolio you are building in this course does not use Web Components or the Shadow DOM. Your CSS lives in an open @layer system with semantic class names, and any stylesheet can reach any element. That is a deliberate choice. Open CSS architectures are simpler to maintain, easier to debug, and accessible to team members who are not JavaScript developers.
Web Components with ::part() and ::slotted() are the right choice in different situations: distributable UI libraries that must work across projects without style conflicts, third-party widgets embedded in pages you do not control, and design system components shared across teams with different codebases. The encapsulation overhead is worth it when the component must be resilient to whatever stylesheet environment it lands in.
Understanding both models makes you a better CSS developer. You know when to reach for the simplicity of open CSS and when encapsulation is worth the tradeoff.
Build Your Demo
Create a new directory in your portfolio at unit-2/part-and-slotted/ with a single index.html file. Build a custom element that demonstrates both ::part() and ::slotted(). Your element should:
-
Use
customElements.define()to register a new tag name -
Attach an open shadow root and stamp a
<template>into it -
Include at least one element marked with a
partattribute -
Include at least one
<slot>(named or default) with fallback content -
Use
::slotted()inside the shadow stylesheet to style projected content -
Use
::part()in the outer page stylesheet to restyle the exposed part
The component can be anything: a card, an alert banner, a profile badge, a testimonial block. Pick something simple enough to finish in a sitting but interesting enough to demonstrate clearly in your video. Add a link to this page from your portfolio homepage.
Resources
- MDN: ::part() – syntax, constraints, and examples
- MDN: ::slotted() – how slotted content styling works
- MDN: Using Shadow DOM – foundational guide to shadow trees, templates, and slots
-
MDN: Using Templates and Slots – how
<template>and<slot>work together - CSS-Tricks: Styling in the Shadow DOM with CSS Shadow Parts – practical walkthrough
Teaching Video Checklist
Your teammates have no experience with the Shadow DOM, so your video needs to start there before getting into the pseudo-elements. They should come away understanding:
- What the Shadow DOM is and why it exists: an encapsulated tree that isolates a component's internal markup and styles from the rest of the page
-
A DevTools walkthrough showing a shadow root, the internal markup,
partattributes, and slotted content projection -
How
::part()works: the component author marks elements withpartnames, and the consumer styles them from outside usingelement-name::part(name) -
How
::slotted()works: the component's internal stylesheet targets light DOM content projected into slots -
A live demo of your custom element showing both pseudo-elements in action, with at least one
::part()override from the outer stylesheet - When this approach makes sense versus the open CSS architecture used in this course