On this page
CSS Pseudo-elements (::before, ::after)
What Are CSS Pseudo-elements?
CSS pseudo-elements let you style a specific part of an element, or insert generated content, without adding extra HTML. The two most popular are ::before and ::after. They are the backbone of modern icon decoration, badges, tooltips, overlays, and many clever UI tricks. In this lesson our focus keyword is CSS pseudo-elements, and you will also learn the trending techniques developers search for: CSS ::before and ::after examples, CSS tooltip without JavaScript, CSS overlay gradient, CSS drop cap, and styling ::marker, ::selection and ::placeholder.
Pseudo-class vs Pseudo-element
A pseudo-class (single colon, such as :hover or :focus) selects an element in a certain state. A pseudo-element (double colon, such as ::before) selects a part of an element or creates a virtual child. The double-colon syntax was introduced in CSS3 to tell them apart. Browsers still accept the single-colon form for the four legacy pseudo-elements (:before, :after, :first-line, :first-letter), but you should always write the double-colon form in new code.
How ::before and ::after Work
::beforecreates a virtual element as the first child of the selected element.::aftercreates a virtual element as the last child of the selected element.- They are inline by default. Set
display: block,inline-blockorposition: absoluteto control layout. - They will not render unless the
contentproperty is set. Even an empty string works:content: '';. - They do not work on replaced elements such as
<img>,<input>,<br>because these have no content box to place children in. - They cannot be selected by JavaScript with
querySelector, but you can read them withgetComputedStyle(el, '::before').
The content Property
The content property accepts several kinds of values:
- A string:
content: 'New'; - An empty string for purely decorative shapes:
content: ''; - An attribute value:
content: attr(data-label); - A counter:
content: counter(item) '. '; - An image:
content: url(icon.svg); - Quote keywords:
open-quoteandclose-quote
Real-world Use Cases
1. Decorative icons and badges
Add a small 'NEW' badge or an arrow icon after a link without touching the HTML markup. This keeps your HTML semantic and clean.
2. Tooltips without JavaScript
Combine attr(data-tip), position: absolute and :hover to display a pure-CSS tooltip. This is one of the most searched CSS tricks.
3. Gradient overlays on images or cards
Use ::after with position: absolute; inset: 0; and a linear-gradient to darken the bottom of a card so white text stays readable.
4. Clearfix
Before Flexbox and Grid, the clearfix hack used ::after with clear: both to make a parent contain floated children. You will still meet it in legacy code.
5. Custom shapes
Triangles, ribbons, speech-bubble tails and decorative lines are all built with empty pseudo-elements and borders or gradients.
Other Useful Pseudo-elements
| Pseudo-element | What it styles |
|---|---|
::first-letter | First letter of a block (drop caps) |
::first-line | First formatted line of a block |
::selection | Text highlighted by the user |
::placeholder | Placeholder text in inputs |
::marker | Bullet or number of a list item |
::backdrop | Background behind a <dialog> or fullscreen element |
::file-selector-button | The button inside input type='file' |
Modern CSS also introduces ::view-transition pseudo-elements for the View Transitions API, and ::highlight() for the CSS Custom Highlight API. These are newer and browser support should be checked before production use.
Accessibility Considerations
- Generated content may be announced by some screen readers, and it cannot be selected or copied. Never place essential information only in
::beforeor::after. - For decorative icons you can provide alternative text using the modern syntax
content: url(icon.svg) / '';which marks the content as decorative in supporting browsers. - Do not rely on pseudo-element text for SEO. Search engines treat it as presentation, not content.
Best Practices
- Use pseudo-elements for decoration, not for meaningful content.
- Set
position: relativeon the parent when the pseudo-element usesposition: absolute. - Use
pointer-events: noneon overlay pseudo-elements so they do not block clicks. - Keep a consistent naming approach and comment complex shape tricks for teammates.
- Prefer
inset: 0over the four-property shorthandtop/right/bottom/leftin modern projects.
Common Mistakes
- Forgetting
content, so nothing appears. - Trying to add
::beforeto an<img>or<input>. - Forgetting
position: relativeon the parent, so the pseudo-element flies to the wrong place. - Placing an overlay on top of a link without
pointer-events: none, making the link unclickable. - Using a single colon for newer pseudo-elements such as
:marker, which is invalid.
Summary
CSS pseudo-elements let you add visual polish without extra markup. Master ::before and ::after, always set content, position them deliberately, and keep them decorative. Try the interactive examples below and edit them live.
Press Run to execute.
Press Run to execute.
Press Run to execute.
Press Run to execute.
Press Run to execute.
Using only CSS, make every label that has the class 'required' show a red asterisk after its text. Use the ::after pseudo-element and the content property. Do not change the HTML.
Press Run to execute.
Show expected output
Each label displays a red * immediately after the text.This is a self-check — compare your result with the expected output above.
Create a small 'SALE' ribbon in the top-right corner of a product box using ::before. The box must have position: relative. The ribbon should have a red background, white bold text, and rounded corners.
Press Run to execute.
Show expected output
A red SALE label appears in the top-right corner of the box.This is a self-check — compare your result with the expected output above.
Was this page helpful?