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

How can you create a tooltip using pseudo-elements?

Creating Tooltips with Pseudo-Elements in CSS

You can create simple tooltips using the ::before or ::after pseudo-elements to display additional information when a user hovers over an element. The pseudo-element contains the tooltip text and is styled to appear above or beside the target element.

Key Points
  1. 1

    Tooltips are often created using ::after with the content property to insert text.

  2. 2

    Use position: absolute for the pseudo-element and position: relative on the parent to control placement.

  3. 3

    Control visibility with opacity, visibility, or transform and trigger it using the parent's :hover pseudo-class.

  4. 4

    Pseudo-elements are ideal for decorative or informational tooltips without extra HTML elements.

Example: Tooltip with ::after

In this example, the tooltip text is displayed above the .tooltip element when the user hovers over it. The ::after pseudo-element is styled with positioning, background, and transition effects to create a smooth tooltip experience.

Best Practices
  1. 1

    Use pseudo-elements for tooltips that are purely informational or decorative.

  2. 2

    Ensure sufficient contrast between tooltip text and background for readability.

  3. 3

    Combine with transitions for smooth appearance and disappearance effects.

  4. 4

    Avoid using pseudo-elements for tooltips requiring interactive content or complex HTML.

Difficulty: 3/10
Topics: pseudo-elements, CSS tooltip implementation, hover states

Scenario Questions

0-2 years experience
  1. 1

    How would you create a simple tooltip that appears when someone hovers over a button using only CSS pseudo-elements?

  2. 2

    What happens if you forget to set position: relative on the button when using ::after for a tooltip?

2-5 years experience
  1. 1

    A tooltip we built with ::before breaks when the page is zoomed — what could be causing it, and how would you debug it?

  2. 2

    We’re using pseudo-elements for tooltips across our app, but they’re overlapping with dropdown menus — how would you fix this without changing the HTML structure?

5-8 years experience
  1. 1

    How would you design a scalable tooltip system using pseudo-elements that works across 50+ components with varying container constraints and responsive breakpoints?

  2. 2

    We’re seeing layout shifts when tooltips appear — how would you optimize for CLS and ensure smooth rendering without JavaScript?

8+ years experience
  1. 1

    Our legacy UI uses inline JS tooltips — how would you architect a migration to CSS pseudo-element tooltips without breaking accessibility or analytics tracking?

  2. 2

    If we need to support RTL, dynamic font scaling, and high-contrast modes in our tooltip system, what architectural decisions would you make to future-proof it across teams?

Follow-up Questions

  • What happens if the tooltip text is too long for the viewport?
  • How would you make this work on touch devices?
  • Why not use aria-describedby instead?