CSS Pseudo Elements
1. Introduction to CSS Pseudo Elements
CSS Pseudo Elements are special keywords that allow you to style a specific part of an element or create virtual elements before or after an element's content.
Why Use Pseudo Elements?
- Add decorative content without HTML.
- Style the first letter or first line of text.
- Create icons, badges, and labels.
- Reduce unnecessary HTML elements.
- Improve website design and readability.
p::first-letter{
font-size:40px;
}
2. What are Pseudo Elements?
A pseudo element begins with two colons
(::) and is used to style a particular
part of an element or insert generated content.
selector::pseudo-element{
property:value;
}
| Pseudo Element | Description |
|---|---|
| ::before | Inserts content before an element. |
| ::after | Inserts content after an element. |
| ::first-letter | Styles the first letter. |
| ::first-line | Styles the first line of text. |
| ::selection | Styles selected text. |
3. ::before Pseudo Element
The ::before pseudo element inserts content
before the actual content of an element.
h2::before{
content:"★ ";
color:gold;
}
button::before{
content:"👉 ";
}
| Use | Example |
|---|---|
| Icons | ★ Title |
| Labels | 👉 Button |
| Decorations | Custom symbols |
content property is required when using ::before.
4. ::after Pseudo Element
The ::after pseudo element inserts content
after the content of an element.
h2::after{
content:" ✓";
color:green;
}
a::after{
content:" ↗";
}
| Use | Example |
|---|---|
| Icons | Link ↗ |
| Status | Completed ✓ |
| Decorative Symbols | Custom text |
::after is widely used to create badges, arrows, and decorative effects.
5. ::first-letter Pseudo Element
The ::first-letter pseudo element styles
the first letter of a block-level element.
p::first-letter{
font-size:40px;
color:red;
font-weight:bold;
}
article::first-letter{
float:left;
margin-right:5px;
}
| Property | Purpose |
|---|---|
| font-size | Increase letter size. |
| color | Change text color. |
| font-weight | Make the letter bold. |
| float | Create a drop-cap effect. |
::first-letter is commonly used in magazines, newspapers, and blogs to create decorative drop caps.
6. ::first-line Pseudo Element
The ::first-line pseudo element styles only
the first line of a block-level element. The styled line
changes automatically when the width of the element changes.
p::first-line{
color:blue;
font-weight:bold;
}
article::first-line{
text-transform:uppercase;
}
| Property | Purpose |
|---|---|
| color | Changes text color. |
| font-weight | Makes text bold. |
| text-transform | Changes letter case. |
::first-line is useful for highlighting the beginning of articles and blog posts.
7. ::selection Pseudo Element
The ::selection pseudo element styles the text
selected by the user using the mouse or keyboard.
::selection{
background:yellow;
color:black;
}
p::selection{
background:#007bff;
color:white;
}
| Property | Purpose |
|---|---|
| background | Changes selected text background. |
| color | Changes selected text color. |
::selection improves readability and provides a better user experience.
8. Practical CSS Pseudo Element Examples
Example 1 : Add an Icon Before a Heading
h2::before{
content:"📘 ";
}
Example 2 : Add a Check Mark After Text
.success::after{
content:" ✔";
color:green;
}
Example 3 : Style the First Letter
p::first-letter{
font-size:35px;
color:red;
}
Example 4 : Customize Selected Text
::selection{
background:black;
color:white;
}
9. Advantages of CSS Pseudo Elements
- Reduce unnecessary HTML elements.
- Create decorative content easily.
- Improve typography.
- Enhance website appearance.
- Easy to maintain.
- Improve code readability.
- Support creative UI designs.
| Feature | Available |
|---|---|
| Generated Content | ✔ Yes |
| Typography Effects | ✔ Yes |
| Decorative Styling | ✔ Yes |
10. CSS Pseudo Element Comparison Table
| Pseudo Element | Purpose | Example |
|---|---|---|
| ::before | Adds content before an element. | h2::before |
| ::after | Adds content after an element. | p::after |
| ::first-letter | Styles the first letter. | p::first-letter |
| ::first-line | Styles the first line. | p::first-line |
| ::selection | Styles selected text. | ::selection |
h3::before{
content:"⭐ ";
}
h3::after{
content:" ⭐";
}
11. CSS Pseudo Element Best Practices
Following best practices helps you create clean, maintainable, and attractive web pages using CSS Pseudo Elements.
- Use pseudo elements for decorative content only.
- Always use the
contentproperty with::beforeand::after. - Keep generated content simple and meaningful.
- Use pseudo elements to reduce unnecessary HTML.
- Combine pseudo elements with transitions for better UI effects.
- Maintain accessibility by avoiding important information in generated content.
- Keep typography effects consistent.
- Test pseudo elements across different browsers.
12. Common CSS Pseudo Element Mistakes
| Mistake | Correct Practice |
|---|---|
Forgetting the content property. |
Always specify content for ::before and ::after. |
| Using pseudo elements for important text. | Keep important content inside the HTML. |
| Adding unnecessary HTML elements. | Use pseudo elements for decorative purposes. |
| Using excessive decorative content. | Keep the design clean and simple. |
| Ignoring browser testing. | Test your design in different browsers. |
❌ Poor
h2::before{
color:red;
}
✔ Better
h2::before{
content:"★ ";
color:red;
}
content property, the ::before and ::after pseudo elements will not appear.
13. Browser Support
CSS Pseudo Elements are fully supported by all modern web browsers.
| Browser | Support |
|---|---|
| Google Chrome | ✔ Supported |
| Mozilla Firefox | ✔ Supported |
| Microsoft Edge | ✔ Supported |
| Safari | ✔ Supported |
| Opera | ✔ Supported |
14. CSS Pseudo Element Interview Questions
- What is a CSS pseudo element?
- What is the purpose of the
::beforepseudo element? - Why is the
contentproperty required? - What is the difference between
::beforeand::after? - How does
::first-letterwork? - What does
::first-linestyle? - What is the purpose of
::selection? - Can pseudo elements replace HTML elements?
- Can pseudo elements be combined with pseudo classes?
- Are CSS pseudo elements supported by modern browsers?
15. Lesson Summary
In this lesson, you learned:
- ✔ Introduction to CSS Pseudo Elements.
- ✔ What pseudo elements are.
- ✔
::beforepseudo element. - ✔
::afterpseudo element. - ✔
::first-letterpseudo element. - ✔
::first-linepseudo element. - ✔
::selectionpseudo element. - ✔ Practical examples.
- ✔ Best Practices and Common Mistakes.
- ✔ Browser Support and Interview Questions.
Quick Quiz
Which CSS pseudo element inserts content before an element?