CSS Pseudo-elements: The Magic of Creating Virtual Elements â
The Essence of Pseudo-elements â
Pseudo-elements are special selectors in CSS that allow you to insert "virtual" elements into a document or style specific parts of elements without modifying the HTML structure.
Pseudo-elements use double colon :: syntax (CSS3 spec, but : single colon syntax is still valid for backward compatibility). You can think of pseudo-elements as "ghost elements created by CSS" â they exist in the rendering result but not in the HTML code.
/* Modern syntax (recommended) */
selector::pseudo-element {
property: value;
}
/* Legacy syntax (backward compatible) */
selector:pseudo-element {
property: value;
}Difference between Pseudo-elements and Pseudo-classes:
- Pseudo-classes (:hover, :focus) select specific states of elements
- Pseudo-elements (::before, ::after) create or select specific parts of elements
::before and ::after â
::before and ::after are the most commonly used pseudo-elements, inserting generated content before and after element content.
Basic Syntax â
.element::before {
content: ""; /* content property is required */
/* other styles */
}
.element::after {
content: "";
/* other styles */
}Important Rules:
- The
contentproperty is required, even if it's an empty string - Pseudo-elements are inline elements by default, usually need to set
displayproperty - Pseudo-elements are not in the DOM, cannot be directly accessed by JavaScript
- Pseudo-elements cannot be applied to replaced elements (like
<img>,<input>)
Decorative Icons â
/* Add icons */
.tip::before {
content: "đĄ";
margin-right: 8px;
}
.warning::before {
content: "â ī¸";
margin-right: 8px;
}
.success::after {
content: "â";
margin-left: 8px;
color: #27ae60;
}
/* Using Unicode */
.external-link::after {
content: "\2197"; /* â */
font-size: 0.8em;
margin-left: 4px;
}
/* Using images */
.pdf-link::before {
content: "";
display: inline-block;
width: 16px;
height: 16px;
background: url("pdf-icon.svg") no-repeat center;
background-size: contain;
margin-right: 6px;
vertical-align: middle;
}Quote Marks â
/* Auto-add quotes */
blockquote::before {
content: open-quote;
font-size: 3em;
color: #3498db;
line-height: 0;
margin-right: 10px;
vertical-align: -0.4em;
}
blockquote::after {
content: close-quote;
font-size: 3em;
color: #3498db;
line-height: 0;
margin-left: 10px;
vertical-align: -0.4em;
}
blockquote {
quotes: '"' '"' "'" "'"; /* Define quote style */
font-style: italic;
padding: 20px;
}Decorative Shapes â
/* Ribbon effect */
.ribbon {
position: relative;
background-color: #3498db;
color: white;
padding: 10px 20px;
display: inline-block;
}
.ribbon::before,
.ribbon::after {
content: "";
position: absolute;
top: 0;
width: 0;
height: 0;
border-style: solid;
}
.ribbon::before {
left: -20px;
border-width: 20px 20px 20px 0;
border-color: transparent #3498db transparent transparent;
}
.ribbon::after {
right: -20px;
border-width: 20px 0 20px 20px;
border-color: transparent transparent transparent #3498db;
}
/* Tag badge */
.tag {
position: relative;
background-color: #e74c3c;
color: white;
padding: 5px 10px;
margin-right: 15px;
}
.tag::after {
content: "";
position: absolute;
right: -15px;
top: 0;
width: 0;
height: 0;
border-top: 15px solid #e74c3c;
border-right: 15px solid transparent;
border-bottom: 15px solid #e74c3c;
}Counters â
/* Auto-numbering */
body {
counter-reset: section;
}
h2::before {
counter-increment: section;
content: "Section " counter(section) ": ";
color: #3498db;
font-weight: bold;
}
/* Nested counters */
.chapter {
counter-reset: subsection;
}
.chapter h2::before {
counter-increment: section;
content: counter(section) ". ";
}
.chapter h3::before {
counter-increment: subsection;
content: counter(section) "." counter(subsection) " ";
}
/* List styling */
ol {
counter-reset: custom-counter;
list-style: none;
}
ol li::before {
counter-increment: custom-counter;
content: counter(custom-counter) ". ";
color: #3498db;
font-weight: bold;
margin-right: 8px;
}Displaying Attribute Values â
/* Display link URL */
a[href]::after {
content: " (" attr(href) ")";
font-size: 0.8em;
color: #777;
}
/* Display abbreviation full name in print styles */
@media print {
abbr[title]::after {
content: " (" attr(title) ")";
font-style: italic;
}
}
/* Display image alt text */
img::after {
content: attr(alt);
display: block;
text-align: center;
font-size: 0.9em;
color: #666;
margin-top: 5px;
}::first-letter - First Letter â
Selects the first letter of a block-level element, commonly used to create drop cap effects:
/* Classic drop cap */
p::first-letter {
font-size: 3em;
font-weight: bold;
float: left;
line-height: 1;
margin: 0 10px 0 0;
color: #e74c3c;
}
/* Decorative first letter */
article p:first-of-type::first-letter {
font-size: 4em;
font-family: "Georgia", serif;
color: #3498db;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
float: left;
padding: 10px;
margin-right: 10px;
}
/* First letter with border */
.chapter::first-letter {
font-size: 2.5em;
font-weight: bold;
color: white;
background-color: #2c3e50;
padding: 10px 15px;
margin-right: 10px;
float: left;
border-radius: 4px;
}::first-line - First Line â
Selects the first line of text in a block-level element:
/* Highlight first line */
p::first-line {
font-weight: bold;
color: #2c3e50;
font-size: 1.1em;
}
/* News article style */
article p:first-of-type::first-line {
text-transform: uppercase;
letter-spacing: 2px;
font-weight: 600;
}
/* Combined usage */
.intro::first-line {
font-variant: small-caps;
color: #3498db;
}
.intro::first-letter {
font-size: 2em;
color: #e74c3c;
}::selection - Text Selection â
Customize the style when users select text:
/* Global selection style */
::selection {
background-color: #3498db;
color: white;
}
/* Specific element selection style */
code::selection {
background-color: #2c3e50;
color: #f39c12;
}
.highlight::selection {
background-color: #f39c12;
color: #2c3e50;
}
/* Firefox compatibility */
::-moz-selection {
background-color: #3498db;
color: white;
}::placeholder - Placeholder â
Customize the style of form input box placeholders:
/* Basic placeholder style */
input::placeholder {
color: #95a5a6;
font-style: italic;
opacity: 1;
}
textarea::placeholder {
color: #bdc3c7;
font-size: 0.9em;
}
/* Placeholder change on focus */
input:focus::placeholder {
color: transparent;
}
/* Different browser prefixes */
input::-webkit-input-placeholder {
color: #95a5a6;
}
input::-moz-placeholder {
color: #95a5a6;
opacity: 1;
}
input:-ms-input-placeholder {
color: #95a5a6;
}Practical Applications â
Clearfix â
.clearfix::after {
content: "";
display: table;
clear: both;
}Loading Animation â
.loading::after {
content: "";
display: inline-block;
width: 20px;
height: 20px;
border: 3px solid #f3f3f3;
border-top: 3px solid #3498db;
border-radius: 50%;
animation: spin 1s linear infinite;
margin-left: 10px;
vertical-align: middle;
}
@keyframes spin {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}Tooltip â
.tooltip {
position: relative;
cursor: help;
}
.tooltip::before {
content: attr(data-tooltip);
position: absolute;
bottom: 100%;
left: 50%;
transform: translateX(-50%);
background-color: #2c3e50;
color: white;
padding: 8px 12px;
border-radius: 4px;
white-space: nowrap;
opacity: 0;
pointer-events: none;
transition: opacity 0.3s;
margin-bottom: 8px;
}
.tooltip::after {
content: "";
position: absolute;
bottom: 100%;
left: 50%;
transform: translateX(-50%);
border: 6px solid transparent;
border-top-color: #2c3e50;
opacity: 0;
pointer-events: none;
transition: opacity 0.3s;
margin-bottom: 2px;
}
.tooltip:hover::before,
.tooltip:hover::after {
opacity: 1;
}Breadcrumb Navigation â
.breadcrumb {
list-style: none;
display: flex;
padding: 0;
}
.breadcrumb li {
display: flex;
align-items: center;
}
.breadcrumb li:not(:last-child)::after {
content: "âē";
margin: 0 10px;
color: #95a5a6;
font-size: 1.2em;
}
.breadcrumb li:last-child {
color: #3498db;
font-weight: bold;
}Progress Bar â
.progress {
width: 100%;
height: 30px;
background-color: #ecf0f1;
border-radius: 15px;
overflow: hidden;
position: relative;
}
.progress::before {
content: "";
position: absolute;
top: 0;
left: 0;
height: 100%;
width: var(--progress, 0%);
background: linear-gradient(90deg, #3498db, #2ecc71);
border-radius: 15px;
transition: width 0.3s ease;
}
.progress::after {
content: attr(data-label);
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
color: #2c3e50;
font-weight: bold;
z-index: 1;
}Card Decoration â
.card {
position: relative;
background: white;
border-radius: 8px;
padding: 20px;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
overflow: hidden;
}
/* Top decoration bar */
.card::before {
content: "";
position: absolute;
top: 0;
left: 0;
right: 0;
height: 4px;
background: linear-gradient(90deg, #667eea, #764ba2);
}
/* Corner badge */
.card::after {
content: "NEW";
position: absolute;
top: 20px;
right: -30px;
background-color: #e74c3c;
color: white;
padding: 5px 40px;
transform: rotate(45deg);
font-size: 0.8em;
font-weight: bold;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}Button Hover Effect â
.button {
position: relative;
padding: 12px 24px;
background-color: #3498db;
color: white;
border: none;
border-radius: 4px;
overflow: hidden;
cursor: pointer;
transition: color 0.3s;
}
.button::before {
content: "";
position: absolute;
top: 0;
left: -100%;
width: 100%;
height: 100%;
background-color: #2980b9;
transition: left 0.3s;
z-index: -1;
}
.button:hover::before {
left: 0;
}
.button:hover {
color: white;
}Divider Lines â
.section-title {
text-align: center;
position: relative;
margin: 40px 0;
}
.section-title::before,
.section-title::after {
content: "";
position: absolute;
top: 50%;
width: 40%;
height: 1px;
background-color: #ddd;
}
.section-title::before {
left: 0;
}
.section-title::after {
right: 0;
}
/* Decorative divider */
.divider {
text-align: center;
margin: 30px 0;
position: relative;
}
.divider::before {
content: "";
position: absolute;
left: 0;
top: 50%;
width: 100%;
height: 1px;
background: linear-gradient(90deg, transparent, #3498db, transparent);
}
.divider::after {
content: "âĻ";
position: relative;
display: inline-block;
padding: 0 20px;
background-color: white;
color: #3498db;
font-size: 20px;
}Best Practices â
- Always include the content property: Even if it's an empty string, write
content: "" - Pay attention to z-index: Pseudo-elements might cover other content
- Performance considerations: Excessive use of pseudo-elements may affect rendering performance
- Clear semantics: Use pseudo-elements for decorative content, use HTML for substantive content
- Accessibility: Pseudo-element content may not be visible to screen readers
- Browser compatibility: Older browsers may require single colon syntax
Summary â
Pseudo-elements are one of the most creative features in CSS. They allow us to add rich visual effects and decorative elements without polluting the HTML structure.
Key Points:
- ::before and ::after: Most commonly used pseudo-elements, insert virtual elements before and after content
contentproperty is required- Can be used for icons, quotes, shapes, counters, attribute value display, etc.
- Default as inline elements, often need to set
display
- ::first-letter: Select first letter, implement drop cap and other effects
- ::first-line: Select first line, highlight first line of text
- ::selection: Customize text selection style
- ::placeholder: Customize form placeholder style
- Practical Applications: Clearfix, loading animations, tooltips, breadcrumb navigation, progress bars, decorative effects
- Best Practices: Use reasonably, pay attention to performance, maintain semantics, consider accessibility