Pseudo-element Selectors
Content insertion, ::before, ::after, ::first-letter, and decorative effects.
Overview
Pseudo-elements style specific parts of elements.
Key Concepts
- ::before/::after — Insert content
- ::first-letter — Style first letter
- ::first-line — Style first line
- ::selection — Style selected text
- ::placeholder — Style input placeholders
Code Examples
/* Decorative content */
.quote::before {
content: open-quote;
font-size: 3rem;
line-height: 0;
vertical-align: -0.4em;
margin-right: 0.25em;
}
.quote::after {
content: close-quote;
font-size: 3rem;
line-height: 0;
vertical-align: -0.4em;
margin-left: 0.25em;
}
/* Tooltips */
.tooltip::after {
content: attr(data-tooltip);
position: absolute;
bottom: 100%;
left: 50%;
transform: translateX(-50%);
background: #333;
color: white;
padding: 0.5rem 1rem;
border-radius: 4px;
white-space: nowrap;
opacity: 0;
pointer-events: none;
transition: opacity 0.2s;
}
.tooltip:hover::after {
opacity: 1;
}
/* Drop cap */
.article p:first-of-type::first-letter {
float: left;
font-size: 4rem;
line-height: 0.8;
padding-right: 0.5rem;
font-weight: bold;
color: var(--primary);
}
/* First line styling */
.intro::first-line {
font-weight: bold;
color: var(--primary);
}
/* Selection styling */
::selection {
background: var(--primary);
color: white;
}
/* Placeholder styling */
.input::placeholder {
color: #999;
font-style: italic;
}
/* Icon with pseudo-element */
.icon::before {
content: '★';
margin-right: 0.5em;
}
/* Decorative line */
.heading {
position: relative;
display: inline-block;
}
.heading::after {
content: '';
position: absolute;
bottom: -8px;
left: 0;
width: 50px;
height: 3px;
background: var(--primary);
}
/* Custom list markers */
.custom-list li::before {
content: '→';
color: var(--primary);
margin-right: 0.5em;
}
/* Status indicators */
.status::before {
content: '';
display: inline-block;
width: 8px;
height: 8px;
border-radius: 50%;
margin-right: 0.5em;
}
.status.online::before {
background: green;
}
.status.offline::before {
background: red;
}
/* Content insertion with counter */
.list {
counter-reset: item;
}
.list-item::before {
counter-increment: item;
content: counter(item) '.';
font-weight: bold;
margin-right: 0.5em;
}
Practice
Create a blog with decorative drop caps and custom list markers.