A pseudo-element is a virtual element created by CSS that allows styling specific parts of an element:
/* Main pseudo-elements */
.element::before { content: "★"; }
.element::after { content: ""; }
p::first-line { font-weight: bold; }
p::first-letter { font-size: 2em; }Pseudo-elements are like invisible helpers that create additional elements where they don’t exist in HTML! They allow adding decorative elements without changing the markup. 🎭
/* Modern syntax (double colon) */
element::pseudo-element {
property: value;
}
/* Legacy syntax (single colon) */
element:pseudo-element {
property: value;
}Most popular pseudo-elements for creating decorative elements:
.quote::before {
content: """;
font-size: 2em;
color: #666;
}
.quote::after {
content: """;
font-size: 2em;
color: #666;
}
/* Icons */
.icon::before {
content: "★";
color: gold;
margin-right: 5px;
}
/* Decorative elements */
.button::after {
content: "";
position: absolute;
width: 100%;
height: 2px;
background: blue;
bottom: 0;
left: 0;
}<p class="quote">This is a quote with quotation marks</p>
<button class="icon">Favorite</button>
<button class="button">Button with underline</button>Styles the first line of text:
p::first-line {
font-weight: bold;
color: #333;
text-transform: uppercase;
}
.article::first-line {
font-size: 1.2em;
letter-spacing: 1px;
}Creates drop cap effect:
p::first-letter {
font-size: 3em;
float: left;
line-height: 1;
margin: 0 8px 0 0;
color: #c00;
}
.drop-cap::first-letter {
font-family: serif;
font-weight: bold;
background: #f0f0f0;
padding: 5px;
}Styles text selected by user:
::selection {
background: #007bff;
color: white;
}
p::selection {
background: yellow;
color: black;
}
.special::selection {
background: linear-gradient(45deg, red, blue);
color: white;
}Styles placeholder in input fields:
input::placeholder {
color: #999;
font-style: italic;
opacity: 0.7;
}
textarea::placeholder {
color: #666;
font-size: 14px;
line-height: 1.4;
}<input type="text" placeholder="Enter your name">
<textarea placeholder="Write a message..."></textarea>Styles list markers:
li::marker {
color: red;
font-size: 1.2em;
}
.custom-list li::marker {
content: "→ ";
color: blue;
}
ol li::marker {
font-weight: bold;
color: green;
}.btn {
position: relative;
padding: 10px 20px;
background: #007bff;
color: white;
border: none;
overflow: hidden;
}
.btn::before {
content: "";
position: absolute;
top: 0;
left: -100%;
width: 100%;
height: 100%;
background: rgba(255, 255, 255, 0.2);
transition: left 0.3s ease;
}
.btn:hover::before {
left: 100%;
}.tooltip {
position: relative;
}
.tooltip::after {
content: attr(data-tooltip);
position: absolute;
bottom: 100%;
left: 50%;
transform: translateX(-50%);
background: black;
color: white;
padding: 5px 10px;
border-radius: 4px;
font-size: 12px;
opacity: 0;
pointer-events: none;
transition: opacity 0.3s;
}
.tooltip:hover::after {
opacity: 1;
}<span class="tooltip" data-tooltip="Tooltip text">Hover me</span>.counter {
counter-reset: item;
}
.counter li {
counter-increment: item;
}
.counter li::before {
content: counter(item) ". ";
font-weight: bold;
color: #007bff;
}/* Must specify content */
.element::before {
content: ""; /* Even if empty */
display: block;
width: 20px;
height: 20px;
background: red;
}
/* Doesn't work with img */
img::before {
content: "Text"; /* Won't display */
}❌ Wrong:
.element::before {
/* Forgot content */
background: red;
}✅ Correct:
.element::before {
content: "";
background: red;
}Pseudo-elements are powerful tools for creating decorative effects:
Use them to enhance visual design without changing HTML! 🎯