1. Home
  2. Coding Hub
  3. Coding Notes
  4. Css
  5. Pseudo Elements
45% OFFBLACK FRIDAY SALE
Sale ends in
03Days
12Hrs
21Mins
32Secs
Claim
  1. Home
  2. Coding Hub
  3. Coding Notes
  4. Css
  5. Pseudo Elements

Limited time offer

45% OFF

BLACK FRIDAY SALE

Sale ends in

03Days
12Hrs
21Mins
32Secs
Claim
HTMLCSSJavaScriptPythonJavaC++React.jsSQLNext.jsNode.js
Underrated CoderUnderrated Coder
Underrated CoderUnderrated Coder
Resources
  • Coding Hub
  • Roadmap
  • DSA Sheet
  • Coding Notes
  • Coding Quiz
  • Code Compiler
  • Coder AI
Interview Preparation
  • 1:1 Mentorship
  • Interview Q&A
Community
  • Blog
  • Coder Community
Help & Support
  • Contact Us
  • Troubleshooting
  • Report a Bug
HTMLCSSJavaScriptPythonJavaC++React.jsSQLNext.jsNode.js
Outline
CSS IntroductionCSSOM & ParsingBasic SelectorsCSS CombinatorsAttribute SelectorsThe CascadeSpecificity RulesCSS Inheritance
Box Modelbox-sizing ResetMargin CollapsePadding & BordersLogical PropertiesSizing UnitsViewport UnitsMath Functions
Web FontsVariable FontsFluid TypographyText FormattingText OverflowModern Colorscolor-mix() FunctionCSS Gradients
Display PropertyPosition MechanicsSticky PositioningStacking ContextAnchor PositioningOverflow ControlFilters & ShadowsAspect & object-fit
Pseudo-ClassesPseudo-Elements:is() & :where():has() SelectorCustom Properties@property RuleNative Nesting
Flexbox ArchitectureFlex DirectionAxis AlignmentFlex WrappingFlex SizingGap & Ordering
Grid ArchitectureGrid TracksGrid PlacementTemplate AreasAuto-Fit GridsCSS Subgrid
Media QueriesContainer QueriesContainer UnitsPreference QueriesCascade Layers@scope Rule
CSS TransitionsKeyframe AnimationsScroll AnimationsView TransitionsRendering Performance
CSS

Pseudo-Elements

Overview

If pseudo-classes target the state of an element, pseudo-elements target a specific sub-part of an element, or allow you to literally inject fake HTML elements into the DOM purely using CSS. The ::before and ::after pseudo-elements are the secret weapon of advanced CSS design. They allow you to add decorative icons, tooltips, or complex shadows without polluting your actual HTML file with empty <div> tags.

Syntax

css
/* Notice the DOUBLE colon (::) for pseudo-elements */

/* 1. Targeting parts of text */
p::first-line { font-weight: bold; } /* Makes the first line of text bold */
p::first-letter { font-size: 3rem; float: left; } /* Drop-cap effect */
::selection { background: blue; color: white; } /* Styling the user's highlight */

/* 2. INJECTING Virtual Elements */
/* Adding a red asterisk after required form labels */
.label-required::after {
  content: "*"; /* The content property is strictly required! */
  color: red;
  margin-left: 4px;
}

/* Creating an aesthetic quotation mark before a blockquote */
blockquote::before {
  content: '"';
  font-size: 4rem;
  color: gray;
  position: absolute;
}

Common Pitfalls

  • Forgetting the content property on ::before or ::after. If you don't declare content: ""; (even an empty string), the browser refuses to render the pseudo-element entirely. It must have content to exist.
  • Trying to attach ::before to a replaced element like an <img> or <input>. Pseudo-elements are injected inside the element. Since an <img> cannot have children, pseudo-elements will silently fail on them.

Interview Questions

What is the historical difference between a single colon (:) and a double colon (::) in CSS?

In CSS2, everything used a single colon. CSS3 introduced the double colon strictly for pseudo-elements (::before) to mathematically separate them from pseudo-classes (:hover). However, browsers still support single colons for ::before for legacy compatibility.

Real-World Example

Using an empty ::after element to create a beautiful hover underline animation.

