CSS Pseudo Elements

CSS Pseudo Elements allow you to style specific parts of an HTML element or insert virtual content without modifying the HTML document.

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;

}
CSS Pseudo Elements help create clean and attractive web designs with less HTML.

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.
Pseudo Elements allow you to style parts of an element without changing the HTML.

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
The 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;

}
CSS Pseudo Elements are widely used for decorative effects without adding extra HTML.

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
CSS Pseudo Elements make designs cleaner while keeping HTML simple.

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:" ⭐";

}
CSS Pseudo Elements allow you to create rich visual effects while keeping your HTML clean and organized.

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 content property with ::before and ::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.
Using pseudo elements correctly results in cleaner HTML and professional-looking web pages.

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;

}
Without the 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
Most modern browsers provide excellent support for CSS Pseudo Elements.

14. CSS Pseudo Element Interview Questions

  1. What is a CSS pseudo element?
  2. What is the purpose of the ::before pseudo element?
  3. Why is the content property required?
  4. What is the difference between ::before and ::after?
  5. How does ::first-letter work?
  6. What does ::first-line style?
  7. What is the purpose of ::selection?
  8. Can pseudo elements replace HTML elements?
  9. Can pseudo elements be combined with pseudo classes?
  10. Are CSS pseudo elements supported by modern browsers?
Practice these interview questions to strengthen your understanding of CSS Pseudo Elements.

15. Lesson Summary

In this lesson, you learned:
  • ✔ Introduction to CSS Pseudo Elements.
  • ✔ What pseudo elements are.
  • ::before pseudo element.
  • ::after pseudo element.
  • ::first-letter pseudo element.
  • ::first-line pseudo element.
  • ::selection pseudo element.
  • ✔ Practical examples.
  • ✔ Best Practices and Common Mistakes.
  • ✔ Browser Support and Interview Questions.
Congratulations! You have successfully completed the CSS Pseudo Elements lesson. You can now enhance web pages with generated content and style specific parts of elements using CSS pseudo elements.

Quick Quiz

Which CSS pseudo element inserts content before an element?