Visual guide illustrating CSS pseudo-elements with code snippet syntax and DOM structure
Tutorials

CSS Pseudo-elements in Hindi: ::before, ::after और practical use cases

M
Maahi
8 November 2026 4 min read4 views

CSS Pseudo-elements जैसे ::before, ::after और ::marker का use करके बिना extra HTML markup लिखे clean और modern UI design बनाना सीखें। Vivaa Technologies की technical guide.

CSS Pseudo-elements: Modern Web Design का Secret Weapon

Frontend development में clean, maintainable और semantic HTML markup लिखना हर professional developer की top priority होती है। अक्सर UI design करते समय हमें किसी heading के आगे decorative icon add करना होता है, dynamic tooltip create करना होता है, या text selection styling customize करनी होती है।

Beginner developers इन visual effects के लिए extra <div> या <span> tags insert कर देते हैं, जिससे Document Object Model (DOM) unnecessarily heavy हो जाता है। इस problem का exact solution है CSS Pseudo-elements।

Vivaa Technologies के इस tutorial में हम detail में समझेंगे कि pseudo-elements क्या हैं, ये pseudo-classes से कैसे different हैं, और इनका practical implementation production-level projects में कैसे किया जाता है।

CSS Pseudo-elements क्या हैं?

CSS Pseudo-element एक keyword है जो selector के साथ append होकर target element के specific sub-part को style करने की capability provide करता है। यह directly HTML structure को modify किए बिना virtual elements create करने की permission देता है।

CSS3 specification के अनुसार, pseudo-elements को double colon (::) syntax के साथ define किया जाता है, ताकि इन्हें single colon (:) वाली pseudo-classes (जैसे :hover, :focus) से visually distinguish किया जा सके।

Syntax:

CSS

selector::pseudo-element {
  property: value;
}

Most Important CSS Pseudo-elements

1. ::before और ::after

ये दोनों modern web design में सबसे ज्यादा use होने वाले powerful pseudo-elements हैं। ये selected target element के content के immediate start या end में inline virtual content inject करते हैं।

Key Rule: ::before और ::after को browser engine में render करने के लिए content property declare करना mandatory है, चाहे value empty string (content: "";) ही क्यों न हो।

Example Code:

CSS

/* Decorative Badge Indicator */
.badge::before {
  content: "★ ";
  color: #ff9800;
}

/* Animated Button Arrow */
.cta-button::after {
  content: " →";
  display: inline-block;
  transition: transform 0.3s ease;
}

.cta-button:hover::after {
  transform: translateX(6px);
}

Common Use Cases:

  • Interactive button hover micro-interactions

  • Decorative visual underlines, gradient borders और background overlays

  • Tooltips, quotes और status badges create करना without extra HTML markup

2. ::selection

जब user browser window में किसी text content को cursor से highlight या select करता है, तब default operating system highlight style apply होता है। ::selection pseudo-element से text highlight background-color और text color को brand palette के according customize किया जा सकता है।

Example Code:

CSS

::selection {
  background-color: #0052cc;
  color: #ffffff;
}

3. ::placeholder

Forms और user inputs के UI polish के लिए यह standard pseudo-element है। Input fields के placeholder attribute text के font-size, font-weight, color और opacity को style करने के लिए इसका use होता है।

Example Code:

CSS

input::placeholder {
  color: #a0aec0;
  font-style: italic;
  font-size: 0.95rem;
}

4. ::first-letter और ::first-line

Editorial content, technical blogs और modern typography में text styling enhance करने के लिए ये दोनों pseudo-elements use होते हैं:

  • ::first-letter: Paragraph के पहले character को target करता है, जो drop-cap layouts बनाने के लिए standard method है।

  • ::first-line: Rendered text block की first line को target करता है, जो screen resolution और responsive viewport width के according dynamically adjust होती है।

Example Code:

CSS

.article-body p:first-of-type::first-letter {
  font-size: 3rem;
  font-weight: 700;
  float: left;
  line-height: 1;
  margin-right: 8px;
  color: #1a73e8;
}

5. ::marker

Standard ordered (<ol>) और unordered (<ul>) list items के bullet points, roman numerals और numeric list markers को direct target करता है। इसकी help से बिना custom layout hacks के marker color और font properties modify हो जाती हैं।

Example Code:

CSS

ul.tech-stack li::marker {
  color: #e91e63;
  font-size: 1.25em;
}

Difference: Pseudo-classes vs Pseudo-elements

इन दोनों core concepts के बीच clear distinction समझना बहुत जरूरी है:

  • Pseudo-classes (:): Element की dynamic operational state को target करती हैं जब user interaction होता है या specific DOM tree structure match होता है (जैसे :hover, :focus, :active, :nth-child())।

  • Pseudo-elements (::): Element के specific internal structural part या generated virtual child element को style करती हैं (जैसे ::before, ::after, ::selection, ::marker)।

Best Practices for Frontend Developers

  • Keep DOM Tree Lightweight: Pure visual styling, accent lines और decorative icons के लिए wrapper elements create करने के बजाय हमेशा pseudo-elements implement करें।

  • Maintain Web Accessibility: Screen readers ::before और ::after content को differently parse कर सकते हैं। Essential text content हमेशा HTML markup में रखें, generated content में नहीं।

  • Never Skip Content Property: Position absolute या background animations set करते समय empty string content: ""; add करना compulsory है, वरना browser element को render नहीं करेगा।

Modern web development standards follow करना और clean CSS architecture लिखना professional full-stack developer बनने का core foundation है। Vivaa Technologies के tutorials के साथ जुड़े रहें और production-ready frontend practices master करें।

Tags
#CSS#Frontend Development#Web Design#Full Stack#CSS Pseudo Elements#Web Development

Share this article