Questions
41 of 45
1What is a pseudo-element in CSS?
2How are pseudo-elements different from pseudo-classes?
3What is the syntax of a pseudo-element?
4Name some commonly used pseudo-elements.
5What does the ::before pseudo-element do? What does the ::after pseudo-element do?
6How can you insert content using pseudo-elements?
7What is the difference between :before and ::before? Why were pseudo-elements changed from one colon (:) to two (::) in CSS3?
8What does the ::first-letter pseudo-element do?
9What does the ::first-line pseudo-element do?
10How can you style the first line of a paragraph differently?
11Can pseudo-elements be styled with animations or transitions?
12What’s the difference between using ::before and ::after?
13Can pseudo-elements be positioned using CSS positioning properties? Can pseudo-elements have background images or colors? How can you use pseudo-elements to create shapes or icons?
14Can you use multiple pseudo-elements on the same element?
15What is the ::selection pseudo-element used for?
16How does stacking and z-index affect pseudo-elements?
17Can pseudo-elements have child elements or content inside them?
18Can you apply pseudo-elements to replaced elements (like <img>)?
19How do pseudo-elements interact with display and overflow properties?
20What’s the difference between using pseudo-elements and real elements for decoration?
21How does inheritance work with pseudo-elements?
22Can pseudo-elements trigger JavaScript events (like click)?
23How can you control the generated content with pseudo-elements using attr()?
24Can pseudo-elements be used inside flex or grid layouts?
25How do you control the stacking order of ::before and ::after pseudo-elements?
26How can you create a tooltip using pseudo-elements?
27How can you make a custom underline or highlight effect using ::after?
28How can you make a quotation mark appear before a paragraph using pseudo-elements?
29How do you create a triangle or arrow shape using only CSS pseudo-elements?
30How can you use pseudo-elements to add icons without extra markup?
31How can you create a button hover animation using ::before or ::after?
32How can pseudo-elements help in implementing skeleton loaders or shimmer effects?
33How can you use ::before and ::after to clear floats?
34How can you create a border animation using pseudo-elements?
35How can pseudo-elements be used in responsive design?
36What is the ::marker pseudo-element and when is it used?
37What does the ::placeholder pseudo-element do?
38How does ::cue work with media elements?
39What is ::backdrop, and where is it used?
40What is the difference between ::file-selector-button and other input pseudo-elements?
41How does the ::part() pseudo-element work in Web Components?
42How is ::slotted() different from ::part() in shadow DOM styling?
43Can you style text highlights with pseudo-elements?
44How can ::selection be customized for accessibility and branding?
45What are the limitations of pseudo-elements compared to actual DOM elements?
41 / 45

How does the ::part() pseudo-element work in Web Components?

Understanding the ::part() Pseudo-Element in CSS

The ::part() pseudo-element allows you to style specific parts of a Web Component's shadow DOM from outside the component. It works in combination with the part attribute inside the component's shadow DOM to expose internal elements for styling.

Key Points
  1. 1

    ::part(name) targets elements inside a shadow DOM that have a part="name" attribute.

  2. 2

    It allows encapsulated components to expose certain internal elements for styling while keeping the rest of the shadow DOM private.

  3. 3

    You can style properties like color, background, padding, border, and more on the exposed part.

  4. 4

    Multiple parts can be targeted by separating names with commas, e.g., ::part(header, footer).

Example: Styling a Web Component Part

In this example, the ::part() pseudo-element is used to style the title and content parts of the <my-card> Web Component. Even though these elements are inside the shadow DOM, they are styled externally without breaking encapsulation.

Best Practices
  1. 1

    Use part attributes inside Web Components to expose only the elements intended for external styling.

  2. 2

    Combine ::part() with CSS variables for flexible theming and styling.

  3. 3

    Avoid exposing unnecessary internal elements to preserve encapsulation.

  4. 4

    Test across browsers, as support for ::part() may vary slightly in older versions.

Difficulty: 6/10
Topics: Shadow DOM styling, CSS custom properties, component theming

Scenario Questions

0-2 years experience
  1. 1

    You're building a custom button component with a shadow DOM, and you want the parent page to change the text color of the button's label. How would you expose and style that label using ::part()?

  2. 2

    If you forget to add the part attribute to an element inside your shadow DOM, what happens when you try to style it with ::part() from outside?

2-5 years experience
  1. 1

    A team member reports that their custom card component's header isn't responding to ::part(header) styles anymore — the component hasn't changed, but the CSS rule was working last week. What would you check first?

  2. 2

    You're building a themable date picker component. You want to let consumers style the selected day and the calendar grid separately. How would you design the part names and what tradeoffs would you consider versus using CSS custom properties?

5-8 years experience
  1. 1

    You're designing a design system with 50+ Web Components that need to support enterprise theming. How would you structure your ::part() API to balance flexibility with maintainability, and what edge cases might break downstream consumers?

  2. 2

    A legacy component uses ::shadow and deep selectors for theming. You're migrating to ::part() but some styles are now broken because the internal structure changed. How do you plan the migration without breaking existing themes?

8+ years experience
  1. 1

    Your company is standardizing on Web Components across 15 product teams. How would you design a governance model for ::part() exposure — what criteria determine whether an internal element gets a part, and how do you handle breaking changes when parts are removed?

  2. 2

    You're evaluating whether to adopt ::part() as the primary theming mechanism for your design system. What long-term maintenance, accessibility, and cross-team adoption risks would you surface to leadership, and how would you mitigate them?

Follow-up Questions

  • What happens if a consumer tries to style a part that wasn't declared in the component?
  • How would you handle theming if you needed to style elements conditionally based on user preferences?
  • Can you use ::part() with CSS custom properties? How does that interact?