CSS Pseudo-elements
In this page:
selector::pseudo-element {
property: value;
}
p::first-line { }
element::before { content: "text"; }
Pseudo-element क्या है?
Pseudo-elements किसी element के specific हिस्सों को style करने के लिए, या अपनी stylesheet से सीधे अपने layout में virtual elements insert करने के लिए इस्तेमाल होते हैं, उन्हें pseudo-classes से अलग दिखाने के लिए एक double colon (::) prefix इस्तेमाल करते हुए।
उदाहरण: What is a Pseudo-element?
<style>
p::before {
content: "» ";
}
</style>
<p>Pseudo-element inserted before this text</p>
::before से Content Insert करना
::before pseudo-element आपको किसी element के content से ठीक पहले virtual content insert करने देता है। यह अपनी HTML files को cluttered किए बिना decorative icons, bullet markers, या automated labels जोड़ने के लिए बेहद उपयोगी है।
उदाहरण: Inserting Content with ::before
<style>
.secure::before {
content: "🔒 ";
}
</style>
<a class="secure" href="#">Secure Link</a>
::after से Content Insert करना
::after pseudo-element बिल्कुल ::before जैसा काम करता है, लेकिन यह targeted element के content के ठीक बाद virtual content insert करता है। यह आमतौर पर file formats, external link icons जोड़ने, या floated parent containers को clear करने के लिए इस्तेमाल होता है।
उदाहरण: Inserting Content with ::after
<style>
a.pdf::after {
content: " (PDF)";
}
</style>
<a class="pdf" href="#">Download Report</a>
::selection से Custom Selection Colors
::selection pseudo-element आपको उन highlight और text colors को customize करने देता है जो तब इस्तेमाल होते हैं जब कोई user आपकी webpage पर text select या highlight करता है, browser के default blue highlighting को अपने खुद के brand themes से बदलते हुए।
उदाहरण: Custom Selection Colors with ::selection
<style>
::selection {
background: gold;
color: black;
}
</style>
<p>Select this text to see a custom highlight color</p>
::placeholder से Form Placeholders को Style करना
::placeholder pseudo-element आपको अपने form inputs के अंदर temporary, grayed-out placeholder hints को style करने देता है, उनकी text color, font style, और opacity को अपनी website theme के साथ coordinate करने के लिए customize करते हुए।
उदाहरण: Styling Form Placeholders with ::placeholder
<style>
input::placeholder {
color: #999;
font-style: italic;
}
</style>
<input type="text" placeholder="Enter your name">
- ::before और ::after pseudo-elements के अंदर ज़रूरी content property लिखना भूल जाना, जो उन्हें render होने से रोकता है।
- modern pseudo-elements लिखते समय double colon (::) के बजाय single colon (:) इस्तेमाल करना।
- ::selection declarations के अंदर unsupported styling properties (जैसे font-size) लागू करने की कोशिश करना।
- Pseudo-elements आपको अपनी stylesheet से सीधे बिल्कुल नए, virtual elements बनाने और style करने देते हैं।
- अपने elements से पहले या बाद में decorative content, symbols, या bullet markers insert करने के लिए ::before और ::after इस्तेमाल करें।
- ::selection इस्तेमाल करके selection highlights customize करें, और ::placeholder इस्तेमाल करके temporary form input hints style करें।
Standard CSS pseudo-elements (::before, ::after, ::selection) हर modern web browser द्वारा natively supported हैं।
Chapter Quiz — Complete all 22 topics to unlock
0/22 topics done
Complete these topics first:
- CSS Combinators
- CSS Pseudo-classes
- CSS Pseudo-elements
- CSS Opacity
- CSS Navigation Bar
- CSS Vertical Navbar
- CSS Horizontal Navbar
- CSS Dropdowns
- CSS Advanced Dropdowns
- CSS Image Gallery
- CSS Image Sprites
- CSS Attribute Selectors
- CSS Forms
- CSS Counters
- CSS Specificity
- CSS Specificity Hierarchy
- CSS Variables
- CSS !important
- CSS Box Sizing
- CSS Media Queries
- CSS Pointer Events
- CSS Outline