example
css
.nav-link {
  position: relative;
  text-decoration: none;
}

/* The virtual underline line (hidden by default) */
.nav-link::after {
  content: "";
  position: absolute;
  bottom: -4px;
  left: 0;
  width: 0%; /* Starts invisible */
  height: 2px;
  background: blue;
  transition: width 0.3s ease;
}

/* When the parent is hovered, expand the virtual line! */
.nav-link:hover::after {
  width: 100%;
}

Check Your Knowledge

Test your understanding of Pseudo-Elements with these quick questions.

Question 1

Which CSS property MUST be declared on a `::before` or `::after` pseudo-element for it to render on the screen?

Take CSS Quiz
Outline
CSS IntroductionCSSOM & ParsingBasic SelectorsCSS CombinatorsAttribute SelectorsThe CascadeSpecificity RulesCSS Inheritance
Box Modelbox-sizing ResetMargin CollapsePadding & BordersLogical PropertiesSizing UnitsViewport UnitsMath Functions
Web FontsVariable FontsFluid TypographyText FormattingText OverflowModern Colorscolor-mix() FunctionCSS Gradients
Display PropertyPosition MechanicsSticky PositioningStacking ContextAnchor PositioningOverflow ControlFilters & ShadowsAspect & object-fit
Pseudo-ClassesPseudo-Elements:is() & :where():has() SelectorCustom Properties@property RuleNative Nesting
Flexbox ArchitectureFlex DirectionAxis AlignmentFlex WrappingFlex SizingGap & Ordering
Grid ArchitectureGrid TracksGrid PlacementTemplate AreasAuto-Fit GridsCSS Subgrid
Media QueriesContainer QueriesContainer UnitsPreference QueriesCascade Layers@scope Rule
CSS TransitionsKeyframe AnimationsScroll AnimationsView TransitionsRendering Performance
CSS

Pseudo-Elements

Overview

If pseudo-classes target the state of an element, pseudo-elements target a specific sub-part of an element, or allow you to literally inject fake HTML elements into the DOM purely using CSS. The ::before and ::after pseudo-elements are the secret weapon of advanced CSS design. They allow you to add decorative icons, tooltips, or complex shadows without polluting your actual HTML file with empty <div> tags.

Syntax

css
/* Notice the DOUBLE colon (::) for pseudo-elements */

/* 1. Targeting parts of text */
p::first-line { font-weight: bold; } /* Makes the first line of text bold */
p::first-letter { font-size: 3rem; float: left; } /* Drop-cap effect */
::selection { background: blue; color: white; } /* Styling the user's highlight */

/* 2. INJECTING Virtual Elements */
/* Adding a red asterisk after required form labels */
.label-required::after {
  content: "*"; /* The content property is strictly required! */
  color: red;
  margin-left: 4px;
}

/* Creating an aesthetic quotation mark before a blockquote */
blockquote::before {
  content: '"';
  font-size: 4rem;
  color: gray;
  position: absolute;
}

Common Pitfalls

  • Forgetting the content property on ::before or ::after. If you don't declare content: ""; (even an empty string), the browser refuses to render the pseudo-element entirely. It must have content to exist.
  • Trying to attach ::before to a replaced element like an <img> or <input>. Pseudo-elements are injected inside the element. Since an <img> cannot have children, pseudo-elements will silently fail on them.

Interview Questions

What is the historical difference between a single colon (:) and a double colon (::) in CSS?

In CSS2, everything used a single colon. CSS3 introduced the double colon strictly for pseudo-elements (::before) to mathematically separate them from pseudo-classes (:hover). However, browsers still support single colons for ::before for legacy compatibility.

Real-World Example

Using an empty ::after element to create a beautiful hover underline animation.

example
css
.nav-link {
  position: relative;
  text-decoration: none;
}

/* The virtual underline line (hidden by default) */
.nav-link::after {
  content: "";
  position: absolute;
  bottom: -4px;
  left: 0;
  width: 0%; /* Starts invisible */
  height: 2px;
  background: blue;
  transition: width 0.3s ease;
}

/* When the parent is hovered, expand the virtual line! */
.nav-link:hover::after {
  width: 100%;
}

