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.
Use ::before to insert content before an element's existing content.
Use ::after to insert content after an element's existing content.
The content property is required to display any inserted content.
Pseudo-elements are useful for decorative purposes, icons, counters, or styling enhancements.
In this example, ::before inserts a star before the paragraph text, and ::after inserts sparkles after the text, without modifying the HTML structure.
Always include the content property when using ::before or ::after.
Use pseudo-elements for decoration or stylistic purposes rather than essential content.
Combine with CSS properties like color, font-size, background, and margin for better visual effects.
Check browser compatibility to ensure consistent rendering.
How would you add a star icon after every review rating using only CSS, without changing the HTML?
What happens if you set content: '' on a ::before pseudo-element but don't define width or height?
You're trying to insert a tooltip with ::after, but it's not showing up — what are the top three things you'd check?
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?
We added a loading spinner using ::before on a button, but now screen readers announce the content — how do you make it accessible?
Our pseudo-element tooltip works in Chrome but disappears in Safari — what could be causing this and how would you debug it?
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?
A performance audit shows layout thrashing on a page with 50+ components using ::before for icons — what alternatives would you consider and why?
How would you design a theming system where pseudo-element colors and content change based on user preferences, without duplicating CSS rules?
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?
How would you architect a design system where pseudo-element usage is standardized across 10+ teams, ensuring maintainability, performance, and compliance with WCAG?
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?