CSS Pseudo-classes
In this page:
selector:pseudo-class {
property: value;
}
a:hover { }
li:first-child { }
input:focus { }
Interactive State Selectors
Interactive pseudo-classes elements को सिर्फ तभी target करते हैं जब users उनके साथ interact करते हैं, offer करते हुए: hover (mouse hover), active (click press पर), और focus (Tab key से focused या selected होने पर)।
उदाहरण: Interactive State Selectors
<style>
a:hover {
color: red;
}
a:active {
color: darkred;
}
a:focus {
outline: 2px solid blue;
}
</style>
<a href="#">Hover, click, or tab to me</a>
Structural Child Selectors
Structural child selectors elements को parent element के child tree में उनकी index position के आधार पर target करते हैं, offer करते हुए: first-child (पहला item), last-child (आख़िरी item), और nth-child (specific mathematical offsets या patterns)।
उदाहरण: Structural child selectors
<style>
li:first-child {
font-weight: bold;
}
li:last-child {
color: red;
}
li:nth-child(even) {
background: #eee;
}
</style>
<ul>
<li>One</li>
<li>Two</li>
<li>Three</li>
<li>Four</li>
</ul>
Form Validation Selectors
Form validation pseudo-classes आपको input fields को उनकी current validation state के आधार पर dynamically style करने देते हैं, offer करते हुए: required (mandatory fields), invalid (failed pattern check), और checked (active toggles)।
उदाहरण: Form validation Selectors
<style>
input:required {
border-color: orange;
}
input:invalid {
border-color: red;
}
input:checked {
outline: 2px solid green;
}
</style>
<input type="text" required>
<input type="checkbox" checked>
Negation Selector (:not)
Negation selector (:not) आपको उन elements को target करने देता है जो किसी specified selector rule से match नहीं करते। यह items की lists पर general styles लागू करते समय specific elements को exclude करने के लिए बेहद उपयोगी है।
उदाहरण: The negation Selector (:not)
<style>
li:not(:last-child) {
margin-bottom: 10px;
}
</style>
<ul>
<li>One</li>
<li>Two</li>
<li>Three (no margin below)</li>
</ul>
Attribute Target से Styling
Target selector (:target) आपको अपने page पर किसी specific element को style करने देता है जब उसकी ID browser URL path में active hash symbol (#) से match करती है। यह section targets को dynamically highlight करने के लिए बेहद उपयोगी है।
उदाहरण: Styling by attribute Target
<style>
:target {
background: yellow;
}
</style>
<a href="#section1">Jump to Section 1</a>
<div id="section1">Section 1 content</div>
- यह भूल जाना कि pseudo-classes को हमेशा आपकी stylesheets में colon (:) से prefix होना चाहिए।
- link pseudo-classes को गलत order में लिखना, जिससे hover या active styles render होने में fail हो जाते हैं।
- यह मान लेना कि structural child selectors (जैसे nth-child) अलग types के sibling elements को नज़रअंदाज़ करते हैं।
- Pseudo-classes elements को सिर्फ तभी target करते हैं जब वे किसी specific interaction या structural state में जाएँ।
- :hover, :active, और :focus selectors इस्तेमाल करके interactive states को साफ़ तरीके से style करें।
- :first-child, :last-child, या :nth-child इस्तेमाल करके specific element indices को target करें, और :not इस्तेमाल करके rules negate करें।
Standard CSS pseudo-classes को हर modern web browser में native support है।
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