Questions
18 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?
18 / 45

Can you apply pseudo-elements to replaced elements (like <img>)?

Pseudo-Elements and Replaced Elements in CSS

Pseudo-elements like ::before and ::after cannot be applied to replaced elements, such as <img>, <input>, <textarea>, <video>, or <iframe>. Replaced elements have intrinsic content defined by the browser or an external source, and pseudo-elements can only insert generated content into non-replaced elements.

Key Points
  1. 1

    Replaced elements have content that is not part of the HTML DOM structure (e.g., the image file itself).

  2. 2

    Pseudo-elements like ::before and ::after only work on elements that can contain text or other non-replaced content.

  3. 3

    Applying pseudo-elements to replaced elements will not render any visible output.

  4. 4

    For decorative effects on replaced elements, consider wrapping them in a <div> or <span> and applying pseudo-elements to the wrapper.

Example: Using Pseudo-Elements on a Wrapper Instead of an Image

In this example, the ::before pseudo-element is applied to the wrapper <div> around the image, allowing you to add decorative overlays or effects without directly targeting the <img> element.

Best Practices
  1. 1

    Do not apply ::before or ::after directly to replaced elements; it will not work.

  2. 2

    Use a wrapper element if you need pseudo-element-based decorations or overlays.

  3. 3

    Style the pseudo-elements with position, z-index, and other visual properties to achieve the desired effect.

  4. 4

    Test across browsers, as rendering behavior may differ slightly for replaced elements.

Difficulty: 6/10
Topics: pseudo-elements, replaced elements, CSS rendering

Scenario Questions

0-2 years experience
  1. 1

    I tried to add a ::before pseudo-element to an <img> tag to show a badge icon, but nothing appears — what’s going on?

  2. 2

    If I set content: '★' on img::after in CSS, will it show up next to the image? Why or why not?

  3. 3

    How would you verify whether a pseudo-element is being applied to an <img> element using browser dev tools?

2-5 years experience
  1. 1

    Our design team wants a decorative border around product images using ::before, but it’s not rendering — how do you debug this and what’s your workaround?

  2. 2

    A teammate added a ::after element to an <img> to display a 'New' label, but it’s invisible in Chrome — what’s the root cause and how do you fix it without changing the HTML?

  3. 3

    Why does this CSS rule work on a <div> but not on an <img>, even though both have the same class? Walk me through your debugging process.

5-8 years experience
  1. 1

    We’re building a responsive image gallery with overlay badges — since pseudo-elements don’t work on <img>, how would you design a scalable, accessible solution that doesn’t bloat the DOM?

  2. 2

    In a legacy component library, we have hundreds of images styled with ::before for icons — migrating to a new framework requires avoiding this pattern. What’s your migration strategy and how do you measure impact?

  3. 3

    How would you architect a CSS system that allows for image overlays without relying on pseudo-elements, while maintaining performance and supporting screen readers?

8+ years experience
  1. 1

    We’re standardizing our design system across 10+ products, and some teams rely on img::before for UI badges — how do you drive architectural change without breaking existing features or overwhelming teams?

  2. 2

    How would you evaluate the long-term maintainability and accessibility tradeoffs of using wrapper <span> elements instead of pseudo-elements on images at scale?

  3. 3

    If a major browser changed its behavior to allow pseudo-elements on replaced elements, what ripple effects would that have on our CSS architecture, testing strategy, and third-party component vendors?

Follow-up Questions

  • What happens if you try to style an img with ::before using developer tools?
  • How would you work around this limitation if you needed to overlay text on an image?
  • Have you ever seen this behavior cause a production bug?