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>