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

How can you insert content using pseudo-elements?

Inserting Content with ::before and ::after Pseudo-Elements

You can insert content before or after an element using the ::before and ::after pseudo-elements in CSS. These pseudo-elements allow adding decorative or informational content without changing the HTML.

Key Points
  1. 1

    Use ::before to insert content before an element's existing content.

  2. 2

    Use ::after to insert content after an element's existing content.

  3. 3

    The content property is required to display any inserted content.

  4. 4

    Pseudo-elements are useful for decorative purposes, icons, counters, or styling enhancements.

Example: Adding Content Using ::before and ::after

In this example, ::before inserts a star before the paragraph text, and ::after inserts sparkles after the text, without modifying the HTML structure.

Best Practices
  1. 1

    Always include the content property when using ::before or ::after.

  2. 2

    Use pseudo-elements for decoration or stylistic purposes rather than essential content.

  3. 3

    Combine with CSS properties like color, font-size, background, and margin for better visual effects.

  4. 4

    Check browser compatibility to ensure consistent rendering.

Difficulty: 3/10
Topics: pseudo-element syntax, content insertion, visual decoration

Scenario Questions

0-2 years experience
  1. 1

    How would you add a star icon after every review rating using only CSS, without changing the HTML?

  2. 2

    What happens if you set content: '' on a ::before pseudo-element but don't define width or height?

  3. 3

    You're trying to insert a tooltip with ::after, but it's not showing up — what are the top three things you'd check?

2-5 years experience
  1. 1

    A designer wants a decorative divider between list items using ::after, but it's overlapping with the next item on mobile — how do you fix it without adding extra markup?

  2. 2

    We added a loading spinner using ::before on a button, but now screen readers announce the content — how do you make it accessible?

  3. 3

    Our pseudo-element tooltip works in Chrome but disappears in Safari — what could be causing this and how would you debug it?

5-8 years experience
  1. 1

    You're building a reusable card component that uses pseudo-elements for badges and borders — how do you ensure they don't interfere with internationalization or dynamic content length?

  2. 2

    A performance audit shows layout thrashing on a page with 50+ components using ::before for icons — what alternatives would you consider and why?

  3. 3

    How would you design a theming system where pseudo-element colors and content change based on user preferences, without duplicating CSS rules?

8+ years experience
  1. 1

    We're migrating a legacy UI that relies heavily on pseudo-elements for core functionality — how do you assess the technical debt and plan a transition to semantic HTML without breaking accessibility or design consistency?

  2. 2

    How would you architect a design system where pseudo-element usage is standardized across 10+ teams, ensuring maintainability, performance, and compliance with WCAG?

  3. 3

    A cross-team component library uses ::after for visual indicators, but now we need to support dynamic RTL layouts and dark mode — what architectural decisions would you make to future-proof this?

Follow-up Questions

  • What happens if you try to use ::before on a <span> that has display: inline?
  • Why can't you interact with pseudo-elements using JavaScript?
  • How would you make sure your pseudo-element doesn't break in a high-contrast mode?