Check Your Knowledge

Test your understanding of Pseudo-Elements with these quick questions.

Question 1

Which CSS property MUST be declared on a `::before` or `::after` pseudo-element for it to render on the screen?

Take CSS Quiz

CSS coding notes for Indian students — 62 topics with syntax, pitfalls, interview Q&A, and inline quizzes.

Topics in CSS

  1. CSS Introduction
  2. CSSOM & Parsing
  3. Basic Selectors
  4. CSS Combinators
  5. Attribute Selectors
  6. The Cascade
  7. Specificity Rules
  8. CSS Inheritance
  9. Box Model
  10. box-sizing Reset
  11. Margin Collapse
  12. Padding & Borders
  13. Logical Properties
  14. Sizing Units
  15. Viewport Units
  16. Math Functions
  17. Web Fonts
  18. Variable Fonts
  19. Fluid Typography
  20. Text Formatting
  21. Text Overflow
  22. Modern Colors
  23. color-mix() Function
  24. CSS Gradients
  25. Display Property
  26. Position Mechanics
  27. Sticky Positioning
  28. Stacking Context
  29. Anchor Positioning
  30. Overflow Control
  31. Filters & Shadows
  32. Aspect & object-fit
  33. Pseudo-Classes
  34. Pseudo-Elements
  35. :is() & :where()
  36. :has() Selector
  37. Custom Properties
  38. @property Rule
  39. Native Nesting
  40. Flexbox Architecture
  41. Flex Direction
  42. Axis Alignment
  43. Flex Wrapping
  44. Flex Sizing
  45. Gap & Ordering
  46. Grid Architecture
  47. Grid Tracks
  48. Grid Placement
  49. Template Areas
  50. Auto-Fit Grids
  51. CSS Subgrid
  52. Media Queries
  53. Container Queries
  54. Container Units
  55. Preference Queries
  56. Cascade Layers
  57. @scope Rule
  58. CSS Transitions
  59. Keyframe Animations
  60. Scroll Animations
  61. View Transitions
  62. Rendering Performance

Study next

  • All coding notes
  • CSS notes home
  • CSS certification quiz
  • Online compiler
  • Interview Q&A

CSS coding notes for Indian students — 62 topics with syntax, pitfalls, interview Q&A, and inline quizzes.

Topics in CSS

  1. CSS Introduction
  2. CSSOM & Parsing
  3. Basic Selectors
  4. CSS Combinators
  5. Attribute Selectors
  6. The Cascade
  7. Specificity Rules
  8. CSS Inheritance
  9. Box Model
  10. box-sizing Reset
  11. Margin Collapse
  12. Padding & Borders
  13. Logical Properties
  14. Sizing Units
  15. Viewport Units
  16. Math Functions
  17. Web Fonts
  18. Variable Fonts
  19. Fluid Typography
  20. Text Formatting
  21. Text Overflow
  22. Modern Colors
  23. color-mix() Function
  24. CSS Gradients
  25. Display Property
  26. Position Mechanics
  27. Sticky Positioning
  28. Stacking Context
  29. Anchor Positioning
  30. Overflow Control
  31. Filters & Shadows
  32. Aspect & object-fit
  33. Pseudo-Classes
  34. Pseudo-Elements
  35. :is() & :where()
  36. :has() Selector
  37. Custom Properties
  38. @property Rule
  39. Native Nesting
  40. Flexbox Architecture
  41. Flex Direction
  42. Axis Alignment
  43. Flex Wrapping
  44. Flex Sizing
  45. Gap & Ordering
  46. Grid Architecture
  47. Grid Tracks
  48. Grid Placement
  49. Template Areas
  50. Auto-Fit Grids
  51. CSS Subgrid
  52. Media Queries
  53. Container Queries
  54. Container Units
  55. Preference Queries
  56. Cascade Layers
  57. @scope Rule
  58. CSS Transitions
  59. Keyframe Animations
  60. Scroll Animations
  61. View Transitions
  62. Rendering Performance

Study next

  • All coding notes
  • CSS notes home
  • CSS certification quiz
  • Online compiler
  • Interview Q&A