CSS Pseudo-elements
In this page:
selector::pseudo-element {
property: value;
}
p::first-line { }
element::before { content: "text"; }
What is a Pseudo-element?
Pseudo-elements are used to style specific parts of an element, or to insert virtual elements into your layout directly from your stylesheet, using a double colon (::) prefix to distinguish them from pseudo-classes.
Example: What is a Pseudo-element?
<style>
p::before {
content: "» ";
}
</style>
<p>Pseudo-element inserted before this text</p>
Inserting Content with ::before
The ::before pseudo-element allows you to insert virtual content directly before the content of an element. This is incredibly useful for adding decorative icons, bullet markers, or automated labels without cluttering your HTML files.
Example: Inserting Content with ::before
<style>
.secure::before {
content: "🔒 ";
}
</style>
<a class="secure" href="#">Secure Link</a>
Inserting Content with ::after
The ::after pseudo-element works exactly like ::before, but it inserts the virtual content directly after the content of the targeted element. It is commonly used to append file formats, external link icons, or clear floated parent containers.
Example: Inserting Content with ::after
<style>
a.pdf::after {
content: " (PDF)";
}
</style>
<a class="pdf" href="#">Download Report</a>
Custom Selection Colors with ::selection
The ::selection pseudo-element allows you to customize the highlight and text colors used when a user selects or highlights text on your webpage, replacing the browser's default blue highlighting with your own brand themes.
Example: Custom Selection Colors with ::selection
<style>
::selection {
background: gold;
color: black;
}
</style>
<p>Select this text to see a custom highlight color</p>
Styling Form Placeholders with ::placeholder
The ::placeholder pseudo-element allows you to style the temporary, grayed-out placeholder hints inside your form inputs, letting you customize their text color, font style, and opacity to coordinate with your website theme.
Example: Styling Form Placeholders with ::placeholder
<style>
input::placeholder {
color: #999;
font-style: italic;
}
</style>
<input type="text" placeholder="Enter your name">
- Forgetting to write the required content property inside ::before and ::after pseudo-elements, which prevents them from rendering.
- Using a single colon (:) instead of a double colon (::) when writing modern pseudo-elements.
- Attempting to apply unsupported styling properties (like font-size) inside ::selection declarations.
- Pseudo-elements allow you to create and style brand-new, virtual elements directly from your stylesheet.
- Use ::before and ::after to insert decorative content, symbols, or bullet markers before or after your elements.
- Customize selection highlights using ::selection, and style temporary form input hints using ::placeholder.
Standard CSS pseudo-elements (::before, ::after, ::selection) are natively supported by all modern web browsers.
Chapter Quiz — Complete all 22 topics to unlock
0/22 topics done
Complete these topics first:
- CSS Combinators
- CSS Pseudo-classes
- CSS Pseudo-elements
- CSS Opacity
- CSS Navigation Bar
- CSS Vertical Navbar
- CSS Horizontal Navbar
- CSS Dropdowns
- CSS Advanced Dropdowns
- CSS Image Gallery
- CSS Image Sprites
- CSS Attribute Selectors
- CSS Forms
- CSS Counters
- CSS Specificity
- CSS Specificity Hierarchy
- CSS Variables
- CSS !important
- CSS Box Sizing
- CSS Media Queries
- CSS Pointer Events
- CSS Outline