::slotted(): Style Content Passed into Web Components

The ::slotted() pseudo-element lets a Web Component style external content passed into its slots. It's how a component's internal CSS can style the "light DOM" elements it receives.
Why it exists
Web Components encapsulate their structure and styles in a shadow DOM, which is normally a black box. But components often need to render content provided by the user via slots. ::slotted() was created to bridge this gap, allowing the component author to style that user-provided content without breaking encapsulation.
The mental model
Think of ::slotted() as a component's way of styling its inputs. When you pass an element into a component's <slot>, that element lives in the "light DOM" but is rendered by the component. ::slotted() is the CSS selector that lets the component's internal stylesheet (in the shadow DOM) target and style that specific light DOM element. It's a controlled way to cross the shadow DOM boundary for styling purposes.
How it works
You use ::slotted() inside a <style> tag within a component's shadow DOM. The selector takes an argument that specifies which slotted elements to target. For example, ::slotted(p.content) selects any element with a class of 'content' that has been placed into any <slot> in that component's template. The styles are then applied to that element as it's rendered. It only works on elements, not on text nodes directly placed in a slot.
When to use it
Use ::slotted() when you are building a Web Component and need to enforce a consistent look and feel for the content that users will provide. It's perfect for styling slotted headings, paragraphs, or icons to match the component's theme, without forcing the user to know the internal class names.
When not to use it
Do not try to use ::slotted() in a global stylesheet; it has no effect outside of a shadow DOM. Avoid it if you want the user to have complete control over the styling of the content they provide. Also, remember it doesn't work for text nodes, so if you need to style text, make sure it's wrapped in an element like a .
One canonical example
A <custom-card> component defines a shadow DOM with a slot: <slot name="caption"></slot>. Inside its shadow CSS, it has ::slotted([slot="caption"]) { font-weight: bold; }. When a user writes <custom-card><h2 slot="caption">My Title</h2></custom-card>, the <h2> is rendered in bold, styled by the component itself, even though the <h2> was defined outside the component.
Interview question
What is the primary function of the ::slotted() pseudo-element when used within a Web Component's shadow DOM stylesheet?
- a.To apply styles from the component's internal stylesheet to content provided by the user via slots.Correct
- b.To style elements that are part of the component's own internal shadow DOM structure.
- c.To define fallback content and default styles for a slot when no content is provided.
- d.To enable global CSS rules to directly affect elements inside the component's shadow DOM.
Why? this is the answer
The ::slotted() pseudo-element allows a Web Component's internal stylesheet (in the shadow DOM) to target and style 'light DOM' elements that are passed into its slots. Option B is incorrect because ::slotted() specifically styles content from the light DOM, not the component's internal shadow DOM elements.
Just read this? Test yourself on what you have been reading.
Read the original → developer.mozilla.org
- #css
- #web components
- #shadow dom
- #selectors
You just looked this up. Could you explain it out loud?
That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on css — each one lists the topics its interview covers.
See open roles