CSS Pseudo-elements
CSS Pseudo-elements are virtual elements that allow styling specific parts of an element, such as ::before, ::after, ::first-line, or ::first-letter, enabling content insertion and granular styling control.
What are ::before and ::after in CSS?
::before and ::after are pseudo-elements that insert a generated box as the first or last child of an element. They need a content property (even content: "") to appear, and they're used for decorative icons, quotes, overlays and shapes without adding extra HTML.
.required::after {
content: " *";
color: red;
}
.card { position: relative; }
.card::before { /* decorative overlay */
content: "";
position: absolute;
inset: 0;
background: linear-gradient(transparent, rgb(0 0 0 / .6));
}Interactive Pseudo-elements Demo
Code Editor
<p class="required">Email</p>
<blockquote>Simple is better than complex.</blockquote>
<p class="intro">The first letter of this paragraph is styled with ::first-letter.</p>
<input placeholder="Styled placeholder">Preview
::before and ::after Elements
Basic ::before and ::after
/* Basic pseudo-element structure */
.element::before {
content: ""; /* Required property */
display: block; /* Or inline-block */
width: 20px;
height: 20px;
background: #3b82f6;
}
.element::after {
content: "→"; /* Text content */
color: #10b981;
margin-left: 0.5rem;
}
/* Content with attribute values */
.tooltip::before {
content: attr(data-tooltip);
}The content property is required for pseudo-elements to appear.
Decorative Icons
/* Add icons with Unicode */
.success::before {
content: "✓";
color: #10b981;
font-weight: bold;
margin-right: 0.5rem;
}
.warning::before {
content: "⚠";
color: #f59e0b;
margin-right: 0.5rem;
}
.external-link::after {
content: "↗";
font-size: 0.8em;
vertical-align: super;
}Add visual indicators without HTML changes
Geometric Shapes
/* Triangle arrow */
.arrow::after {
content: "";
position: absolute;
border: 10px solid transparent;
border-left: 10px solid #3b82f6;
right: -20px;
top: 50%;
transform: translateY(-50%);
}
/* Circle bullet */
.bullet::before {
content: "";
display: inline-block;
width: 8px;
height: 8px;
background: #ef4444;
border-radius: 50%;
margin-right: 0.5rem;
}Create shapes using borders and backgrounds
Content-based Pseudo-elements
::first-letter and ::first-line
/* Drop cap styling */
p::first-letter {
font-size: 3em;
font-weight: bold;
color: #dc2626;
float: left;
line-height: 1;
margin: 0.1em 0.1em 0 0;
text-shadow: 2px 2px 4px rgba(0,0,0,0.2);
}
/* First line styling */
.intro::first-line {
font-weight: bold;
color: #1f2937;
text-transform: uppercase;
letter-spacing: 1px;
}
/* Newspaper-style paragraph */
.article p:first-child::first-letter {
font-family: Georgia, serif;
background: #3b82f6;
color: white;
padding: 0.2em;
}Style the first letter or line of text for enhanced typography.
::selection Styling
/* Custom text selection */
::selection {
background: #fbbf24;
color: #1f2937;
}
/* Firefox compatibility */
::-moz-selection {
background: #fbbf24;
color: #1f2937;
}
/* Element-specific selection */
.code-block::selection {
background: #1e40af;
color: #e0e7ff;
}
.highlight::selection {
background: #dc2626;
color: white;
}Customize text selection appearance
::placeholder Styling
/* Placeholder styling */
input::placeholder {
color: #9ca3af;
font-style: italic;
opacity: 0.7;
}
/* Different browser prefixes */
input::-webkit-input-placeholder {
color: #9ca3af;
}
input::-moz-placeholder {
color: #9ca3af;
}
/* Focus state */
input:focus::placeholder {
opacity: 0.3;
transition: opacity 0.2s;
}Style form input placeholders
Advanced Pseudo-element Techniques
CSS-only Tooltips
.tooltip {
position: relative;
display: inline-block;
}
.tooltip::before {
content: attr(data-tooltip);
position: absolute;
bottom: 100%;
left: 50%;
transform: translateX(-50%);
background: #1f2937;
color: white;
padding: 0.5rem;
border-radius: 4px;
font-size: 0.875rem;
white-space: nowrap;
opacity: 0;
pointer-events: none;
transition: opacity 0.3s;
margin-bottom: 0.5rem;
}
.tooltip::after {
content: "";
position: absolute;
bottom: 100%;
left: 50%;
transform: translateX(-50%);
border: 6px solid transparent;
border-top-color: #1f2937;
opacity: 0;
transition: opacity 0.3s;
}
.tooltip:hover::before,
.tooltip:hover::after {
opacity: 1;
}Create interactive tooltips without JavaScript using data attributes.
Content Overlays and Decorations
/* Ribbon decoration */
.ribbon {
position: relative;
background: #f59e0b;
color: white;
padding: 0.5rem 1rem;
font-weight: bold;
}
.ribbon::before,
.ribbon::after {
content: "";
position: absolute;
top: 0;
border: 20px solid #f59e0b;
}
.ribbon::before {
left: -10px;
border-left: 10px solid transparent;
}
.ribbon::after {
right: -10px;
border-right: 10px solid transparent;
}
/* Folded corner effect */
.folded-corner::before {
content: "";
position: absolute;
top: 0;
right: 0;
border: 15px solid transparent;
border-top: 15px solid #f3f4f6;
border-right: 15px solid #f3f4f6;
}Create decorative elements like ribbons and folded corners.
Best Practices & Browser Support
Best Practices
- • Always include the content property
- • Use double colons (::) for modern syntax
- • Position pseudo-elements relatively or absolutely
- • Consider semantic meaning over decoration
- • Test across different browsers
- • Use sparingly to avoid performance issues
Creative Applications
- • CSS-only modals and overlays
- • Icon replacement for text
- • Decorative quotation marks
- • Loading animations and spinners
- • Speech bubbles and callouts
- • Progress bars and indicators
CSS Pseudo-elements — step by step
::before and ::after
Insert generated content before or after an element's actual content using ::before and ::after with a content property — essential for decorative effects.
🎯 Quick Check
What CSS property is REQUIRED for ::before and ::after to appear?
CSS Pseudo-elements: summary
Key points
- Without
content,::beforeand::afterdon't render. - They don't work on replaced elements with no content, such as
img,inputandbr. - Other useful pseudo-elements:
::placeholder,::selection,::marker,::first-line,::first-letter. - Generated text isn't reliably read by screen readers and can't be selected, so keep real content in the HTML.
Frequently asked questions
›Why is my ::before not showing?
The most common cause is a missing content property; add content: "". Also check that the element isn't an img or input, and that the pseudo-element has a size or display value if it's empty.
›Should I use one colon or two for ::before?
Use two colons (::before). One colon still works for the four old pseudo-elements, but the double colon is the modern standard.