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

What’s the difference between using ::before and ::after?

Understanding the Difference Between ::before and ::after Pseudo-Elements

The ::before and ::after pseudo-elements in CSS are used to insert content before or after an element's actual content. The main difference lies in their placement relative to the element's content.

Key Differences
  1. 1

    ::before inserts content immediately before the element's original content.

  2. 2

    ::after inserts content immediately after the element's original content.

  3. 3

    Both require the content property to display anything.

  4. 4

    They are commonly used for decorative purposes, icons, or adding extra styling cues without modifying the HTML structure.

Example: Using ::before and ::after

In this example, ::before adds a star before the paragraph text, while ::after adds sparkles after the text, demonstrating the placement difference between the two pseudo-elements.

Best Practices
  1. 1

    Use ::before and ::after for decorative content or visual cues without altering HTML.

  2. 2

    Always include the content property; otherwise, nothing will be displayed.

  3. 3

    Combine with CSS properties like color, font-size, background, or transform for visual effects.

  4. 4

    Ensure compatibility and test across browsers for consistent rendering.

Difficulty: 3/10
Topics: pseudo-elements, content insertion, CSS rendering order

Scenario Questions

0-2 years experience
  1. 1

    How would you add a star icon before every review rating using CSS, and what’s the minimum CSS you need to make it show up?

  2. 2

    What happens if you try to use ::before on an <img> tag? Why doesn’t it work?

  3. 3

    You’re trying to add a tooltip arrow using ::after, but it’s not showing up—what are the two most likely reasons?

2-5 years experience
  1. 1

    A designer says the ‘read more’ link’s arrow is misaligned—your ::after arrow is rendering below the text instead of inline. How do you debug and fix it?

  2. 2

    You’re using ::before for a bullet in a list, but it breaks when the list item wraps on mobile. What’s causing it, and how do you fix it without changing HTML?

  3. 3

    A teammate used ::before to add a decorative border, but now the layout shifts unexpectedly on Safari. What’s the likely culprit and how do you verify it?

5-8 years experience
  1. 1

    You’re building a reusable card component that uses ::before and ::after for visual accents, but in some contexts, they’re being clipped by overflow:hidden. How do you design this to be robust across parent containers?

  2. 2

    In a high-traffic UI, you notice layout thrashing when toggling pseudo-elements via JS. Why is this happening, and what’s a better approach to achieve the same visual effect without performance cost?

  3. 3

    You inherited a codebase where ::before is used for semantic icons (like a lock for private posts). Is this a good practice? What accessibility and maintainability issues does this introduce?

8+ years experience
  1. 1

    You’re leading a design system migration from icon fonts to SVGs. Many components used ::before with font icons—how do you plan the transition without breaking legacy layouts or accessibility?

  2. 2

    Your team uses ::after for visual indicators (e.g., ‘new’, ‘hot’) across 50+ components. How do you architect this to be scalable, themeable, and maintainable across multiple product lines?

  3. 3

    A legacy app uses ::before and ::after for layout hacks (e.g., clearfixes, spacing). You’re refactoring to CSS Grid. How do you prioritize removal, communicate risk to stakeholders, and avoid regressions?

Follow-up Questions

  • What happens if you forget the content property?
  • Can you style ::before and ::after differently if they have the same content?
  • Why might one of them not appear even when the CSS looks correct?