CSS Pseudo-Elements
CSS pseudo-elements allow you to style specific parts of an HTML element or insert generated content without adding extra HTML elements.
What is a CSS Pseudo-Element?
A pseudo-element is a CSS keyword that lets you style a specific part of an element.
Pseudo-elements are written using two colons
::, such as ::before,
::after, ::first-letter,
and ::first-line.
Pseudo-elements help create visual effects and decorative content using CSS.
Pseudo-Element Syntax
selector::pseudo-element {
property: value;
}
Simple Explanation
The double colon :: tells CSS that we are
targeting a pseudo-element rather than the complete
HTML element.
How Pseudo-Elements Work
1. ::before
The ::before pseudo-element inserts generated
content before the actual content of an element.
.before-example::before {
content: "★ ";
color: #2563eb;
}
Output
CSS is powerful.
2. ::after
The ::after pseudo-element inserts generated
content after the actual content of an element.
.after-example::after {
content: " ✓";
color: #0f766e;
}
Output
Course Completed
Important
When using ::before or
::after to insert generated content,
the content property is normally required.
3. ::first-letter
The ::first-letter pseudo-element styles
the first letter of the text inside an element.
.first-letter-example::first-letter {
color: #7c3aed;
font-size: 40px;
font-weight: 700;
}
Output
Responsive design helps websites work well across different devices and screen sizes.
4. ::first-line
The ::first-line pseudo-element styles the
first line of text in an element.
.first-line-example::first-line {
color: #2563eb;
font-weight: 700;
}
Output
CSS makes webpages visually attractive and easier to understand. Proper styling improves readability, spacing, layout, and overall user experience.
5. ::selection
The ::selection pseudo-element styles the
portion of text selected by the user.
.selection-example::selection {
background-color: #2563eb;
color: #ffffff;
}
Output
Select this text with your mouse to see the custom selection color.
6. ::marker
The ::marker pseudo-element styles the marker
of list items.
.course-list::marker {
color: #2563eb;
}
Output
- HTML
- CSS
- JavaScript
- C#
7. ::placeholder
The ::placeholder pseudo-element styles
the placeholder text inside form controls.
.name-input::placeholder {
color: #7c3aed;
}
Output
Practical Example: Decorative Heading
Pseudo-elements can also be used to add decorative lines or icons around headings.
.decorative-heading::before {
content: "";
display: inline-block;
width: 35px;
height: 4px;
background-color: #2563eb;
margin-right: 10px;
vertical-align: middle;
}
Output
Learn CSS
Common CSS Pseudo-Elements
::before
Inserts content before an element
::after
Inserts content after an element
::first-letter
Styles the first letter
::first-line
Styles the first line
::selection
Styles selected text
::marker
Styles list markers
::placeholder
Styles placeholder text
Pseudo-Class vs Pseudo-Element
| Pseudo-Class | Pseudo-Element |
|---|---|
| Uses a single colon | Uses double colon |
Example: :hover |
Example: ::before |
| Targets a state or condition | Targets a specific part of an element |
| Often used for interaction | Often used for decoration or generated content |
Combined Example
.course-title {
color: #212529;
}
.course-title:hover {
color: #2563eb;
}
.course-title::before {
content: "★ ";
color: #f59e0b;
}
.course-title::after {
content: " ✓";
color: #0f766e;
}
Output
CSS Course
Hover over the heading to see the pseudo-class and pseudo-elements working together.
Best Practices
- Use pseudo-elements for presentation and decoration.
- Keep meaningful content in the HTML whenever it is important for accessibility or search engines.
-
Use
::beforeand::aftercarefully for generated content. - Test pseudo-element styles on different screen sizes.
- Keep decorative CSS separate from core content styles.
Important Note
Pseudo-elements are mainly useful for styling and decoration. Important information should normally be present in the HTML itself instead of being added only through CSS generated content.
Summary
CSS pseudo-elements allow developers to style specific
parts of elements or generate decorative content.
Common pseudo-elements include
::before, ::after,
::first-letter, ::first-line,
::selection, ::marker, and
::placeholder.