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

How can you use pseudo-elements to add icons without extra markup?

Adding Icons with Pseudo-Elements in CSS

Pseudo-elements like ::before and ::after can be used to insert icons or decorative symbols without adding extra HTML elements. This is especially useful when using icon fonts or Unicode characters.

Key Points
  1. 1

    Use content in combination with Unicode characters or icon fonts to display icons.

  2. 2

    Pseudo-elements do not exist in the DOM, so they are purely visual and keep HTML clean.

  3. 3

    You can style these icons with CSS properties like color, font-size, margin, and position.

  4. 4

    Perfect for adding decorative symbols, social media icons, or arrows without extra markup.

Example: Adding a Star Icon Using ::before

In this example, the ::before pseudo-element inserts a gold star before the paragraph text. No additional HTML elements are needed to display the icon.

Example: Using Icon Font with ::after

Here, the ::after pseudo-element adds a Font Awesome user icon to the button without modifying the HTML. You can control its position, size, and color entirely with CSS.

Best Practices
  1. 1

    Use pseudo-elements for decorative or non-interactive icons to reduce HTML clutter.

  2. 2

    Ensure accessibility by not relying solely on pseudo-elements for conveying critical information.

  3. 3

    Combine with position, margin, and font-size to place icons precisely.

  4. 4

    Test across browsers to ensure correct rendering of icon fonts or Unicode symbols.

Difficulty: 6/10
Topics: pseudo-elements, icon rendering, accessibility

Scenario Questions

0-2 years experience
  1. 1

    How would you add a star icon after a rating number using only CSS, without adding any extra HTML elements?

  2. 2

    What happens if you forget to set content: '' on a pseudo-element when trying to display an icon?

2-5 years experience
  1. 1

    A team member added icons using pseudo-elements, but now screen readers are announcing the unicode characters — how would you debug and fix this?

  2. 2

    We’re using icon fonts via pseudo-elements, but the icons disappear on mobile Safari — what could be causing this and how would you investigate?

5-8 years experience
  1. 1

    You're designing a reusable button component that supports icons via pseudo-elements — how do you handle dynamic icon content, accessibility, and performance across 50+ variants?

  2. 2

    In a large design system, pseudo-elements are used for icons everywhere — what are the scalability risks, and how would you mitigate them without forcing a full rewrite?

8+ years experience
  1. 1

    Our legacy UI uses pseudo-elements for icons across 200+ components, but we’re migrating to a new design system with SVG sprites — how would you plan and execute this transition without breaking accessibility or causing layout shifts?

  2. 2

    You’re advising a company that uses pseudo-element icons for performance reasons, but now they’re getting complaints about inconsistent rendering across browsers — how do you balance performance, maintainability, and cross-platform reliability at the architecture level?

Follow-up Questions

  • What happens if the icon font fails to load?
  • How would you test this in a screen reader?
  • Why not just use an <img> tag instead?