agyanadda@gmail.com

CSS Pseudo-elements

Advanced CSS 1 min read Updated Dec 1, 2026 Topic 31 of 39

A pseudo-element styles a part of an element, or adds content. It starts with two colons ::.

Pseudo-element Use
::before add content before the element
::after add content after the element
::first-letter style the first letter
::first-line style the first line
::placeholder style input placeholder text
::selection style text the user selects

::before and ::after need the content property, even if empty: content: "";.

Example

Example — ::before, ::after and ::first-letter HTML
<style>
  .tip::before { content: "💡 "; }
  .required::after { content: " *"; color: crimson; }
  .story::first-letter { font-size: 2.5em; color: #1f386b; float: left; line-height: 1; margin-right: 6px; }
  ::selection { background: #ffd43b; }
</style>

<p class="tip">Use DevTools to inspect CSS.</p>
<p class="required">Email</p>
<p class="story">Once upon a time, a developer learned CSS and built beautiful websites. Select this text to see the custom highlight.</p>

Test Yourself With Exercises

Exercise 1 Multiple Choice

Which property is required for ::before to appear?

Exercise 2 Fill in the Blank

Pseudo-elements are written with ___ colons.

Exercise 3 True / False

::placeholder styles the hint text inside an input.

Frequently Asked Questions

No. Images and inputs have no inner content, so ::before and ::after do not work on them.